ML Lab File Shubham
ML Lab File Shubham
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)
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
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.
John
24
{'name': 'John', 'age': 30, 'gender': 'Male'}
Exp 2
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)
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)
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]]
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]
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
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)
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)
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]
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')
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')
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]
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]
plt.title('Bar Plot')
plt.xlabel('X axis values')
plt.ylabel('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]
plt.title('Bar Plot')
plt.legend()
plt.xlabel('Function values')
plt.ylabel('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]
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')
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
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
figure = hm.get_figure()
figure.savefig('heatmap.png', dpi = 700)
files.download('heatmap.png')
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
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
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
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()
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
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
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))
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()
Christopher
3 Color 813.0 164.0 2
Nolan
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()
Christopher
3 Color 813.0 164.0
Nolan
Robert
5035 Color 56.0 81.0
Rodriguez
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')
Christopher
3 Color 813.0 164.000000
Nolan
Benjamin
5040 Color 13.0 76.000000
Roberds
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
2 SPECTRE
... ...
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()
Christopher
3 Color 813.0 164 2
Nolan
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
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
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, 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-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])
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()
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
0.9385964912280702
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
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")
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
lin_svc = svm.LinearSVC(C=C).fit(X, y)
h = 0.02
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.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
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))
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()
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()
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()
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()
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
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
data.feature_names
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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 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
5 rows × 31 columns
df.tail()
5 rows × 31 columns
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)
▾ 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
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)')
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)
Parameters
----------
block : bool, optional
Whether to wait for all figures to be closed before returning.
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
: