In [2]:
# Implement the Decision Tree algorithm on playtennis dataset.
#Compare the results of using Gini Index and Entropy criterion for min_samp
#values of 20 and 30. Draw a color coded decision tree. Finally, demonstrat
#method using a new instance (The output should be the presence of diabetes
import numpy as np
import pandas as pd
from [Link] import LabelEncoder
from sklearn import tree
import [Link] as plt
# Load the dataset
PlayTennis = pd.read_csv("[Link]")
# Encode categorical variables
Le = LabelEncoder()
PlayTennis['Outlook'] = Le.fit_transform(PlayTennis['Outlook'])
PlayTennis['Temperature'] = Le.fit_transform(PlayTennis['Temperature'])
PlayTennis['Humidity'] = Le.fit_transform(PlayTennis['Humidity'])
PlayTennis['Wind'] = Le.fit_transform(PlayTennis['Wind'])
PlayTennis['Play Tennis'] = Le.fit_transform(PlayTennis['Play Tennis'])
# Split the data into features (X) and target (y)
y = PlayTennis['Play Tennis']
X = [Link](['Play Tennis'], axis=1)
# Train a decision tree classifier
clf_gini = [Link](criterion='gini')
clf_gini = clf_gini.fit(X, y)
clf_entropy = [Link](criterion='entropy')
clf_entropy = clf_entropy.fit(X, y)
# Plot the decision tree
[Link](figsize=(10, 8))
[Link](1, 2, 1)
tree.plot_tree(clf_gini, filled=True)
[Link]("Decision Tree using Gini Index")
[Link](1, 2, 2)
tree.plot_tree(clf_entropy, filled=True)
[Link]("Decision Tree using Entropy")
[Link]()
# Define new instance values
new_instances = [Link]({
'Outlook': [2, 1, 0], # e.g., Sunny, Cloudy, Rainy
'Temperature': [1, 2, 0], # e.g., Hot, Mild, Cool
'Humidity': [1, 0, 1], # e.g., High, Normal, High
'Wind': [0, 1, 0] # e.g., Weak, Strong, Weak
})
# Predict the class labels for the new instances
y_pred_gini = clf_gini.predict(new_instances)
y_pred_entropy = clf_entropy.predict(new_instances)
# Convert the predicted class labels back to 'Yes' or 'No'
y_pred_gini_labels = [Link](y_pred_gini == 1, 'Yes', 'No')
y_pred_entropy_labels = [Link](y_pred_entropy == 1, 'Yes', 'No')
# Print the predicted class labels along with the other outputs
print("Predictions using Gini Index:")
print([Link]({
'Outlook': new_instances['Outlook'].map({0: 'Rainy', 1: 'Cloudy', 2: 'S
'Temperature': new_instances['Temperature'].map({0: 'Cool', 1: 'Mild',
'Humidity': new_instances['Humidity'].map({0: 'Normal', 1: 'High'}),
'Wind': new_instances['Wind'].map({0: 'Weak', 1: 'Strong'}),
'Play Tennis': y_pred_gini_labels
}))
print("\nPredictions using Entropy:")
print([Link]({
'Outlook': new_instances['Outlook'].map({0: 'Rainy', 1: 'Cloudy', 2: 'S
'Temperature': new_instances['Temperature'].map({0: 'Cool', 1: 'Mild',
'Humidity': new_instances['Humidity'].map({0: 'Normal', 1: 'High'}),
'Wind': new_instances['Wind'].map({0: 'Weak', 1: 'Strong'}),
'Play Tennis': y_pred_entropy_labels
}))
Predictions using Gini Index:
Outlook Temperature Humidity Wind Play Tennis
0 Sunny Mild High Weak No
1 Cloudy Hot Normal Strong Yes
2 Rainy Cool High Weak Yes
Predictions using Entropy:
Outlook Temperature Humidity Wind Play Tennis
0 Sunny Mild High Weak No
1 Cloudy Hot Normal Strong Yes
2 Rainy Cool High Weak Yes
In [3]:
#. Implement a neural network for handwritten digits dataset. Determine acc
import numpy as np
from [Link] import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from [Link] import accuracy_score
dataset=load_digits()
x_train,x_test,y_train,y_test=train_test_split([Link],[Link],
NN=MLPClassifier()
[Link](x_train,y_train)
y_pred=[Link](x_test)
accuracy=accuracy_score(y_test,y_pred)*100
print("accuracy for neural network network is:",accuracy)
accuracy for neural network network is: 98.05555555555556
In [4]: #. Implement the K-Means Clustering algorithm on the IRIS dataset. Use Peta
#Finally, display the cluster for a new instance
#(You must accept the values of the Petal Length and Petal Width from the u
import numpy as np
import [Link] as plt
from [Link] import load_iris
from [Link] import KMeans
# Load the IRIS dataset
iris = load_iris()
X = [Link][:, [2, 3]] # Petal Length and Petal Width
# Apply K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=42)
[Link](X)
# Display cluster labels and cluster centers
print("Cluster Labels:\n", kmeans.labels_)
print("\nCluster Centers:\n", kmeans.cluster_centers_)
# Visualize the clusters
[Link](figsize=(8, 6))
[Link](X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis', edgecolor='
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], m
[Link]('Petal Length')
[Link]('Petal Width')
[Link]('K-Means Clustering on IRIS Dataset')
[Link]()
[Link](True)
[Link]()
# Predict the cluster for a new instance
new_petal_length = float(input("Enter Petal Length of the new instance: "))
new_petal_width = float(input("Enter Petal Width of the new instance: "))
new_instance = [Link]([[new_petal_length, new_petal_width]])
predicted_cluster = [Link](new_instance)
print(f"\nPredicted Cluster for [{new_petal_length}, {new_petal_width}]: {p
Cluster Labels:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 0 2 2 2 2 2 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0
0 0]
Cluster Centers:
[[5.59583333 2.0375 ]
[1.462 0.246 ]
[4.26923077 1.34230769]]
Enter Petal Length of the new instance: 7
Enter Petal Width of the new instance: 7
Predicted Cluster for [7.0, 7.0]: 0
In [6]:
#. Implement the Naïve Bayes algorithm on the Iris Dataset using the petal
#sepal width and petal width features only. Finally, predict the name of th
#species of a new instance by accepting the values of the above three featu
import numpy as np
from [Link] import load_iris
from sklearn.naive_bayes import GaussianNB
# Load the IRIS dataset
iris = load_iris()
X = [Link][:, [2, 1, 3]] # Petal Length, Sepal Width, Petal Width
y = [Link]
# Train the Naïve Bayes classifier
nb_classifier = GaussianNB()
nb_classifier.fit(X, y)
# Predict the species for a new instance
new_petal_length = float(input("Enter Petal Length of the new instance: "))
new_sepal_width = float(input("Enter Sepal Width of the new instance: "))
new_petal_width = float(input("Enter Petal Width of the new instance: "))
new_instance = [Link]([[new_petal_length, new_sepal_width, new_petal_widt
predicted_species_index = nb_classifier.predict(new_instance)
predicted_species = iris.target_names[predicted_species_index][0]
print(f"\nPredicted Species for [{new_petal_length}, {new_sepal_width}, {ne
Enter Petal Length of the new instance: 7
Enter Sepal Width of the new instance: 7
Enter Petal Width of the new instance: 7
Predicted Species for [7.0, 7.0, 7.0]: virginica
In [7]:
#. Implement the K-NN algorithm on IRIS dataset.
#Write down your observations for n_neighbors=3, 4, 5. Finally, set the n_n
#of elements in the dataset automatically and note down the observations.
import numpy as np
from [Link] import load_iris
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
# Load the IRIS dataset
iris = load_iris()
X = [Link]
y = [Link]
# 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, ra
# Function to train and evaluate K-NN classifier
def evaluate_knn(n_neighbors):
knn = KNeighborsClassifier(n_neighbors=n_neighbors)
[Link](X_train, y_train)
y_pred = [Link](X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'n_neighbors = {n_neighbors}, Accuracy = {accuracy:.2f}')
# Evaluate for different n_neighbors values
evaluate_knn(3)
evaluate_knn(4)
evaluate_knn(5)
# Calculate the square root of the number of elements in the dataset
n_neighbors_sqrt = int([Link](len(X)))
evaluate_knn(n_neighbors_sqrt)
n_neighbors = 3, Accuracy = 1.00
n_neighbors = 4, Accuracy = 1.00
n_neighbors = 5, Accuracy = 1.00
n_neighbors = 12, Accuracy = 1.00
In [9]:
#Implement multiple linear regression on student-mat dataset. Determine G3
#G2, studytime, failures and absences. Determine the accuracy of the model.
import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model
from [Link] import shuffle
data=pd.read_csv('[Link]',sep=";")
print([Link]())
data=data[["G1","G2","G3","studytime","failures","absences"]]
print("\n data after dropping unnessary columns")
print(data)
predict="G3"
X=[Link]([Link]([predict],1))
y=[Link](data[predict])
x_train,x_test,y_train,y_test=sklearn.model_selection.train_test_split(X,y,
linear=linear_model.LinearRegression()
[Link](x_train,y_train)
acc=[Link](x_test,y_test)
accuracy = round(acc * 100, 2)
print("Accuracy:", accuracy, "%")
print('coefficient:\n',linear.coef_)
print('intercept:\n',linear.intercept_)
predictions=[Link](x_test)
for x in range(len(predictions)):
print(predictions[x],x_test[x],y_test[x])
school sex age address famsize Pstatus Medu Fedu Mjob Fjob ... \
0 GP F 18 U GT3 A 4 4 at_home teacher ...
1 GP F 17 U GT3 T 1 1 at_home other ...
2 GP F 15 U LE3 T 1 1 at_home other ...
3 GP F 15 U GT3 T 4 2 health services ...
4 GP F 16 U GT3 T 3 3 other other ...
famrel freetime goout Dalc Walc health absences G1 G2 G3
0 4 3 4 1 1 3 6 5 6 6
1 5 3 3 1 1 3 4 5 5 6
2 4 3 2 2 3 3 10 7 8 10
3 3 2 2 1 1 5 2 15 14 15
4 4 3 2 1 2 5 4 6 10 10
[5 rows x 33 columns]
data after dropping unnessary columns
G1 G2 G3 studytime failures absences
0 5 6 6 2 0 6
1 5 5 6 2 0 4
2 7 8 10 2 3 10
3 15 14 15 3 0 2
4 6 10 10 2 0 4
.. .. .. .. ... ... ...
390 9 9 9 2 2 11
391 14 16 16 1 0 3
392 10 8 7 1 3 3
393 11 12 10 1 0 0
394 8 9 9 1 0 5
[395 rows x 6 columns]
Accuracy: 80.52 %
coefficient:
[ 0.16098531 0.97880975 -0.22831402 -0.22015059 0.03725838]
intercept:
-1.5152257553919792
9.654449733071692 [ 7 10 2 1 25] 11
9.779540705469062 [ 8 10 1 0 12] 10
13.695800980406577 [12 14 3 0 7] 14
14.964086694731781 [14 15 2 0 0] 16
13.55665408008555 [11 12 2 0 54] 11
10.198567608079665 [12 10 2 1 18] 10
7.0873413469981 [9 8 2 1 0] 0
9.327676405836577 [ 8 10 2 0 6] 10
6.832699441148879 [6 8 1 1 0] 8
8.40487620526008 [ 9 9 2 2 15] 9
8.286301685652719 [9 9 2 0 0] 10
8.29348973714762 [10 9 3 0 2] 10
15.199588763225682 [15 15 2 0 2] 16
4.92948150232256 [5 6 2 0 6] 6
13.702424646877825 [13 13 2 0 23] 13
18.61178587007835 [17 18 1 0 0] 18
14.05260565894826 [13 14 1 0 0] 14
7.7039793165962465 [11 8 2 0 2] 8
9.104126123203095 [ 8 10 2 0 0] 12
11.2863171904412 [12 12 4 0 1] 12
8.372381559186685 [9 9 1 2 8] 9
5.859537990437747 [ 8 6 2 0 18] 7
13.037940556200553 [10 13 1 0 12] 12
7.841061052491481 [10 8 2 0 10] 8
7.303703578136682 [8 8 1 1 4] 8
13.367099222927601 [13 13 2 0 14] 14
12.81439027356707 [10 13 1 0 6] 13
11.929237126633769 [12 12 2 0 6] 12
19.165897842296538 [17 18 2 0 21] 18
7.96433107042057 [7 9 2 0 0] 8
11.932636815779079 [12 12 2 1 12] 13
11.780203604878114 [12 12 2 0 2] 11
6.985521325254123 [7 8 2 0 0] 0
15.82439015633466 [17 15 1 0 4] 16
11.794579707867914 [14 12 4 0 6] 13
-1.5398997289030283 [4 0 1 2 0] 0
10.243921175985616 [ 9 11 2 0 0] 12
9.727563471091862 [10 10 2 1 14] 9
8.069375069490484 [10 8 1 0 10] 9
8.199833138914473 [8 9 2 0 2] 10
-1.0059419436935373 [6 0 2 0 0] 0
3.439642171651532 [6 5 3 1 0] 0
16.136376393587696 [15 16 3 0 7] 15
19.44682171474018 [18 18 1 1 24] 18
6.826960334767431 [7 8 4 0 8] 8
4.9414332881829806 [6 6 2 0 2] 6
12.105573909255572 [14 12 2 1 8] 12
10.090123919864443 [ 9 11 3 0 2] 11
C:\Users\Dell\AppData\Local\Temp\ipykernel_14356\[Link]: FutureWarning: In a
future version of pandas all arguments of [Link] except for the argument 'labels'
will be keyword-only.
X=[Link]([Link]([predict],1))
11.036895928205567 [13 11 2 0 4] 11
8.112975918971946 [ 6 9 1 2 14] 8
14.964086694731781 [14 15 2 0 0] 15
6.6976533491787436 [ 7 8 2 3 10] 10
12.904258509450628 [11 13 1 1 10] 13
9.587082046051316 [11 10 2 0 0] 10
9.575130260190896 [10 10 2 0 4] 11
9.500613499313069 [10 10 2 0 2] 11
4.174222039789595 [ 5 5 2 0 12] 5
20.12416472725001 [18 19 1 0 10] 19
13.08098396527671 [14 13 2 0 2] 13
14.141498522815887 [15 14 3 0 6] 14
11.917285340773349 [11 12 2 0 10] 13
15.199588763225682 [15 15 2 0 2] 16
11.290059630534993 [ 8 12 1 0 0] 12
9.25315964495875 [ 8 10 2 0 4] 10
11.551325202855459 [12 11 1 0 16] 11
13.502777935965181 [15 13 2 0 9] 15
11.773015553383214 [11 12 1 0 0] 10
14.444329300692715 [15 14 2 0 8] 14
8.066151092164548 [9 9 2 1 0] 0
6.853699210609925 [ 9 7 2 1 20] 8
11.979850315790756 [10 12 2 0 16] 11
10.726877098833839 [12 11 2 0 0] 12
11.947812578965435 [14 11 1 0 18] 13
11.136719283661888 [12 11 2 0 11] 11
12.290433530186142 [11 13 4 0 6] 14
4.815914664943883 [7 6 1 2 0] 0
10.801393859711666 [12 11 2 0 2] 12
6.001947845722157 [7 7 3 0 6] 7
10.109263757219763 [11 11 4 0 0] 11
In [10]:
#Implement a SVM on the Iris dataset. Determine accuracy of the model. Find
import numpy as np
from [Link] import load_iris
from [Link] import SVC
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
# Load the IRIS dataset
iris = load_iris()
X = [Link]
y = [Link]
# 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, ra
# Train the SVM model
svm_model = SVC(kernel='linear', random_state=42)
svm_model.fit(X_train, y_train)
# Determine the accuracy of the model
y_pred = svm_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy of SVM model: {accuracy:.2f}')
# Predict the species for a new instance
new_instance = [
float(input("Enter Sepal Length of the new instance: ")),
float(input("Enter Sepal Width of the new instance: ")),
float(input("Enter Petal Length of the new instance: ")),
float(input("Enter Petal Width of the new instance: "))
]
new_instance = [Link]([new_instance])
predicted_species_index = svm_model.predict(new_instance)
predicted_species = iris.target_names[predicted_species_index][0]
print(f"\nPredicted Species for the new instance: {predicted_species}")
Accuracy of SVM model: 1.00
Enter Sepal Length of the new instance: 7
Enter Sepal Width of the new instance: 7
Enter Petal Length of the new instance: 7
Enter Petal Width of the new instance: 7
Predicted Species for the new instance: virginica
In [11]:
#. Implement the support vector machine algorithm on the Iris dataset. Visu
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import SVC
import numpy as np
from [Link] import accuracy_score
import [Link] as aaa
iris=datasets.load_iris()
x=[Link]
y=[Link]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_sta
svm=SVC(kernel='linear')
[Link](x_train,y_train)
y_pred=[Link](x_test)
accuracy=accuracy_score(y_test,y_pred)
print("accuracy:",accuracy)
[Link](x_train[:,0],x_train[:,1],c=y_train)
[Link](svm.support_vectors_[:,0],svm.support_vectors_[:,1],color='red'
[Link]('sepal length')
[Link]('sepal width')
[Link]('support vector machine')
[Link]()
accuracy: 1.0
In [12]: #. Implement the Self Organizing Feature map on the Iris dataset.
#What should be the value of n_clusters argument for this dataset? Justify.
import numpy as np
import [Link] as plt
from [Link] import load_iris
from [Link] import MinMaxScaler
from [Link] import PCA
from [Link] import MiniBatchKMeans
iris=load_iris()
x=[Link]
y=[Link]
scaler=MinMaxScaler()
x_scaled=scaler.fit_transform(x)
pca=PCA(n_components=2)
x_pca=pca.fit_transform(x_scaled)
som=MiniBatchKMeans(n_clusters=3)
[Link](x_scaled)
y_pred=som.labels_
[Link](x_pca[:,0],x_pca[:,1],c=y_pred)
[Link]("self organising feature")
[Link]("pc1")
[Link]("pc2")
[Link]()
C:\Users\Dell\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:1043: UserWarning:
MiniBatchKMeans is known to have a memory leak on Windows with MKL, when there are less
chunks than available threads. You can prevent it by setting batch_size >= 2048 or by
setting the environment variable OMP_NUM_THREADS=1
[Link](
In [15]:
# Implement simple linear regression on salary dataset. Determine salary gi
#Draw the graphs for both training and test sets.
import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model
data=pd.read_csv('salary_Data.csv')
[Link]=['yearExperience','salary']
predict="salary"
X=[Link]([Link]([predict],1))
y=[Link](data[predict])
x_train,x_test,y_train,y_test=sklearn.model_selection.train_test_split(X,y,
linear=linear_model.LinearRegression()
[Link](x_train,y_train)
acc=[Link](x_test,y_test)
print(acc)
print('coefficient:\n',linear.coef_)
print('intercept:\n',linear.intercept_)
predictions=[Link](x_test)
for x in range(len(predictions)):
print(predictions[x],x_test[x],y_test[x])
import [Link] as mtp
test_pred=[Link](x_test)
train_pred=[Link](x_train)
[Link](x_test,y_test,color="blue")
[Link](x_test,test_pred,color="red")
[Link]("salary vs experience(training dataset)")
[Link]("years of eperience")
[Link]("salary(in rupees)")
[Link]()
[Link](x_train,y_train,color="green")
[Link](x_train,train_pred,color="red")
[Link]("salary vs experience(training dataset)")
[Link]("years of eperience")
[Link]("salary(in rupees)")
[Link]()
new_instance=[[2.6]]
predicted_salary=[Link](new_instance)
print("the predict_salary is:",predicted_salary)
C:\Users\Dell\AppData\Local\Temp\ipykernel_14356\[Link]: FutureWarning: In a
future version of pandas all arguments of [Link] except for the argument 'labels'
will be keyword-only.
X=[Link]([Link]([predict],1))
0.9568915233172142
coefficient:
[9747.18161565]
intercept:
24513.383367391354
55704.36453748343 [3.2] 54445
55704.36453748343 [3.2] 64445
62527.39166844107 [3.9] 63218
82021.75489974863 [5.9] 81363
112238.0179082753 [9.] 105582
126858.79033175597 [10.5] 121872
90794.218353837 [6.8] 91738
60577.955345310314 [3.7] 57189
63502.10983000645 [4.] 56957
the predict_salary is: [49856.05556809]
In [ ]: