0% found this document useful (0 votes)
12 views8 pages

Exercise Final

The document outlines the implementation of k-Nearest Neighbors (K-NN) for classification and a Convolution Neural Network (CNN) for image classification using Python. It details the algorithms, libraries needed, and provides code examples for both K-NN using the Iris dataset and CNN using the CIFAR-10 dataset. The results show successful predictions and accuracy for both models.

Uploaded by

cerani1307
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)
12 views8 pages

Exercise Final

The document outlines the implementation of k-Nearest Neighbors (K-NN) for classification and a Convolution Neural Network (CNN) for image classification using Python. It details the algorithms, libraries needed, and provides code examples for both K-NN using the Iris dataset and CNN using the CIFAR-10 dataset. The results show successful predictions and accuracy for both models.

Uploaded by

cerani1307
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/ 8

Exercise:13

Implement k-Nearest Neighbors(K-NN) for classification

Aim:
To Implement k-Nearest Neighbors(K-NN) for classification
Alogrithm:

1. Import Libraries: You'll need libraries like NumPy and optionally Matplotlib for
visualization.
2. Prepare the Dataset: Use a dataset for testing. The Iris dataset is a common choice.
3. Distance Calculation: Implement a function to calculate the distance between points.
4. Finding Neighbors: Write a function to find the k-nearest neighbors.
5. Prediction: Implement a function to predict the class of a new point based on its
neighbors.

Program:
import numpy as np
from collections import Counter
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

class KNN:
def __init__(self, k=3):
self.k = k

def fit(self, X, y):


self.X_train = X
self.y_train = y

def predict(self, X):


predictions = [self._predict(x) for x in X]
return np.array(predictions)

def _predict(self, x):


# Compute distances between x and all examples in the training set
distances = np.linalg.norm(self.X_train - x, axis=1)

# Sort by distance and return indices of the first k neighbors


k_indices = np.argsort(distances)[:self.k]

# Extract the labels of the k nearest neighbor


k_nearest_labels = [self.y_train[i] for i in k_indices]

# Return the most common class label among the k neighbors


most_common = Counter(k_nearest_labels).most_common(1)
return most_common[0][0]

# Load Iris dataset


iris = load_iris()
X = iris.data
y = iris.target

# Split the dataset into training and testing sets


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

# Create and fit the K-NN model


k=3
model = KNN(k=k)
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = np.mean(predictions == y_test)
print(f'Predictions: {predictions}')
print(f'True Labels: {y_test}')
print(f'Accuracy: {accuracy:.2f}')

Output:
Predictions: [0 2 0 1 0]
True Labels: [0 2 0 1 0]
Accuracy: 1.00

Result:
Thus k-Nearest Neighbors(K-NN) are implemented successfully using python
Exericse:14
Build a Convolution Neural Network(CNN) for image classification
Aim:
To implement Build a Convolution Neural Network(CNN) for image classification
Alogrithm:
1 Install Required Libraries
2 Import Necessary Libraries
3 Load and Preprocess the Dataset
4 Build the CNN Model
5Compile the Model
6Train the Model
7 Evaluate the Model
8 Visualize Training History
9 Make Predictions
Program:
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
# Load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Normalize pixel values to be between 0 and 1


x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Convert labels to categorical one-hot encoding


y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
model = models.Sequential()

# First convolutional block


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

# Second convolutional block


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

# Third convolutional block


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

# Flatten the output


model.add(layers.Flatten())

# Fully connected layers


model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax')) # 10 classes for CIFAR-10
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test,
y_test))
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc}')
# Plot training & validation accuracy values
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.show()

# Plot training & validation loss values


plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend()
plt.show()
# Make predictions on the test data
predictions = model.predict(x_test)

# Show a sample prediction


import numpy as np

# Display the first test image and its predicted label


plt.imshow(x_test[0])
plt.title(f'Predicted label: {np.argmax(predictions[0])}')
plt.axis('off')
plt.show()
Output:
Test accuracy: 0.7500

Result:
Thus Convolution Neural Network(CNN) for image classification implemented
successfully using python

You might also like