3170724-machine-learning-lab-manual
3170724-machine-learning-lab-manual
10 10
Write a program to implementation of hierarchical
clustering from dataset
Machine Learning[1010206715] 2107020601029
Practical-1
Aim: - Write a program to Implementation of mean, median and mode.
import statistics
def main():
num_list = input("Enter a list of numbers separated by spaces:
").split() num_list = [int(num) for num in num_list]
Output: -
BAIT,Surat 1
Machine Learning[1010206715] 2107020601029
Practical-2
Aim: - Write a program to implement Data distribution histogram.
import matplotlib.pyplot as
plt import numpy as np
data = np.random.normal(0, 1, 1000) # Generate 1000 random data points with mean 0 and
standard deviation 1
plt.hist(data, bins=20, edgecolor='k') # You can adjust the number of bins as needed
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Data Distribution Histogram')
plt.show()
Output: -
BAIT,Surat 2
Machine Learning[1010206715] 2107020601029
Practical-3
Aim: - Write a program to implement scatter plot using given dataset.
import matplotlib.pyplot as plt
x1 = [90, 46, 38, 40, 98, 12, 68, 36, 40, 22]
y1 = [24, 48, 6, 38, 68, 98, 56, 74, 60, 12]
x2 = [28, 30, 50, 66, 8, 6, 38, 68, 74, 42]
y2 = [28, 36, 95, 36, 40, 22, 58, 4, 50, 18]
Output: -
BAIT,Surat 3
Machine Learning[1010206715] 2107020601029
Practical-4
Aim: - Write a program to Implementation of linear regression from given dataset.
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
X = np.array([2, 4, 6, 8, 10]).reshape(-1, 1)
y = np.array([3, 6, 9, 12, 15])
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
slope = model.coef_[0]
intercept = model.intercept_
print(f"Slope: {slope}")
print(f"Intercept:
{intercept}")
plt.scatter(X, y, label='Data', color='black')
plt.plot(X, y_pred, label='Linear Regression',
color='blue') plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression
Example') plt.legend()
plt.show()
Output: -
BAIT,Surat 4
Machine Learning[1010206715] 2107020601029
Practical-5
Aim: - Write a program to implement Scale.
min_max_scaler = MinMaxScaler()
scaled_data_minmax = min_max_scaler.fit_transform(data)
standard_scaler = StandardScaler()
scaled_data_standard = standard_scaler.fit_transform(data)
Output: -
BAIT,Surat 5
Machine Learning[1010206715] 2107020601029
Practical-6
Aim: - Write a program to training and testing from given dataset.
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 4, 5, 4, 5])
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
slope = model.coef_[0]
intercept = model.intercept_
print(f"Slope: {slope}")
print(f"Intercept: {intercept}")
print(f"Mean Squared Error: {mse}")
Output: -
BAIT,Surat 6
Machine Learning[1010206715] 2107020601029
Practical-7
Aim: - Write a program to Implementation of Decision tree from given dataset.
data = load_iris()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train) y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
Output: -
BAIT,Surat 7
Machine Learning[1010206715] 2107020601029
Practical-8
Aim: - Write a program to Implement K-Nearest Neighbors Algorithm from given dataset.
data = load_iris()
X = data.data
y = data.target
Output: -
BAIT,Surat 8
Machine Learning[1010206715] 2107020601029
Practical-9
Aim: - Write a program to implementation of K- Mean clustering from given dataset.
X, y = make_blobs(n_samples=300, centers=3,
random_state=42)
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)
labels = kmeans.labels_
BAIT,Surat 9
Machine Learning[1010206715] 2107020601029
Practical-10 Practical-10
Aim: - Write a program to implementation of hierarchical clustering from dataset.
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.datasets import make_circles
Output: -
BAIT,Surat 10