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

Ai Manual Astha 31 34

Uploaded by

spamanna1510
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)
10 views4 pages

Ai Manual Astha 31 34

Uploaded by

spamanna1510
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

Introduction to Artificial Intelligence (3171105)

EXPERIMENT: 10

AIM: Create a program using a convolution neural network that identifies


objects like water bottles, caps, books, etc using the webcam.

CODE:
import numpy as np
import tensorflow as tf
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load the Iris dataset


iris = datasets.load_iris()

# We will use only two classes for binary classification


X = iris.data
y = (iris.target != 0).astype(int) # Classify as 0 or 1 (binary classification: not
setosa vs. setosa)

# Split the dataset into train and test sets


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

# Standardize the features (mean=0, variance=1) for better convergence


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build the Logistic Regression model using Keras


model = Sequential()

# Logistic Regression is essentially a single-layer neural network with sigmoid


activation
model.add(Dense(1, input_dim=X_train.shape[1], activation='sigmoid'))

Astha Panchasara 210170111052


Introduction to Artificial Intelligence (3171105)

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])

# Train the model


history = model.fit(X_train, y_train, epochs=100, batch_size=10,
validation_data=(X_test, y_test), verbose=1)

# Evaluate the model on the test data


test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"\nTest Accuracy: {test_acc:.4f}")

# Make predictions on the test set


y_pred = (model.predict(X_test) > 0.5).astype(int)

# Optional: Display training history


import matplotlib.pyplot as plt

# Plot the training and validation accuracy over epochs


plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy over Epochs')
plt.show()

OUTPUT:

Astha Panchasara 210170111052


Introduction to Artificial Intelligence (3171105)

Astha Panchasara 210170111052


Introduction to Artificial Intelligence (3171105)

CONCLUSION: In this practical, we have learned to implement the Logistic


regression model using the TensorFlow model.

Astha Panchasara 210170111052

You might also like