0% found this document useful (0 votes)
22 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)
22 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

Python Programs for Practicing Artificial Intelligence

Beginner Level

1. Linear Regression Example

import numpy as np

import [Link] as plt

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

# Sample data: house sizes and prices

house_sizes = [Link]([[1500], [1700], [1800], [2400], [3000]])

house_prices = [Link]([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()

[Link](X_train, y_train)

# Predict house prices

predictions = [Link](X_test)

print("Predicted prices:", predictions)

# Plotting the results


[Link](house_sizes, house_prices, color='blue')

[Link](X_test, predictions, color='red')

[Link]('House Size (sq ft)')

[Link]('Price ($)')

[Link]()

Intermediate Level

2. K-Nearest Neighbors (KNN) Example

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import KNeighborsClassifier

from [Link] import accuracy_score

# Load the iris dataset

iris = load_iris()

X, y = [Link], [Link]

# 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)

[Link](X_train, y_train)

# Make predictions
y_pred = [Link](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 [Link] import layers, models

from [Link] 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 = [Link]()

[Link](layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))

[Link](layers.MaxPooling2D((2, 2)))

[Link](layers.Conv2D(64, (3, 3), activation='relu'))

[Link](layers.MaxPooling2D((2, 2)))

[Link](layers.Conv2D(64, (3, 3), activation='relu'))

# Add Dense layers

[Link]([Link]())
[Link]([Link](64, activation='relu'))

[Link]([Link](10, activation='softmax'))

# Compile the model

[Link](optimizer='adam', loss='sparse_categorical_crossentropy',

metrics=['accuracy'])

# Train the model

[Link](X_train, y_train, epochs=10, validation_data=(X_test, y_test))

You might also like