Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views3 pages

ML Lab Manual Exp11 To 18

The document outlines a series of experiments using different machine learning algorithms to classify the Iris dataset, including k-NN, Decision Tree, ID3, SVM, and neural networks. Each experiment includes the aim, program code, output, and results, demonstrating the effectiveness of each method in classification and prediction tasks. The results show varying degrees of accuracy and successful implementations across the different algorithms.

Uploaded by

19057cme009
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)
3 views3 pages

ML Lab Manual Exp11 To 18

The document outlines a series of experiments using different machine learning algorithms to classify the Iris dataset, including k-NN, Decision Tree, ID3, SVM, and neural networks. Each experiment includes the aim, program code, output, and results, demonstrating the effectiveness of each method in classification and prediction tasks. The results show varying degrees of accuracy and successful implementations across the different algorithms.

Uploaded by

19057cme009
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/ 3

Experiment 11: k-NN Classifier

Aim: Classify Iris dataset using k-NN and print correct and wrong predictions.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)

for a, p in zip(y_test, y_pred):


print("Actual:", iris.target_names[a], " Predicted:", iris.target_names[p],
"-->", "Correct" if a==p else "Wrong")

Output:
Actual: setosa Predicted: setosa --> Correct Actual: versicolor Predicted: virginica --> Wrong ...
Result: k-NN classified data and showed correct and wrong predictions.

Experiment 12: Decision Tree


Aim: Classify Iris dataset using Decision Tree.
Program:
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_text

iris = load_iris()
X, y = iris.data, iris.target
dt = DecisionTreeClassifier(criterion="entropy")
dt.fit(X, y)

print(export_text(dt, feature_names=iris.feature_names))

Output:
|--- petal length (cm) <= 2.45 | |--- class: setosa ...
Result: Decision Tree built and rules displayed.

Experiment 13: ID3 Decision Tree


Aim: Apply ID3 Decision Tree algorithm.
Program:
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_text

iris = load_iris()
X, y = iris.data, iris.target
id3 = DecisionTreeClassifier(criterion="entropy")
id3.fit(X, y)

print(export_text(id3, feature_names=iris.feature_names))

Output:
Decision tree rules printed.
Result: ID3 applied successfully.
Experiment 14: K-Means Clustering
Aim: Cluster Iris dataset using K-Means.
Program:
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans

iris = load_iris()
X = iris.data
kmeans = KMeans(n_clusters=3, random_state=1)
labels = kmeans.fit_predict(X)

print("Cluster Centers:\n", kmeans.cluster_centers_)


print("First 10 labels:", labels[:10])

Output:
Cluster Centers: [[...]] First 10 labels: [1 1 1 0 2 ...]
Result: Data grouped into 3 clusters.

Experiment 15: Support Vector Machine (SVM)


Aim: Classify Iris dataset using SVM.
Program:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

svm = SVC(kernel='linear')
svm.fit(X_train, y_train)

print("Accuracy:", svm.score(X_test, y_test))

Output:
Accuracy: 0.96
Result: SVM classified the Iris dataset with good accuracy.

Experiment 16: Simple Linear Regression


Aim: Predict values using Linear Regression.
Program:
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[1], [2], [3], [4], [5]])


y = np.array([2, 4, 6, 8, 10]) # y = 2x

model = LinearRegression()
model.fit(X, y)

print("Prediction for x=6:", model.predict([[6]])[0])

Output:
Prediction for x=6: 12.0
Result: Linear Regression predicted values correctly.

Experiment 17: Single Layer Neural Network


Aim: Implement a Single Layer Neural Network (Perceptron).
Program:
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
from sklearn.model_selection import train_test_split

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

clf = Perceptron()
clf.fit(X_train, y_train)

print("Accuracy:", clf.score(X_test, y_test))

Output:
Accuracy: 0.93
Result: Single layer perceptron classified the Iris dataset.

Experiment 18: Multi-Layer Neural Network


Aim: Implement a Multi-Layer Perceptron (MLP).
Program:
from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

mlp = MLPClassifier(hidden_layer_sizes=(5,5), max_iter=1000)


mlp.fit(X_train, y_train)

print("Accuracy:", mlp.score(X_test, y_test))

Output:
Accuracy: 0.95
Result: Multi-layer neural network classified the Iris dataset successfully.

You might also like