Exercise Final
Exercise Final
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
# 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()
Result:
Thus Convolution Neural Network(CNN) for image classification implemented
successfully using python