pratham ML

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Name :- Prathamesh B Bothe

RollNo :- B-235 In-Charge:-Bisweswar Thakur

Practical No:01
Aim : On the fruit dataset, compare the performance of Logistic Regression, SVM, KNN on the
basis of
their accuracy.

Code : import numpy as np import


pandas as pd
import matplotlib.pyplot as plt from sklearn.metrics import classification_report from
sklearn.model_selection import train_test_split from sklearn.preprocessing
import MinMaxScaler from sklearn.linear_model import LogisticRegression from sklearn.tree
import DecisionTreeClassifier from sklearn.neighbors
import KNeighborsClassifier from sklearn.discriminant_analysis import
LinearDiscriminantAnalysis from sklearn.svm import SVC import os
os.chdir("C:\\Users\PramodJ\Desktop\Python") fruits = pd.read_table('fruit.txt') print(fruits.head())
print(fruits.tail()) print (fruits.describe()) feature_names=['mass','width','height','color_score'] X
= fruits[feature_names] Y = fruits['fruit_label'] X_train,
X_test,y_train,y_test=train_test_split(X,Y,random_state=0)
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train) X_test= scaler.transform(X_test) knn =
KNeighborsClassifier() knn.fit(X_train, y_train) print('Accuracy of K-NN classifier on
training set:{:.2f}'.format(knn.score(X_train,y_train))) print('Accuracy of K-NN classifier
on test set:{:.2f}'.format(knn.score(X_test,y_test))) svm = SVC() svm.fit(X_train,y_train)
print('Accuracy of SVM classifier on trainning
set:{:.2f}'.format(svm.score(X_train,y_train))) print('Accuracy of SVM classifier on test
set:{:.2f}'.format(svm.score(X_test,y_test)))

# Logistic Regression
from sklearn.linear_model import LogisticRegression model = LogisticRegression()
model.fit(X_train, y_train) predictions = model.predict(X_test)
print('Accuracy of Logistic Regression on training set: {:.2f}'.format(model.score(X_train,
y_train))) print('Accuracy of Logistic Regressionr on test set: {:.2f}'.format(model.score(X_test,
y_test))) classification_report(y_test, predictions)

Output :
Prathamesh B Bothe
:- B-235 In-Charge:-Bisweswar Thakur
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Logistic Regression :
Prathamesh B Bothe
- B-235 In-Charge:-Bisweswar Thakur

Practical No:02

Aim : On the iris dataset, perform KNN algorithm and discuss result

Code :
import pandas as pd
from sklearn.datasets import load_iris iris=load_iris()
iris.feature_names iris.target_names
df= pd.DataFrame(iris.data,columns=iris.feature_names) df.head()
df['target']=iris.target df.head() df[df.target==1].head() df.shape
df['flower_name'] =df.target.apply(lambda x: iris.target_names[x] )
df.head() df0 =df[:50] df1=df[50:100] df2=df[100:]
from sklearn.model_selection import train_test_split
x=df.drop(['target','flower_name'],axis='columns') y=df.target
x_train,x_test,y_train,y_test =train_test_split(x,y,test_size=0.2,random_state=1) len(x_train)
from sklearn.neighbors import KNeighborsClassifier # create knn classifier knn
=KNeighborsClassifier(n_neighbors=10) knn.fit(x_train, y_train)
knn.score(x_test,y_test) from sklearn.metrics import confusion_matrix
y_pred=knn.predict(x_test) cm
= confusion_matrix(y_test,y_pred) cm
%matplotlib inline import matplotlib.pyplot as plt import seaborn as sn
plt.figure(figsize=(7,5)) sn.heatmap(cm, annot=True) plt.xlabel('predicted')
plt.ylabel('True') from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred) Output : feature names :
Target names :

len(x_train) :
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

len(x_test) :

Knn score :

Confusion matrix:

Heatmap:

Classification report :
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Practical No:03

Aim : Implement apriori algorithm on Online retail dataset and discuss results.

Code : import pandas as pd import numpy as np import seaborn as sns import


matplotlib as mlp import matplotlib.pyplot as plt from mlxtend.frequent_patterns
import apriori
from mlxtend.frequent_patterns import association_rules import os
os.getcwd() os.chdir("C:\\Users\\PramodJ\\Desktop ")
transaction_df=pd.read_csv('OnlineRetail.csv') transaction_df.head()
transaction_df = transaction_df[transaction_df.Country=='France'] transaction_filtered =
transaction_df[['InvoiceNo','Description','Quantity']].copy() transaction_filtered
transaction_filtered
= transaction_filtered[transaction_filtered.Quantity > 0 ]
transaction_filtered.sort_values(by='Quantity', ascending=True) transaction_filtered['Quantity']=
[1]*len(transaction_filtered)
[2]*5
invoice = list(transaction_filtered.InvoiceNo)
index_no = [invoice[index] for index in np.arange(len(invoice)) if not invoice[index].isnumeric()]
transaction_filtered[transaction_filtered['InvoiceNo'].isin(index_no)]
temp_df = transaction_filtered[transaction_filtered.Description != transaction_filtered.Description]
temp_df for invoice in list(temp_df.InvoiceNo): if len(transaction_filtered[transaction_filtered.Invoice
== invoice]) > 1: print((str)(invoice)) temp = transaction_filtered[transaction_filtered.Invoice ==
invoice].groupby(['Invoice']).agg({'Description':lambda x: list(x)}) if len(list(set(temp)))>0 :
print(temp) transaction_filtered.dropna(axis=0, inplace=True) transaction_filtered def return_one(x):
return 1 table = pd.pivot_table(transaction_filtered, values='Quantity', index=['InvoiceNo'],
columns=['Description'], aggfunc=return_one, fill_value=0) table frequent_itemsets = apriori(table,
min_support=0.01, use_colnames=True) frequent_itemsets rules = association_rules(frequent_itemsets,
metric="lift", min_threshold=1) rules rules.sort_values(by=['support','confidence'], ascending=False)
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Output :Apriori :
Prathamesh B Bothe
B-235 In-Charge:-Bisweswar Thakur

Practical No:04

Aim : Implement Naïve Bayes Classifier and K-Nearest Neighbor Classifier on Data set of your
choice.Test and Compare for Accuracy and Precision.
Code :
KNN
import numpy as np import pandas as pd
import matplotlib.pyplot as plt

from sklearn.metrics import classification_report from sklearn.model_selection import


train_test_split from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model
import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors
import KNeighborsClassifier from sklearn.discriminant_analysis import
LinearDiscriminantAnalysis from sklearn.svm import SVC import os
os.chdir("C:\\Users\PramodJ\Desktop\Python") fruits = pd.read_table('fruit.txt') print(fruits.head())
print(fruits.tail())
print (fruits.describe()) feature_names=['mass','width','height','color_score'] X =
fruits[feature_names] Y = fruits['fruit_label']
X_train, X_test,y_train,y_test=train_test_split(X,Y,random_state=0) scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train) X_test= scaler.transform(X_test)
knn = KNeighborsClassifier() knn.fit(X_train, y_train) print('Accuracy of K-NN classifier on training
set:{:.2f}'.format(knn.score(X_train,y_train))) print('Accuracy of K-NN classifier on test
set:{:.2f}'.format(knn.score(X_test,y_test)))
naïve bayes import
numpy as np
import matplotlib.pyplot as plt import pandas as pd
os.chdir("C:\\Users\\PramodJ\\Desktop\\Python") dataset = pd.read_csv('iris.csv') X
= dataset.iloc[:,:4].values y = dataset['variety'].values dataset.head(5)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2) from sklearn.preprocessing
import StandardScaler sc = StandardScaler()
X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) from
sklearn.naive_bayes import GaussianNB classifier = GaussianNB()
classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) y_pred from
sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test,
y_pred) from sklearn.metrics import accuracy_score print ("Accuracy : ",
accuracy_score(y_test, y_pred)) c
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Output : KNN

