0% found this document useful (0 votes)
38 views41 pages

MLT Lab Manual

Uploaded by

may581232
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)
38 views41 pages

MLT Lab Manual

Uploaded by

may581232
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/ 41

SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.

(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))

from math import sqrt

print(sqrt(100))

from math import floor

print(math.floor(22.3))

from math import ceil

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]

print("min of list is",min(list))

print("max of list is",max(list))

#tuple is immutable

T=(2,4,5,6,8)

print("min of tuple is",min(T))

print("max of tuple is",max(T))

print(abs(-3))
SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning

print("5 to the power of 4 is",pow(5,4))

from math import sqrt

import math

print("square root of 256 is",sqrt(256))

print(math.ceil(5.6))

print(math.floor(5.6))

print("truncate of 3467.4 is",math.trunc(3467.4))

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

2.Study of python libraries for ML applications such as pandas,matplot libraries.

Aim:

To study of python libraries for ML applications such as pandas,matplot.

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.index)#to get row index syntax datafrme.index

print(s2.columns) #to get column labels dataframe.column

print(s2.size)#size of df rows*columns

print(s2.shape)#to get dimensionality of df

print(s2.memory_usage())#memory requirement to store each column

print(s2.ndim)#no. of dimensions

print(s2.head())#returns first 5 rows from data frame(default)

print(s2.tail())#returns last ...........................

#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

import matplotlib.pyplot as plt

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.title("Scatter plot of sepalwidthcm")

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

2.Import required libraries and packages.

3.Import,read and print the dataset.

4.Find out the null values present and fill the NA values using Forward and Backward
filling.

5.Using drop() drop the unnecessary values.

6.Find the dimensions of the dataset like shape and size

7.Replace the values with replace().

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

4. Simple Linear Regression

Aim:

To write a program for simple linear regression using machine learning.

Algorithm:

1.Start

2.Import required libraries.

3.Using pandas read the csv file using a variable.

4.Do the data pre-processing steps by filling the NA values by using pad method and
remove the insignificant variables.

5.Build the model by setting the regression fit.

6.Split the dataset into training and testing 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

import seaborn as sns

from sklearn.model_selection import train_test_split

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

from sklearn.linear_model import LinearRegression

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)

from sklearn.metrics import mean_squared_error

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

5. Multiple Linear Regressions

Aim:

To write a program for simple linear regression using machine learning.

Algorithm:

1.Start

2.import required libraries.

3.Using pandas read the CSV file using a variable.

4.Copy the dataset into a variable and print the dataset.

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.

7.Using predicted values and actual calculated values,calculate the R-squared


value,which gives the performance of the trained model.

8.Stop

Program:

import os

import pandas as pd

import seaborn as sns

import numpy as np

from sklearn.model_selection import train_test_split

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"])

from sklearn.linear_model import LinearRegression

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)

from sklearn.metrics import mean_squared_error

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.

6a. Lasso Regression (mse)


SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning

Aim:

To write a program for lasso regression (mse) using machine learning.

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.

5.Split the given dataset into train and test data

6.Fitting the models

i. Fit the model by training,using lasso regression method with training dataset.

ii. Test the model by using the test dataset.

iii. Calculate the score of test and train data model.

7. For calculating RMSE,use log function.

8.Stop

Program:

import numpy as np

import os

import pandas as pd

import seaborn as sns

from sklearn.model_selection import train_test_split

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)

from sklearn.linear_model import Lasso

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

6b. Ridge Regression (mse)

Aim:

To write a program for ridge regression (mse) using machine learning.

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.

5.Split the given dataset into train and test data

6.Fitting the models

i. Fit the model by training with training dataset using Ridge Regression

ii. Test the model performance by using the test dataset.

iii. Calculate the score of the model for train and test data

7. For calculating RMSE,use log function.

8.Stop

Program:

import numpy as np

import os

import pandas as pd

import seaborn as sns

from sklearn.model_selection import train_test_split

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)

from sklearn.linear_model import Ridge

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

6c. Lasso Regression (score)

Aim:

