0% found this document useful (0 votes)
26 views56 pages

ML Lab File Shubham

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)
26 views56 pages

ML Lab File Shubham

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/ 56

Machine Learning IT-323

Shubham Singhal 2K22/AE/64

Exp 1

Write a program to add two numbers to illustrate the use of print statement.

a = 7
b = 9
c = a+b
print("The sum of the two numbers is : ", c)

The sum of the two numbers is : 16

Write a program to illustrate the use of conditional statements by checking if the input
number is odd or even.

a = 17
if(a%2==0):
print('Even')
else:
print('Odd')

Odd

Write a program to illustrate the use of functions.

def myFunction(a):
if(a%2==0):
print("Even")
else:
print('Odd')
myFunction(12)

Even

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 1 of 56
:
Write a program to access the values in a dictionary.

d1 = {'name': 'John', 'age': 24, 'gender': 'Male'}


print(d1['name'])
print(d1['age'])
d1['age'] = 30
print (d1)

John
24
{'name': 'John', 'age': 30, 'gender': 'Male'}

Write a program to perform various string operations.

s1 = input('Enter first string : ')


s2 = input('Enter second string : ')
s3 = s1 + ' ' + s2
print('Concatenated string is ', s3)
print(s3.find('neha'))
print(s3.find('abc'))
s4 = s3.replace('hi', 'bye')
print(s4)

Enter first string : dgfdcddabc


Enter second string : neha
Concatenated string is dgfdcddabc neha
11
7
dgfdcddabc neha

Exp 2

To understand NumPy functions.

import numpy as np

1-D Array

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 2 of 56
:
a = np.array([1,2,3,4,5,6])
print("The dimensions of a are", a.ndim)
print('The shape of a is', a.shape)
print("The datatype of a is :", a.dtype)

The dimensions of a are 1


The shape of a is (6,)
The datatype of a is : int64

b = np.array([[1,2,3], [4,5,6]])
print(b)
bb= a.reshape((3,2))
print(bb)
print('Dimension of b is', b.ndim)
print('Shape of b is', b. shape)

[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
Dimension of b is 2
Shape of b is (2, 3)

Operations on Numpy array

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 3 of 56
:
print(a)
print(a*3)
print('')
print(a)
print(a**2)
print('-----')
print([1,2,3]*2)
print(a)
a[0] = 10
print(a)
print(bb)

[1 2 3 4 5 6]
[ 3 6 9 12 15 18]

[1 2 3 4 5 6]
[ 1 4 9 16 25 36]
-----
[1, 2, 3, 1, 2, 3]
[1 2 3 4 5 6]
[10 2 3 4 5 6]
[[10 2]
[ 3 4]
[ 5 6]]

Create a NumPy array copy

c = a.reshape(3,2).copy()
print(c)
c[0][0] = 99
print(c)
print(a)

[[10 2]
[ 3 4]
[ 5 6]]
[[99 2]
[ 3 4]
[ 5 6]]
[10 2 3 4 5 6]

Indexing in NumPy arrays

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 4 of 56
:
print(a[0])
print(a[np.array([1,2])])
print('Even elements of a are: ', a[a%2==0])
a[a==10] = 12
print(a)

10
[2 3]
Even elements of a are: [10 2 4 6]
[12 2 3 4 5 6]

Clipping in Arrays

d = a.copy()
print(d)
print(d.clip(3,5))

[12 2 3 4 5 6]
[5 3 3 4 5 5]

Exp 3

import matplotlib.pyplot as plt

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 5 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,1,2,4,5,6,7,7,8,11]
plt.plot(x,y)

[<matplotlib.lines.Line2D at 0x7becac842320>]

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 6 of 56
:
from google.colab import files
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,1,2,4,5,6,7,7,8,11]
plt.plot(x,y)
plt.savefig('testfig.png', dpi = 3000)
files.download("testfig.png")

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 7 of 56
:
from google.colab import files
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,1,2,4,5,6,7,7,8,11]
plt.plot(x,y)

plt.savefig('testfig1.png', dpi = 200)


plt.savefig('testfig2.png', dpi = 1500)

