Artificial Intelligence and Machine Learning
ISBN 9788119221196

Highlights

Notes

  

Chapter 4: Study of Supervised Learning

Linear Regression ◀◀◀

import numpy as np

import pandas as pd

import matplotlib.pyplot as pl

from sklearn import linear_model

df = pd.read_csv(“dataset1.csv”)

df

Output:

table-wrap

area

price

0

2600

550000

1

3000

565000

2

3200

610000

3

3600

680000

4

4000

725000

df.shape

Output: (5, 2)

%matplotlib inline

pl.xlabel(‘Area in square feet’)

pl.ylabel(‘Price in Rupees’)

pl.scatter(df.area,df.price, color=‘b’, marker=‘s’)

Output:

reg = linear_model.LinearRegression()

reg.fit(df[[‘area’]],df.price)

Output: LinearRegression()

reg.coef_ #coef is m (slope)

Output: array([135.78767123])

reg.predict([[3300]])

Output: array([628715.75342466])

reg.intercept_ #intercept is b

Output: 180616.43835616432

# y = m * x + b

135.78767123 * 3300 + 180616.43835616432

Output: 628715.7534151643

Logistic Regression ◀◀◀

import numpy as np

import pandas as pd

from matplotlib import pyplot as pl

df1 = pd.read_csv(“loan.csv”)

df1.head()

Output:

table-wrap

age

bought_loan

0

23

0

1

18

0

2

55

1

3

43

0

4

36

1

pl.scatter(df1.age,df1.bought_loan, marker=‘d’, color=‘b’)

Output:

from sklearn.model_selection import train_test_split

x_train, x_test,y_train,y_test = train_test_split(df1[[‘age’]], df1.bought_loan, train_size=0.9, shuffle=False)

x_test

Output:

table-wrap

age

18

33

19

60

20

52

from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression()

logreg.fit(x_train,y_train)

Output: LogisticRegression()

logreg.predict(x_test)

Output: array([0, 1, 1], dtype=int64)

K Nearest Neighbour Algorithm ◀◀◀

import numpy as np

import pandas as pd

ds = pd.read_csv(“iris.csv”)

ds

Output:

ds.iloc[:,1:5]

Output:

x = ds.iloc[:, 1:5].values

y = ds.iloc[:, 5].values

from sklearn.preprocessing import LabelEncoder #0 and nclass-1

lblenc_y = LabelEncoder()

y = lblenc_y.fit_transform(y)

y

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.neighbors import KNeighborsClassifier

knn_model = KNeighborsClassifier(n_neighbors=5)

knn_model.fit(x_train, y_train)

Output: KNeighborsClassifier()

y_predict = knn_model.predict(x_test)

from sklearn.metrics import confusion_matrix, classification_report

print(confusion_matrix(y_test,y_predict))

Output:

[[13 0 0]

[0 7 2]

[0 0 8]]

#accuracy

print(28/30)

Output: 0.9333333333333333

print(classification_report(y_test, y_predict))