0% found this document useful (0 votes)
8 views4 pages

AI_Python_Programs_with_Code

Uploaded by

hemaparvathi143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

AI_Python_Programs_with_Code

Uploaded by

hemaparvathi143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs for Practicing Artificial Intelligence

Beginner Level

1. Linear Regression Example

import numpy as np

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

# Sample data: house sizes and prices

house_sizes = np.array([[1500], [1700], [1800], [2400], [3000]])

house_prices = np.array([300000, 320000, 340000, 360000, 400000])

# Splitting the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(house_sizes, house_prices,

test_size=0.2, random_state=0)

# Create the linear regression model

model = LinearRegression()

model.fit(X_train, y_train)

# Predict house prices

predictions = model.predict(X_test)

print("Predicted prices:", predictions)

# Plotting the results


plt.scatter(house_sizes, house_prices, color='blue')

plt.plot(X_test, predictions, color='red')

plt.xlabel('House Size (sq ft)')

plt.ylabel('Price ($)')

plt.show()

Intermediate Level

2. K-Nearest Neighbors (KNN) Example

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from sklearn.metrics import accuracy_score

# Load the iris dataset

iris = load_iris()

X, y = iris.data, iris.target

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,

random_state=42)

# Create the KNN model

knn = KNeighborsClassifier(n_neighbors=5)

knn.fit(X_train, y_train)

# Make predictions
y_pred = knn.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

Advanced Level

3. Convolutional Neural Network (CNN) Example

import tensorflow as tf

from tensorflow.keras import layers, models

from tensorflow.keras.datasets import cifar10

# Load CIFAR-10 dataset

(X_train, y_train), (X_test, y_test) = cifar10.load_data()

# Normalize pixel values

X_train, X_test = X_train / 255.0, X_test / 255.0

# Create CNN model

model = models.Sequential()

model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Add Dense layers

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))

model.add(layers.Dense(10, activation='softmax'))

# Compile the model

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',

metrics=['accuracy'])

# Train the model

model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))

You might also like