To write a program for lasso regression (score) using machine learning.

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.

5.Split the given dataset into train and test data

6.Fitting the models

i. Fit the model by training with training dataset using Lasso Regression

ii. Test the model performance by using the test dataset.

7.Calculate the score of the train and test data.

8.Stop.

Program:

import numpy as np

import os

import pandas as pd

import seaborn as sns

from sklearn.model_selection import train_test_split

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)

from sklearn.linear_model import Lasso

lasso=Lasso()

lasso.fit(x_train,y_train)

train_score=lasso.score(x_train,y_train)

test_score=lasso.score(x_test,y_test)

print("train score for model is {}".format(train_score))

print("test score for model is {}".format(test_score))

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

6d. Ridge Regression (score)

Aim:

To write a program for ridge regression (score) using machine learning.

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.

5.Split the given dataset into train and test data

6.Fitting the models

i. Fit the model by training with training dataset using Ridge Regression

ii. Test the model performance by using the test dataset.

7.Calculate the score of the train and test data.

8.Stop.

Program:

import numpy as np

import os

import pandas as pd

import seaborn as sns

from sklearn.model_selection import train_test_split

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)

from sklearn.linear_model import Ridge

ridge=Ridge()

ridge.fit(x_train,y_train)

train_score=ridge.score(x_train,y_train)

test_score=ridge.score(x_test,y_test)

print("train score for model is {}".format(train_score))

print("test score for model is {}".format(test_score))

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:

To write a program for logistic regression using machine learning.

Algorithm:

1.Start.

2.Import required libraries,read the dataset by importing it.

3.Split the dataset into training and testing dataset.

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.

6.Find the best attribute using S-graph,boxplot,heat map.

7.Stop.

Program:

import os

import pandas as pd

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

from sklearn import metrics

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))

import seaborn as sns

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

import matplotlib.pyplot as plt

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

plt. figure(figsize=(12, 10))

cor=x_train.corr()

sns. heatmap(cor, annot=True, cmap=plt.cm.Reds)

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:

To write a program for decision tree using machine learning.

Algorithm:

1.Start

2.Import the required libraries and packages.

3.Read and import the dataset,split the dataset into training and testing data.

4.Build the model by using DecisionTreeClassifier() and predict the output.

5.Print the evaluation metrics of the model,confusion matrix and plot it.

6.Stop.

Program:

import os

import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split

from sklearn import metrics

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.

09. Naive Bayes


SREENIVASA INSTITUTE OF TECHNOLOGY AND MANAGEMENT STUDIES.
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Artificial Intelligence and Machine Learning

Aim:

To write a program for navie bayes using machine learning.

Algorithm:

1.Start

2.Import required packages and libraries.

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.

5.Calculate all the evaluation metrics for the model.

6.Stop.

Program:

import os

import pandas as pd

from sklearn.naive_bayes import GaussianNB

from sklearn.model_selection import train_test_split

from sklearn import metrics

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:

To write a program for KNN using machine learning.

Algorithm:

1.Start

2.Import required libraries and packages.

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.

5.Calculate all the evaluation metrics for the model.

6.Stop.

Program:

import os

import pandas as pd

from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import train_test_split

from sklearn import metrics

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:

To write a program for SVM using machine learning.

Algorithm:

1.Start

2.import the required libraries and packages.

3.import and read the dataset.

4.Split the dataset into testing and training data for training the model after data pre-
processing.

5.Build the model using SVM classifier.

6.Calculate all the evaluation metrics .

7.Stop.

Program:

#kernel=linear

import os

import pandas as pd

from sklearn import svm

from sklearn.model_selection import train_test_split

from sklearn import metrics

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

from sklearn import svm

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

from sklearn import svm

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:

To write a program for K-Means clustering using machine learning.

Algorithm:

1.Start

2.Import all the required packages, libraries and dataset.

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.

5.Evaluate the Silhoutte_score.

6.Stop.

Program:

import os

import pandas as pd

import numpy as np

from sklearn.cluster import KMeans

from sklearn.metrics import Silhoutte_score

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.

You might also like