ML File
ML File
ML File
Practical – 1
Aim: Write a program to Implementation of mean, median and mode.
Program:
import numpy as np
v1=np.arange(1,33)
print(v1)
print(' ')
v2=np.mean(v1)
print(v2)
print(' ')
v4=np.arange(1,11)
v3=np.median(v4)
print(v3)
print(' ')
v5=np.arange(1,11)
v6=np.arange(2,22)
sum=0
i=0
for i in range(1,11):
sum=sum+i
print(sum)
sum/11
print(sum)
1
ML (1010206715) 2107020601002
Output:
2
ML (1010206715) 2107020601002
Practical – 2
Aim: Write a program to implement Data distribution histogram.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import numpy
import matplotlib.pyplot as plt
plt.hist(x, 100)
plt.show()
Output:
3
ML (1010206715) 2107020601002
Practical – 3
Aim: Write a program to implement scatter plot using given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
plt.scatter(x, y)
plt.show()
Output:
4
ML (1010206715) 2107020601002
Practical – 4
Aim: Write a program to Implementation of linear regression from given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
def myfunc(x):
return slope * x + intercept
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()
5
ML (1010206715) 2107020601002
Output:
6
ML (1010206715) 2107020601002
Practical – 5
Aim: Write a program to implement Scale.
Program:
import pandas
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
scale = StandardScaler()
df = pandas.read_csv("data.csv")
X = df[['Weight', 'Volume']]
scaledX = scale.fit_transform(X)
print(scaledX)
Output:
7
ML (1010206715) 2107020601002
Practical – 6
Aim: Write a program to training and testing from given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import numpy
import matplotlib.pyplot as plt
numpy.random.seed(2)
x = numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40, 100) / x
train_x = x[:80]
train_y = y[:80]
test_x = x[20:]
test_y = y[20:]
plt.scatter(train_x, train_y)
plt.plot(myline, mymodel(myline))
plt.show()
8
ML (1010206715) 2107020601002
Output:
9
ML (1010206715) 2107020601002
Practical – 7
Aim: Write a program to Implementation of Decision tree from given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import pandas
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
df = pandas.read_csv("data.csv")
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
tree.plot_tree(dtree, feature_names=features)
10
ML (1010206715) 2107020601002
Output:
11
ML (1010206715) 2107020601002
Practical – 8
Aim: Write a program to Implement K-Nearest Neighbors Algorithm from given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
knn.fit(data, classes)
new_x = 8
new_y = 21
new_point = [(new_x, new_y)]
prediction = knn.predict(new_point)
12
ML (1010206715) 2107020601002
Output:
13
ML (1010206715) 2107020601002
Practical – 9
Aim: Write a program to implementation of K- Mean clustering from given dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
for i in range(1,11):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data)
inertias.append(kmeans.inertia_)
The elbow method shows that 2 is a good value for K, so we retrain and visualize the result:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
14
ML (1010206715) 2107020601002
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)
plt.scatter(x, y, c=kmeans.labels_)
plt.show()
Output:
15
ML (1010206715) 2107020601002
Practical – 10
Aim: Write a program to implementation of hierarchical clustering from dataset.
Program:
#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
plt.show()
16
ML (1010206715) 2107020601002
Output:
17