files.download("testfig1.png")
files.download("testfig2.png")

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 8 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,8,11,7,15,21,24,30,30,40]

plt.plot(x,y)

plt.title("My first plot")


plt.xlabel("x-axis")
plt.ylabel("y-axis")

Text(0, 0.5, 'y-axis')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 9 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,8,11,7,15,21,24,30,30,40]

plt.plot(x,y, color = 'red')

plt.title("My first plot")


plt.xlabel("x-axis")
plt.ylabel("y-axis")

Text(0, 0.5, 'y-axis')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 10 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y1 = [1,8,11, 7, 15, 21, 24, 30,30,40]
y2 = [1,2,3,4,5,6,7,8,9,10]
y3 = [3,4,5,6,2,9,11,6,0,1]
plt.plot(x, y1, color = 'red')
plt.plot(x, y2, color = 'green')
plt.plot(x, y3, color = 'blue')
plt.title('Multiple Line Plots')
plt.xlabel('X axis values')
plt.ylabel('Y axis values')

Text(0, 0.5, 'Y axis values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 11 of 56
:
x= [1,2,3,4,5,6,7,8,9,10]
y1 = [1,8,11, 7, 15, 21, 24, 30, 30, 40]
y2 = [1,2,3,4,5,6,7,8,9,10]
y3 = [3,4,5,6,2,9,11,6,0,1]
plt.plot(x, y, color = 'red', label = 'plot 1')
plt.plot(x, y2, color = 'green', label = 'plot 2')
plt.plot(x, y3, color = 'blue', label = 'plot 3')
plt.title('Multiple Line Plots')
plt.legend()
plt.xlabel('X axis values')
plt.ylabel('Y axis values')

Text(0, 0.5, 'Y axis values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 12 of 56
:
plt.style.use('classic')
x = [1,2,3,4,5,6,7,8,9,10]

y1 = [1,8,11, 7, 15, 21, 24, 30,30,40]


y2 = [1,2,3,4,5,6,7,8,9,10]
y3 = [3,4,5,6,2,9,11,6,0,1]

plt.plot(x, y, color = 'red', label = 'plot 1')


plt.plot(x, y2, color = 'green', label = 'plot 2')
plt.plot(x, y3, color = 'blue', label = 'plot 3')
plt.title('Multiple Line Plots')
plt.legend()
plt.xlabel('X axis values')
plt.ylabel('Y axis values')

Text(0, 0.5, 'Y axis values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 13 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]

y1 = [1,8,11, 7, 15, 21, 24, 30,30,40]

plt.bar(x, y1, color = 'green')

plt.title('Bar Plot')
plt.xlabel('X axis values')
plt.ylabel('Y axis values')

Text(0, 0.5, 'Y axis values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 14 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]

y1 = [1,8,11, 7, 15, 21, 24, 30,30,40]

plt.barh(x, y1, color = 'green')

plt.title('Bar Plot')
plt.legend()
plt.xlabel('Function values')
plt.ylabel('Input values')

WARNING:matplotlib.legend:No artists with labels found to put in legend. No


Text(0, 0.5, 'Input values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 15 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y1 = [1000, 1100, 1300, 7,1,21,4,13,9,11]

plt.bar(x,y1, color = 'green')


plt.title('Bar Plot')
plt.xlabel('Function values')
plt.ylabel('Input values')

Text(0, 0.5, 'Input values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 16 of 56
:
x = [1,2,3,4,5,6,7,8,9,10]
y1 = [1000,1100,1300, 7, 1, 21, 4, 13,9,11]
plt.bar(x, y1, color = 'green')
plt.title('Bar Plot')
plt.yscale('log')
plt.legend()
plt.xlabel('Function Values')
plt.ylabel('Input Values')

WARNING:matplotlib.legend:No artists with labels found to put in legend. No


Text(0, 0.5, 'Input Values')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 17 of 56
:
import seaborn as sn
values = [[12,78,34], [1,1,99], [32,100,0]]
hm = sn.heatmap(data = values)

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 18 of 56
:
values = [[12,78,34], [1,1,99], [32,100,0]]
hm = sn.heatmap(data = values, annot = True)

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 19 of 56
:
values = [[12,78,34], [1,1,99], [32,100,0]]
hm = sn.heatmap(data = values, annot = True, fmt = 'd')

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 20 of 56
:
values = [[12,78,34], [1,1,99], [32, 100, 0]]
title = 'First Heatmap'
xlabels = ['A', 'B', 'C']
ylabels = ['D', 'E', 'F']
hm= sn.heatmap(data = values, annot = True, fmt = 'd', xticklabels = xlabels, yt

hm.set(title = title, xlabel = 'Horizontal', ylabel = 'Vertical')

[Text(0.5, 1.0, 'First Heatmap'),


Text(0.5, 24.999999999999986, 'Horizontal'),
Text(57.0, 0.5, 'Vertical')]

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 21 of 56
:
values = [[12,78,34], [1,1,99], [32, 100, 0]]
title = 'First Heatmap'
xlabels = ['A', 'B', 'C']
ylabels = ['D', 'E', 'F']
hm= sn.heatmap(data = values, annot = True, fmt = 'd', xticklabels = xlabels, yt

hm.set(title = title, xlabel = 'Horizontal', ylabel = 'Vertical')

figure = hm.get_figure()
figure.savefig('heatmap.png', dpi = 700)
files.download('heatmap.png')

# This is formatted as code

Exp 4

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 22 of 56
:
import cv2

img= cv2.imread('Olives.png', cv2.IMREAD_GRAYSCALE)

plt.imshow(img, cmap='gray')
plt.title('Olives')
plt.show()
cv2.imwrite('Olives_Copy.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Exp 5

import random
import matplotlib.pyplot as plt

data = pd.read_csv("/content/drive/MyDrive/sem 5 ml lab exp/Advertising - Advert


data.head()

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 23 of 56
:
def predict_sales(radio, weight, bias):
return weight * radio + bias

def cost_function(radio, sales, weight, bias):


companies = len(radio)
total_error = 0.0
for i in range(companies):
total_error += (sales[i] - (weight * radio[i] + bias))**2
return total_error / companies

def update_weight(radio, sales, weight, bias, learning_rate):


weight_deriv = 0.0
bias_deriv = 0.0
companies = len(radio)
for i in range(companies):
weight_deriv += -2 * radio[i] * (sales[i] - (weight * radio[i] + bias))
bias_deriv += -2 * (sales[i] - (weight * radio[i] + bias))
weight -= (weight_deriv / companies) * learning_rate
bias -= (bias_deriv / companies) * learning_rate
return weight, bias

def train(radio, sales, weight, bias, learning_rate, iters):


cost_history = []
for i in range(iters):
weight, bias = update_weight(radio, sales, weight, bias, learning_rate)
cost = cost_function(radio, sales, weight, bias)
cost_history.append(cost)
if i % 10 == 0:
print("iter={:d} weight={:.2f} bias={:.4f} cost={:.2f}".format(i, we
return weight, bias, cost_history

radio = data['radio'].values
sales = data['sales'].values
weight = 0.0
bias = 0.0
learning_rate = 0.001
lr_iters = 100
weight, bias, cost_history = train(radio, sales, weight, bias, learning_rate, lr

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 24 of 56
:
iter=0 weight=0.74 bias=0.0280 cost=92.32
iter=10 weight=0.49 bias=0.0737 cost=42.70
iter=20 weight=0.48 bias=0.1268 cost=42.42
iter=30 weight=0.48 bias=0.1795 cost=42.14
iter=40 weight=0.48 bias=0.2320 cost=41.87
iter=50 weight=0.48 bias=0.2842 cost=41.59
iter=60 weight=0.48 bias=0.3361 cost=41.32
iter=70 weight=0.48 bias=0.3877 cost=41.06
iter=80 weight=0.47 bias=0.4390 cost=40.80
iter=90 weight=0.47 bias=0.4900 cost=40.53

print(f'bias: {bias}')
print(f'weight: {weight}')

bias: 0.5356058602630249
weight: 0.4710459903974583

Exp 6

import seaborn as sb

df = sb.load_dataset('iris')
df.head()

sepal_length sepal_width petal_length petal_width species

0 5.1 3.5 1.4 0.2 setosa

1 4.9 3.0 1.4 0.2 setosa

2 4.7 3.2 1.3 0.2 setosa

3 4.6 3.1 1.5 0.2 setosa

4 5.0 3.6 1.4 0.2 setosa

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 25 of 56
:
df['species'].map({'versicolor':0,'virginica':1})

species

0 NaN

1 NaN

2 NaN

3 NaN

4 NaN

... ...

145 1.0

146 1.0

147 1.0

148 1.0

149 1.0

150 rows × 1 columns

dtype: float64

X = df.drop(['species'], axis = 1)
y = df['species']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random

from sklearn.linear_model import LogisticRegression


classifier = LogisticRegression()

from sklearn.model_selection import GridSearchCV


parameter = {'penalty': ['l1', 'l2', 'elasticnet'], 'C': [1, 2, 3, 4, 5, 6, 10,

classifier_regressor = GridSearchCV(classifier, param_grid=parameter, scoring='a


classifier_regressor.fit(X_train, y_train)

Show hidden output

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 26 of 56
:
print(classifier_regressor.best_params_)
print(classifier_regressor.best_score_)
y_pred = classifier_regressor.predict(X_test)
from sklearn.metrics import accuracy_score, classification_report
score = accuracy_score(y_pred, y_test)
print(score)
print(classification_report(y_pred, y_test))

{'C': 1, 'max_iter': 100, 'penalty': 'l2'}


0.9636363636363636
1.0
precision recall f1-score support

setosa 1.00 1.00 1.00 15


versicolor 1.00 1.00 1.00 11
virginica 1.00 1.00 1.00 12

accuracy 1.00 38
macro avg 1.00 1.00 1.00 38
weighted avg 1.00 1.00 1.00 38

Exp 7

data = pd.read_csv('movie_metadata.csv')

data.head()

color director_name num_critic_for_reviews duration director_facebook_

0 Color James Cameron 723.0 178.0

1 Color Gore Verbinski 302.0 169.0

2 Color Sam Mendes 602.0 148.0

Christopher
3 Color 813.0 164.0 2
Nolan

4 NaN Doug Walker NaN NaN

5 rows × 28 columns

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 27 of 56
:
data.county = data.country.fillna(" ")

data.duration=data.duration.fillna(data.duration.mean())

data.dropna()

color director_name num_critic_for_reviews duration director_facebo

0 Color James Cameron 723.0 178.0

1 Color Gore Verbinski 302.0 169.0

2 Color Sam Mendes 602.0 148.0

Christopher
3 Color 813.0 164.0
Nolan

5 Color Andrew Stanton 462.0 132.0

... ... ... ... ...

5026 Color Olivier Assayas 81.0 110.0

5027 Color Jafar Panahi 64.0 90.0

5033 Color Shane Carruth 143.0 77.0

Robert
5035 Color 56.0 81.0
Rodriguez

5042 Color Jon Gunn 43.0 90.0

3755 rows × 28 columns

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 28 of 56
:
data.dropna (axis=1, how='all')

color director_name num_critic_for_reviews duration director_faceb

0 Color James Cameron 723.0 178.000000

1 Color Gore Verbinski 302.0 169.000000

2 Color Sam Mendes 602.0 148.000000

Christopher
3 Color 813.0 164.000000
Nolan

4 NaN Doug Walker NaN 107.201074

... ... ... ... ...

5038 Color Scott Smith 1.0 87.000000

5039 Color NaN 43.0 43.000000

Benjamin
5040 Color 13.0 76.000000
Roberds

5041 Color Daniel Hsia 14.0 100.000000

5042 Color Jon Gunn 43.0 90.000000

5043 rows × 28 columns

data = pd.read_csv('movie_metadata.csv', dtype={'duration': str})


data['duration'] = pd.to_numeric(data['duration'], errors='coerce')
data['duration'] = data['duration'].fillna(data['duration'].mean()).astype(int)

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 29 of 56
:
data['movie_title'].str.upper()

movie_title

0 AVATAR

1 PIRATES OF THE CARIBBEAN: AT WORLD'S END

2 SPECTRE

3 THE DARK KNIGHT RISES

4 STAR WARS: EPISODE VII - THE FORCE AWAKENS ...

... ...

5038 SIGNED SEALED DELIVERED

5039 THE FOLLOWING

5040 A PLAGUE SO PLEASANT

5041 SHANGHAI CALLING

5042 MY DATE WITH DREW

5043 rows × 1 columns

dtype: object

data=data.rename(columns={'title_year':'release_date','movie_facebook_likes':'fa

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 30 of 56
:
data.head()

color director_name num_critic_for_reviews duration director_facebook_

0 Color James Cameron 723.0 178

1 Color Gore Verbinski 302.0 169

2 Color Sam Mendes 602.0 148

Christopher
3 Color 813.0 164 2
Nolan

4 NaN Doug Walker NaN 107

5 rows × 28 columns

Exp 8

import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
data.data

array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,


1.189e-01],
[2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
8.902e-02],
[1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
8.758e-02],
...,
[1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
7.820e-02],
[2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
1.240e-01],
[7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
7.039e-02]])

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 31 of 56
:
data.feature_names

array(['mean radius', 'mean texture', 'mean perimeter', 'mean area',


'mean smoothness', 'mean compactness', 'mean concavity',
'mean concave points', 'mean symmetry', 'mean fractal
dimension',
'radius error', 'texture error', 'perimeter error', 'area
error',
'smoothness error', 'compactness error', 'concavity error',
'concave points error', 'symmetry error',
'fractal dimension error', 'worst radius', 'worst texture',
'worst perimeter', 'worst area', 'worst smoothness',
'worst compactness', 'worst concavity', 'worst concave
points',
'worst symmetry', 'worst fractal dimension'], dtype='<U23')

data.target

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1,
0, 0,
1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0,
0, 0,
1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1,
0, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0,
1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 1,
1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1,
0, 0,
0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0,
1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0,

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 32 of 56
:
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1,
0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0,
1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1,
1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,
1, 1,
1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 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, 0, 0, 0, 0, 0, 0, 1])

df=pd.DataFrame(np.c_[data.data, data.target], columns=[list(data.feature_names)


df.head()

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

0 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001

1 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869

2 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974

3 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414

4 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980

5 rows × 31 columns

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 33 of 56
:
df.tail()

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

564 21.56 22.39 142.00 1479.0 0.11100 0.11590 0.24390

565 20.13 28.25 131.20 1261.0 0.09780 0.10340 0.14400

566 16.60 28.08 108.30 858.1 0.08455 0.10230 0.09251

567 20.60 29.33 140.10 1265.0 0.11780 0.27700 0.35140

568 7.76 24.54 47.92 181.0 0.05263 0.04362 0.00000

5 rows × 31 columns

df.shape

(569, 31)

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 34 of 56
:
x=df.iloc[:, 0:-1]
y=df.iloc[:, -1]
x

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

0 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.30010

1 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.08690

2 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.19740

3 11.42 20.38 77.58 386.1 0.14250 0.28390 0.24140

4 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.19800

... ... ... ... ... ... ... ...

564 21.56 22.39 142.00 1479.0 0.11100 0.11590 0.24390

565 20.13 28.25 131.20 1261.0 0.09780 0.10340 0.14400

566 16.60 28.08 108.30 858.1 0.08455 0.10230 0.09251

567 20.60 29.33 140.10 1265.0 0.11780 0.27700 0.35140

568 7.76 24.54 47.92 181.0 0.05263 0.04362 0.00000

569 rows × 30 columns

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, random_st
from sklearn.neighbors import KNeighborsClassifier
classifier=KNeighborsClassifier()
classifier.fit(X_train, Y_train)
classifier.score(x_test, y_test)

0.9385964912280702

patientData=[100.1, 50, 12, 110]

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 35 of 56
:
patientData1=x_test
patientData1

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

517 19.89 20.26 130.50 1214.0 0.10370 0.13100 0.14110

213 17.42 25.56 114.50 948.0 0.10060 0.11460 0.16820

349 11.95 14.96 77.23 426.7 0.11580 0.12060 0.01171

421 14.69 13.98 98.22 656.1 0.10310 0.18360 0.14500

263 15.61 19.38 100.00 758.6 0.07840 0.05616 0.04209

... ... ... ... ... ... ... ...

420 11.57 19.04 74.20 409.7 0.08546 0.07722 0.05485

311 14.61 15.69 92.68 664.9 0.07618 0.03515 0.01447

466 13.14 20.74 85.98 536.9 0.08675 0.10890 0.10850

200 12.23 19.56 78.54 461.0 0.09586 0.08087 0.04187

229 12.83 22.33 85.26 503.2 0.10880 0.17990 0.16950

114 rows × 30 columns

pred=classifier.predict(patientData1)

for i in range(len(pred)):

if pred[i]==0:
print("Patient has cancer malignant tumar")
else:
print("Patient has no cancer benign tumar")

Patient has cancer malignant tumar


Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 36 of 56
:
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has no cancer benign tumar
Patient has cancer malignant tumar

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 37 of 56
:
Exp 9

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 38 of 56
:
from sklearn import datasets, svm
iris = datasets.load_iris()
def visualize_sepal_data():
X = iris.data[:, :2]
y = iris.target
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.title('Sepal Width & Length')
plt.show()
def visualize_petal_data():
X = iris.data[:, 2:]
y = iris.target
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.xlabel('Petal length')
plt.ylabel('Petal width')
plt.title('Petal Width & Length')
plt.show()

X = iris.data[:, :2]
y = iris.target
C = 1.0

svc = svm.SVC(kernel='linear', C=C).fit(X, y)

lin_svc = svm.LinearSVC(C=C).fit(X, y)

rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)

poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)

h = 0.02

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1


y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))

titles = ['SVC with linear kernel',


'LinearSVC (linear kernel)',
'SVC with RBF kernel',
'SVC with polynomial (degree 3) kernel']

for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 39 of 56
:
for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
plt.subplot(2, 2, i + 1)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)

plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)

plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlabel('Petal length')
plt.ylabel('Petal width')

plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())

plt.xticks(())
plt.yticks(())

plt.title(titles[i])

plt.show()

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 40 of 56
:
Exp 10

import numpy as np

input_data = np.array([[1, 0, 1, 0], [1, 0, 1, 1], [0, 1, 0, 1]])


output_data = np.array([[1], [1], [0]])

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

epochs = 5000
learning_rate = 0.1

input_neurons = input_data.shape[1]
hidden_neurons = 3
output_neurons = 1

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 41 of 56
:
weights_input_hidden = np.random.uniform(size=(input_neurons, hidden_neurons))
bias_hidden = np.random.uniform(size=(1, hidden_neurons))
weights_hidden_output = np.random.uniform(size=(hidden_neurons, output_neurons))
bias_output = np.random.uniform(size=(1, output_neurons))

for epoch in range(epochs):


hidden_layer_input = np.dot(input_data, weights_input_hidden) + bias_hidden
hidden_layer_output = sigmoid(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) + bi
predicted_output = sigmoid(output_layer_input)

error_output_layer = output_data - predicted_output


slope_output_layer = sigmoid_derivative(predicted_output)
slope_hidden_layer = sigmoid_derivative(hidden_layer_output)

delta_output_layer = error_output_layer * slope_output_layer


error_hidden_layer = delta_output_layer.dot(weights_hidden_output.T)
delta_hidden_layer = error_hidden_layer * slope_hidden_layer

weights_hidden_output += hidden_layer_output.T.dot(delta_output_layer) * lea


weights_input_hidden += input_data.T.dot(delta_hidden_layer) * learning_rate
bias_output += np.sum(delta_output_layer, axis=0, keepdims=True) * learning_
bias_hidden += np.sum(delta_hidden_layer, axis=0, keepdims=True) * learning_

print("Predicted output after training:")


print(predicted_output)

Predicted output after training:


[[0.9831553 ]
[0.96735236]
[0.04442678]]

Exp 11

import itertools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score, confusion_matrix

df = pd.read_csv("cancerdata.csv")

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 42 of 56
:
df.head()

id diagnosis radius_mean texture_mean perimeter_mean area_mean

0 842302 M 17.99 10.38 122.80 1001.0

1 842517 M 20.57 17.77 132.90 1326.0

2 84300903 M 19.69 21.25 130.00 1203.0

3 84348301 M 11.42 20.38 77.58 386.1

4 84358402 M 20.29 14.34 135.10 1297.0

5 rows × 33 columns

df = df.drop('id', axis = 1)

df.shape

(569, 32)

M = df[df.diagnosis == 'M']
B = df[df.diagnosis == 'B']

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 43 of 56
:
plt.title("Malignant vs Benign Cancer ")
plt.xlabel("Radius Mean")
plt.ylabel("Texture Mean")
plt.scatter(M.radius_mean, M.texture_mean, color = 'red', label = 'Malignant', a
plt.scatter(B.radius_mean, B.texture_mean, color = 'blue', label = 'Benign', alp
plt.legend()
plt.show()

df.diagnosis = [ 1 if i == 'M' else 0 for i in df.diagnosis]


x = df.drop(['diagnosis'], axis = 1)
y = df.diagnosis.values

x = (x - np.min(x)) / (np.max(x) - np.min(x))


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, rando

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 44 of 56
:
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()

dt.fit(x_train, y_train)

▾ DecisionTreeClassifier i ?
DecisionTreeClassifier()

dt.score(x_test, y_test)

0.956140350877193

Exp 12

import numpy as np
import pandas as pd
import itertools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt

df = pd.read_csv("cancerdata.csv")

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 45 of 56
:
df.head()

id diagnosis radius_mean texture_mean perimeter_mean area_mean

0 842302 M 17.99 10.38 122.80 1001.0

1 842517 M 20.57 17.77 132.90 1326.0

2 84300903 M 19.69 21.25 130.00 1203.0

3 84348301 M 11.42 20.38 77.58 386.1

4 84358402 M 20.29 14.34 135.10 1297.0

5 rows × 33 columns

df = df.drop('id', axis = 1)

df.shape

(569, 32)

M = df[df.diagnosis == 'M']
B = df[df.diagnosis == 'B']

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 46 of 56
:
plt.title("Malignant vs Benign Cancer ")
plt.xlabel("Radius Mean")
plt.ylabel("Texture Mean")
plt.scatter(M.radius_mean, M.texture_mean, color = 'red', label = 'Malignant', a
plt.scatter(B.radius_mean, B.texture_mean, color = 'blue', label = 'Benign', alp
plt.legend()
plt.show()

df.diagnosis = [ 1 if i == 'M' else 0 for i in df.diagnosis]

x = df.drop(['diagnosis'], axis = 1)
y = df.diagnosis.values
x = (x - np.min(x)) / (np.max(x) - np.min(x))
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, rando

from sklearn.tree import DecisionTreeClassifier


dt = DecisionTreeClassifier()

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 47 of 56
:
dt.fit(x_train, y_train)

▾ DecisionTreeClassifier i ?
DecisionTreeClassifier()

dt.score(x_test, y_test)

0.956140350877193

from sklearn.datasets import load_breast_cancer


data = load_breast_cancer()
data.data

array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,


1.189e-01],
[2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
8.902e-02],
[1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
8.758e-02],
...,
[1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
7.820e-02],
[2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
1.240e-01],
[7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
7.039e-02]])

data.feature_names

array(['mean radius', 'mean texture', 'mean perimeter', 'mean area',


'mean smoothness', 'mean compactness', 'mean concavity',
'mean concave points', 'mean symmetry', 'mean fractal
dimension',
'radius error', 'texture error', 'perimeter error', 'area
error',
'smoothness error', 'compactness error', 'concavity error',
'concave points error', 'symmetry error',
'fractal dimension error', 'worst radius', 'worst texture',
'worst perimeter', 'worst area', 'worst smoothness',
'worst compactness', 'worst concavity', 'worst concave
points',
'worst symmetry', 'worst fractal dimension'], dtype='<U23')

data.target

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 48 of 56
:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1,
0, 0,
1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0,
0, 0,
1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1,
0, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0,
1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 1,
1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1,
0, 0,
0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0,
1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1,
0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0,
1, 1,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1,
1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
1, 1,
1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1,
1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,
1, 1,
1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 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, 0, 0, 0, 0, 0, 0, 1])

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 49 of 56
:
data.target_names

array(['malignant', 'benign'], dtype='<U9')

df= pd.DataFrame(np.c_[data.data, data.target], columns=[list(data.feature_names


df.head()

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

0 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001

1 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869

2 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974

3 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414

4 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980

5 rows × 31 columns

df.tail()

mean mean mean mean mean mean mean


radius texture perimeter area smoothness compactness concavity

564 21.56 22.39 142.00 1479.0 0.11100 0.11590 0.24390

565 20.13 28.25 131.20 1261.0 0.09780 0.10340 0.14400

566 16.60 28.08 108.30 858.1 0.08455 0.10230 0.09251

567 20.60 29.33 140.10 1265.0 0.11780 0.27700 0.35140

568 7.76 24.54 47.92 181.0 0.05263 0.04362 0.00000

5 rows × 31 columns

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 50 of 56
:
print("shape of x train = ", x_train.shape)
print("shape of y train = ", y_train.shape)
print("shape of x test = ", x_test.shape)
print("shape of y test = ", y_test.shape)

shape of x train = (455, 31)


shape of y train = (455,)
shape of x test = (114, 31)
shape of y test = (114,)

from sklearn.ensemble import RandomForestClassifier


classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy')
classifier.fit(x_train, y_train)

▾ RandomForestClassifier i ?

RandomForestClassifier(criterion='entropy')

classifier.score(x_test, y_test)

0.956140350877193

patientdata = [[17.99,
10.38,
122.8,
1001.0,
0.1184,
0.2776,
0.3001,0.1471,0.2419,0.07871, 1.095, 0.9053, 8.589, 153.4, 0.006, 0.04904, 0.05

patientdata1 = np.array(patientdata).reshape(1, -1)


patientdata1

array([[1.7990e+01, 1.0380e+01, 1.2280e+02, 1.0010e+03, 1.1840e-01,


2.7760e-01, 3.0010e-01, 1.4710e-01, 2.4190e-01, 7.8710e-02,
1.0950e+00, 9.0530e-01, 8.5890e+00, 1.5340e+02, 6.0000e-03,
4.9040e-02, 5.3373e-02, 1.5870e-02, 3.0030e-02, 6.1930e-03,
2.5380e+01, 1.7330e+01, 1.8460e+02, 2.0190e+03, 1.6220e-01,
6.6560e-01, 7.1190e-01, 2.6000e-01, 4.6010e-01, 1.1890e-01,
1.5000e-02]])

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 51 of 56
:
classifier.predict(patientdata)

/usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X
warnings.warn(
array([1])

pred = classifier.predict(patientdata)

/usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X
warnings.warn(

if pred[0] == 0:
print('Patient has Cancer (malignant tumor)')
else:
print('Patient has no Cancer (malignant benign)')

Patient has no Cancer (malignant benign)

Exp 13

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 52 of 56
:
import matplotlib.pyplot as plt
x= [4, 5, 10, 4, 3, 11, 14, 6, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
plt.scatter(x, y)
plt.show

matplotlib.pyplot.show
def show(*args, **kwargs)

Display all open figures.

Parameters
----------
block : bool, optional
Whether to wait for all figures to be closed before returning.

from sklearn.cluster import KMeans


data = list(zip(x, y))
inertias = []

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 53 of 56
:
for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)
x_values = range(1, len(inertias) + 1)
plt.plot(x_values, inertias, marker='o')
plt.title('Elbow method')
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()

kmeans = KMeans(n_clusters=2)
kmeans.fit(data)

▾ KMeans i ?

KMeans(n_clusters=2)

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 54 of 56
:
plt.scatter(x, y, c=kmeans.labels_)
plt.show()

https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 55 of 56
:
https://colab.research.google.com/drive/13oTpQmuxdl-qr9HHkOClyK7IY5L-laNc#scrollTo=WVG8_Jx4OPEy 06/11/24, 10 02 AM
Page 56 of 56
:

You might also like