MLT Lab Manual
MLT Lab Manual
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
1. Study of python basic libraries such as statistics, math, numpy and scipy.
Aim:
To study of python basic libraries such as statistics, math, numpy and scipy.
Program :
l=[1,2,34,5,5]
print(min(l))
print(max(l))
t=(2,3,4,5,6)
print(max(t))
print(min(t))
u=-2
print(abs(u))
print(pow(3,2))
import math
print(math.sqrt(100))
print(sqrt(100))
print(math.floor(22.3))
print(math.ceil(22.3))
print(math.trunc(33.7))
print(math.cos(0))
print(math.sin(1))
print(math.pi)
print(math.factorial(3))
print(math.log(10))
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
print(math.log10(10))
print(math.log2(6))
print(math.log(2,3))
import numpy
x=(numpy.array([[[[1,2,3],[2,3,4],[2,5,6],[3,5,6]]]]))
print(x.ndim)
list=[2,3,4,5]
print(list[1:3])
print(list[:])
print(list[:3])
print(list[1:])
print(list[1:2:4])
print(list[1::4])
print(list[1:4:-2])
o=numpy.array([[1,2,3],[2,3,4]])
print(o[0,1:2])
print(o[1,2::2])
print(o[0:2,1:3])
#list is mutable
list=[2,7,6,4,9]
#tuple is immutable
T=(2,4,5,6,8)
print(abs(-3))
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
import math
print(math.ceil(5.6))
print(math.floor(5.6))
print(math.pi)
print(math.cos(0))
print(math.factorial(4))
print(math.log2(6))
print(math.log10(6))
print(math.log(6))
print(math.log(6,2))
import numpy
print(numpy.array([[2,3,4,5],[3,5,6,7],[5,6,7,8],[4,6,7,8]]))
x=numpy.array([[[[2,3,4,5],[3,5,6,7],[5,6,7,8],[4,6,7,8],[3,7,8,9]]]])
print(x.ndim)
list=[2,7,6,4]
print(list[:3])
print(list[:])
print(list[2:])
list=[6,7,8,9,1,2,3,4,5]
print(list[1:-4:2])
o=numpy.array([[1,2,3,6,7,8],[3,4,5,6,7,8]])
print(o[0,1:5])
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
print(o[1,2::3])
print(o[0:2,1:3])
Result:
Thus the study of python basic libraries such as statistics, math, numpy and scipy was
studied.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Program:
import pandas as pd
import os
os.chdir("Z:\\")
dataset=pd.read_csv("salary_data.csv")
#print(dataset)
#dataset=pd.read_csv("diabetes.csv",index_col=0)
s1=dataset.copy(deep=False)
print("Shollow Copy")
print(s1)
s2=dataset.copy(deep=True)
print("Deep Copy")
print(s2)
print(s2.size)#size of df rows*columns
print(s2.ndim)#no. of dimensions
#print(s2["Glucose"])
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
##plotting tools
import numpy as np
x=df1['SepalWidthCm']
y=df1['PetalLengthCm']
a=df1['PetalWidthCm']
plt.plot(x,y)
plt.plot(x,a)
##Bar plot
import numpy as np
counts=[50,50,49]
Species=('Iris-viriginica','Iris-versicolor','Iris-setosa')
index=np.arange(len(Species))
plt.bar(index,counts,color=['green','blue','cyan'])
plt.xlabel('Species')
plt.ylabel('Frequency')
plt.show()
##Scatter plot
plt.scatter(df1['SepalWidthCm'],df1['PetalWidthCm'],c='Red')
plt.xlabel('Sepalwidth')
plt.ylabel('Petalwidth')
plt.show()
##Histogram
plt.hist(df2['SepalWidthCm'])
plt.hist(df2['SepalWidthCm'],color='green',edgecolor='orange',bins=5)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
plt.title('histogram of sepalwidth')
plt.xlabel('Sepalwidth')
plt.ylabel('Frequency')
plt.show()
Result:
Thus the study of python basic libraries for ML applications such as pandas,matplot
was studied.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
3. Demonstrate various data pre-processing steps like data cleaning, data integration,
data transformation, data reduction.
Aim:
To demonstrate various data pre-processing steps like data cleaning, data integration,
data transformation, data reduction using machine learning.
Algorithm:
1.Start
4.Find out the null values present and fill the NA values using Forward and Backward
filling.
8.Stop.
Program:
##IRIS DATASET
import pandas as pd
import os
os.chdir("F:\\")
df=pd.read_csv("Iris_data_sample.csv")
print(df)
print(df['SepalLengthCm'].isnull())
print(df['SepalLengthCm'].notnull())
df1=pd.read_csv("Iris_data_sample.csv",index_col=0,na_values=["??","???","
","###"])
print(df1)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
na=df1.isna().sum()
print(na)
missing=df1[df1.isnull().any(axis=1)]
print(missing)
j=df1.describe()
print(j)
df2=df1.fillna(method='pad')
print(df2)
df2=df1.fillna(method='bfill')
print(df2)
print(df1)
g=df1.dropna()
print(g)
h=g.shape
print(h)
i=df1.size
print(i)
l=df1.replace({5.10000:5})
print(l)
##TOYOTA dataset
import pandas as pd
import numpy as np
import os
os.chdir("F:\\")
df=pd.read_csv("Toyota.csv")
print(df)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
cars_data=pd.read_csv("toyota.csv")
s1=cars_data.copy(deep=True)
print(s1)
print(df['Price'].isnull())
print(df['Age'].notnull())
df1=pd.read_csv("Toyota.csv",index_col=0,na_values=["??"])
print(df1)
a=df1.isna().sum()
print(a)
missing=df[df.isnull().any(axis=1)]
print(missing)
j=df.describe()
print(j)
df2=df1.fillna(method='pad')
print(df2)
df2=df1.fillna(method='bfill')
print(df2)
g=df1.dropna()
print(g)
h=g.shape
print(h)
t=df1.shape
print(t)
i=df1.size
print(i)
l=df1.replace({46986.0:5})
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
print(l)
count=df1['FuelType'].value_counts()
print(count)
k=np.unique(df1.loc[:,"Age"])
print(k)
col=["KM","FuelType","HP","MetColor","Automatic","CC","Doors","Weight"]
reference=cars_data.drop(columns=col,axis=1)
print(reference)
Output:
Result:
Thus the various data pre-processing steps like data cleaning, data integration, data
transformation, data reduction using machine learning has been demonstrated successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
4.Do the data pre-processing steps by filling the NA values by using pad method and
remove the insignificant variables.
7.Using LinearRegression(),predict the values and compute the RMSE values and R
squared values.
8.Stop
Program:
import numpy as np
import os
import pandas as pd
os.chdir("z://datasets")
df=pd.read_csv("Toyota.csv",index_col=0,na_values=["??"])
print(df)
df1=df.fillna(method='pad')
sns.regplot(x="Age",y="Price",scatter=True,fit_reg=False,data=dataset)#regreesion is
not fit
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
sns.regplot(x="Age",y="Price",scatter=True,fit_reg=True,data=dataset)#regression is
fitted
col=["Age","FuelType","HP","MetColor","Automatic","CC","Doors","Weight"]
reference=df1.drop(columns=col,axis=1)
print(reference)
x=reference.drop(["Price"],axis="columns",inplace=False)
y=reference["Price"]
print(x)
print(y)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
reg=LinearRegression()
reg=reg.fit(x_train,y_train)
prediction_lin1=reg.predict(x_test)
print(prediction_lin1)
lin_mse1=mean_squared_error(y_test,prediction_lin1)
print(np.log(lin_mse1))
r2_lin_test1=reg.score(x_test,y_test)
r2_lin_train1=reg.score(x_train,y_train)
print(r2_lin_test1,r2_lin_train1)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Output:
Result:
Thus the program for simple linear regression using machine learning has been
executed successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
5.Give the training dataset as an input and output to train the model.
6.After the training the model using training data,use the test dataset and pass the test
dataset to the model built.
8.Stop
Program:
import os
import pandas as pd
import numpy as np
os.chdir("F:\\")
dataset=pd.read_csv("house_price-train.csv")
dataset
df=pd.read_csv("house_price-
train.csv",index_col=0,na_values=["??","???","","????","###"])
df1=df.fillna(method='pad')
df1
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
col=["RERA","BHK_OR_RK","READY_TO_MOVE","RESALE","ADDRESS","L
ONGITUDE","LATITUDE","TARGET(PRICE_IN_LACS)"]
home=df1.drop(columns=col,axis=1)
home1=home.copy()
home1
df1.shape
sns.regplot(x="BHK_NO.",y="SQUARE_FT",scatter=True,fit_reg=False,data=df1)
sns.regplot(x="BHK_NO.",y="SQUARE_FT",scatter=True,fit_reg=True,data=df1)
sum(df1["SQUARE_FT"]>2500)
sns.distplot(df1["SQUARE_FT"])
x1=home.drop(["BHK_NO."],axis="columns",inplace=False)
y1=home1["BHK_NO."]
x_train,x_test,y_train,y_test=train_test_split(x1,y1,test_size=1/3,random_state=0)
reg=LinearRegression()
reg=reg.fit(x_train,y_train)
prediction_lin1=reg.predict(x_test)
lin_mse1=mean_squared_error(y_test,prediction_lin1)
print(np.log(lin_mse1))r2_lin_test1=reg.score(x_test,y_test)
r2_lin_train1=reg.score(x_train,y_train)
print(r2_lin_test1,r2_lin_train1)
Output:
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Result:
Thus the program for multiple linear regressions using machine learning has been
executed successfully.
Aim:
Algorithm:
1.Start
2.Import the required packages and libraries, change the working directory paths.
3.Import the dataset by using read.csv command and do the data pre-processing by
using the padding method.
4.Take only the essential features as input by dropping the unnecessary features.
i. Fit the model by training,using lasso regression method with training dataset.
8.Stop
Program:
import numpy as np
import os
import pandas as pd
os.chdir("F:/")
df=pd.read_csv("house_price-train.csv",index_col=0,na_values=["??","????"])
df1=df.fillna(method='pad')
df1
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
col=["UNDER_CONSTRUCTION","RERA","BHK_OR_RK","READY_TO_MOV
E","ADDRESS"]
reference=df1.drop(columns=col,axis=1)
print(reference)
x=reference.drop(["SQUARE_FT"],axis="columns",inplace=False)
y=reference["SQUARE_FT"]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
lasso=Lasso()
lasso.fit(x_train,y_train)
y_pred_lasso=lasso.predict(x_test)
mse=mean_squared_error(y_test,y_pred_lasso)
print(np.log(mse))
Output:
Result:
Thus the program for lasso regression(mse) using machine learning has been executed
successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
2.Import the required packages and libraries,change the working directory paths.
3.Import the dataset by using read.csv command and do the data pre-processing by
using the padding method.
4.Take only the essential features as input by dropping the unnecessary features.
i. Fit the model by training with training dataset using Ridge Regression
iii. Calculate the score of the model for train and test data
8.Stop
Program:
import numpy as np
import os
import pandas as pd
os.chdir("F:/")
df=pd.read_csv("house_price-train.csv",index_col=0,na_values=["??","????"])
df1=df.fillna(method='pad')
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
df1
col=["UNDER_CONSTRUCTION","RERA","BHK_OR_RK","READY_TO_MOVE","AD
DRESS"]
reference=df1.drop(columns=col,axis=1)
print(reference)
x=reference.drop(["SQUARE_FT"],axis="columns",inplace=False)
y=reference["SQUARE_FT"]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
ridge=Ridge()
ridge.fit(x_train,y_train)
y_pred_ridge=ridge.predict(x_test)
mse=mean_squared_error(y_test,y_pred_ridge)
print(np.log(mse))
Output:
Result:
Thus the program for ridge regression (mse) using machine learning has been
executed successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
2.Import the required packages and libraries,change the working directory paths.
3.Import the dataset by using read.csv command and do the data pre-processing by
using the padding method.
4.Take only the essential features as input by dropping the unnecessary features.
i. Fit the model by training with training dataset using Lasso Regression
8.Stop.
Program:
import numpy as np
import os
import pandas as pd
os.chdir("F:/")
df=pd.read_csv("house_price-train.csv",index_col=0,na_values=["??","????"])
df1=df.fillna(method='pad')
df1
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
col=["UNDER_CONSTRUCTION","RERA","BHK_OR_RK","READY_TO_MOV
E","ADDRESS"]
reference=df1.drop(columns=col,axis=1)
print(reference)
x=reference.drop(["SQUARE_FT"],axis="columns",inplace=False)
y=reference["SQUARE_FT"]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
lasso=Lasso()
lasso.fit(x_train,y_train)
train_score=lasso.score(x_train,y_train)
test_score=lasso.score(x_test,y_test)
Output:
Result:
Thus the program for lasso regression (score) using machine learning has been
executed successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
2.Import the required packages and libraries,change the working directory paths.
3.Import the dataset by using read.csv command and do the data pre-processing by
using the padding method.
4.Take only the essential features as input by dropping the unnecessary features.
i. Fit the model by training with training dataset using Ridge Regression
8.Stop.
Program:
import numpy as np
import os
import pandas as pd
os.chdir("F:/")
df=pd.read_csv("house_price-train.csv",index_col=0,na_values=["??","????"])
df1=df.fillna(method='pad')
df1
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
col=["UNDER_CONSTRUCTION","RERA","BHK_OR_RK","READY_TO_MOV
E","ADDRESS"]
reference=df1.drop(columns=col,axis=1)
print(reference)
x=reference.drop(["SQUARE_FT"],axis="columns",inplace=False)
y=reference["SQUARE_FT"]
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=1/3,random_state=0)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
ridge=Ridge()
ridge.fit(x_train,y_train)
train_score=ridge.score(x_train,y_train)
test_score=ridge.score(x_test,y_test)
Output:
Result:
Thus the program for ridge regression (score) using machine learning has been
executed successfully.
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
7. Logistic Regression
Aim:
Algorithm:
1.Start.
4.Build the model by using the LogisticRegression() and fit it,predict the output.
5.Calculate all the evaluation metrics for the fitted model and visualize it.
7.Stop.
Program:
import os
import pandas as pd
os.chdir("D:/")
pima=pd.read_csv("diabetes.csv")
pima
s=pima.copy(deep=True)
x=s.loc[:,['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','Diab
etesPedigreeFunction','Age']]
y=s.loc[:,'Outcome']
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
x_train,x_test,y_train,y_test
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
logreg=LogisticRegression()
logreg=logreg.fit(x_train,y_train)
y_pred=logreg.predict(x_test)
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
cnf_matrix=metrics.confusion_matrix(y_test,y_pred)
cnf_matrix
print("Precision:",metrics.precision_score(y_test,y_pred))
print("Recall:",metrics.recall_score(y_test,y_pred))
a=s["Glucose"]
b=s["Outcome"]
sns.regplot(x=a,y=b,data=s,logistic=True)
tp=132
tn=47
fp=14
fn=38
accuracy=(tp+tn)/(tp+tn+fp+fn)
accuracy
precision=(tp)/(tp+fp)
precision
recall=(tp)/(tp+fn)
recall
##Boxplot
plt.figure(figsize=(15,10))
plt.subplot(2,2,1)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
fig=x_train.boxplot(column='Age')
fig.set_title('')
fig.set_ylabel('Age')
##heatmap
cor=x_train.corr()
plt.show()
##Barplot
import numpy as np
y=(accuracy,precision,recall,sensitivity,specificity,f_measure)
metrics=('accuracy','precision','recall','sensitivity','specificity','f_measure')
index=np.arange(len(metrics))
plt.bar(index,y,color=['red','blue','yellow','black','lavender','cyan'])
plt.xlabel('metrics')
plt.ylabel('frequency')
plt.show()
Output:
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Result:
Thus the program for logistic regression using machine learning has been executed
successfully.
8. Decision Tree
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
3.Read and import the dataset,split the dataset into training and testing data.
5.Print the evaluation metrics of the model,confusion matrix and plot it.
6.Stop.
Program:
import os
import pandas as pd
os.chdir("D:/")
pima=pd.read_csv("diabetes.csv")
s=pima.copy(deep=True)
x=s.loc[:,['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','Diab
etesPedigreeFunction','Age']]
y=s.loc[:,'Outcome']
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
x_train,x_test,y_train,y_test
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
clf=DecisionTreeClassifier()
clf=clf.fit(x_train,y_train)
clf
y_pred=clf.predict(x_test)
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
cnf_matrix=metrics.confusion_matrix(y_test,y_pred)
cnf_matrix
##entropy
clf=DecisionTreeClassifier(criterion="entropy",max_depth=3)
clf=clf.fit(x_train,y_train)
clf
y_pred=clf.predict(x_test)
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
cnf_matrix=metrics.confusion_matrix(y_test,y_pred)
cnf_matrix
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Output:
Result:
Thus the program for decision tree using machine learning has been executed
successfully.
Aim:
Algorithm:
1.Start
3.Change the working directory path of the file and import the dataset.
4.Build the model using the GaussianNB and predict the output of model.
6.Stop.
Program:
import os
import pandas as pd
os.chdir("D:/")
pima=pd.read_csv("diabetes.csv")
s=pima.copy(deep=True)
model=GaussianNB()
x=s.loc[:,['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','Diab
etesPedigreeFunction','Age']]
y=s.loc[:,'Outcome']
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
model=model.fit(x_train,y_train)
y_pred=model.predict(x_test)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
Output:
Result:
Thus the program for navie bayes using machine learning has been executed
successfully.
10. KNN
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
3.Change the working directory of the file path and import the dataset.
4.Build the model using K-NeighborsClassifier and predict the output of model.
6.Stop.
Program:
import os
import pandas as pd
os.chdir("D:/")
pima=pd.read_csv("diabetes.csv")
s=pima.copy(deep=True)
model=KNeighborsClassifier(n_neighbors=3)
x=s.loc[:,['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','Diab
etesPedigreeFunction','Age']]
y=s.loc[:,'Outcome']
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
model=model.fit(x_train,y_train)
y_pred=model.predict(x_test)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
Output:
Result:
Thus the program for KNN using machine learning has been executed successfully.
11.SVM
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
4.Split the dataset into testing and training data for training the model after data pre-
processing.
7.Stop.
Program:
#kernel=linear
import os
import pandas as pd
os.chdir("D:/")
pima=pd.read_csv("diabetes.csv")
s=pima.copy(deep=True)
x=s.loc[:,['Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','Diab
etesPedigreeFunction','Age']]
y=s.loc[:,'Outcome']
x
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
x_train,x_test,y_train,y_test
clf=svm.SVC(kernel='linear')
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
clf.fit(x_train,y_train)
y_pred=model.predict(x_test)
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
#kernel=rbf
clf=svm.SVC(kernel='rbf')
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1)
clf.fit(x_train,y_train)
y_pred=model.predict(x_test)
print("Accuracy:",metrics.accuracy_score(y_test,y_pred))
print("precision:",metrics.precision_score(y_test,y_pred))
print("recall:",metrics.recall_score(y_test,y_pred))
Output:
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Result:
Thus the program for SVM using machine learning has been executed successfully.
12.K-Means clustering
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Aim:
Algorithm:
1.Start
3.After data pre-processing, split the dataset into training and testing data.
4.Build the model using training data by K-Means Classifier and Silhoutte_score.
6.Stop.
Program:
import os
import pandas as pd
import numpy as np
os.chdir(“Z://datasets”)
pd1=pd.read_csv(“diabetes.csv”)
x=np.array(pd1.drop([‘Outcome’],1).astype(float))
kmeans=KMeans(n_clusters=2)
y=kmeans.fit(x)
data=kmeans.predict(x)
ss=Silhoutte_score(x,kmeans.labels_)
print(ss)
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning
Output:
Result:
Thus the program for K-Means clustering using machine learning has been executed
successfully.