naïve bayes :

Confusion matrix :
Prathamesh B Bothe
B-235 In-Charge:-Bisweswar Thakur

Practical No:05

Aim : Implement K-Means Clustering on the proper data set of your choice.

Code :

from sklearn.cluster import KMeans import pandas as pd


import matplotlib.pyplot as plt from matplotlib import style style.use("ggplot")
%matplotlib inline data =
pd.DataFrame([[1, 2], [5, 8],
[1.5, 1.8],
[8, 8],
[1, 0.6],
[9, 11]], columns=['x','y']) print( data ) kmeans = KMeans(n_clusters=2).fit(data) centroids =
kmeans.cluster_centers_ labels = kmeans.labels_ print(centroids) print(labels) Output :

Centroids :
[[1.16666667 1.46666667 1. ]
[7.33333333 9. 0. ]]

Labels : [1 0 1 0 1 0]
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Practical No:06

Aim : Design and implement SVM for classification with the proper data set of your choice.
Comment on Design and Implementation for Linearly non separable Dataset.
Code :
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_circles from mpl_toolkits.mplot3d import Axes3D
X, Y = make_circles(n_samples = 500, noise = 0.02)

plt.scatter(X[:, 0], X[:, 1], c = Y, marker = '.') plt.show() X1


= X[:, 0].reshape((-1, 1))
X2 = X[:, 1].reshape((-1, 1)) X3 = (X1**2 + X2**2)
X = np.hstack((X, X3))

# visualizing data in higher dimension fig = plt.figure()


axes = fig.add_subplot(111, projection = '3d') axes.scatter(X1, X2, X1**2 + X2**2, c = Y, depthshade
= True) plt.show()
from sklearn import svm

svc = svm.SVC(kernel = 'linear') svc.fit(X, Y) w


= svc.coef_ b =
svc.intercept_

x1 = X[:, 0].reshape((-1, 1))


x2 = X[:, 1].reshape((-1, 1))
x1, x2 = np.meshgrid(x1,
x2)
x3 = -(w[0][0]*x1 + w[0][1]*x2 + b) / w[0][2]

fig = plt.figure()
axes2 = fig.add_subplot(111, projection = '3d') axes2.scatter(X1, X2, X1**2 + X2**2, c = Y,
depthshade = True) axes1 = fig.gca(projection = '3d') axes1.plot_surface(x1, x2, x3, alpha = 0.01)
plt.show() Output:
Prathamesh B Bothe
B-235 In-Charge:-Bisweswar Thakur
Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

support vector classifier using a linear kernel


Name :- Prathamesh B Bothe
RollNo :- B-235 In-Charge:-Bisweswar Thakur

Practical No:07

Aim : Implement a basic not gate using perceptron.

Code : # importing Python library import numpy as np

# define Unit Step


Function def unitStep(v):
if v >= 0: return 1 else:
return 0

# design Perceptron Model def perceptronModel(x, w, b):


v = np.dot(w, x) + b y = unitStep(v) return y

# NOT Logic
Function # w = -1, b = 0.5 def
NOT_logicFuncti on(x): w = -1 b = 0.5 return perceptronModel(x, w, b)

# testing the Perceptron Model test1 = np.array(1) test2 =


np.array(0)

print("NOT({}) = {}".format(1, NOT_logicFunction(test1))) print("NOT({}) =


{}".format(0, NOT_logicFunction(test2))) Output :
NOT(1) =

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy