Lab Assignment 2
Lab Assignment 2
In [2]: # Q2. Write Python script to find the statistics of the given data (take your own) such as Mean, Median, Standard deviation, Variance and mode.
import numpy as np
from scipy import stats
data = np.array([15, 22, 31, 35, 47, 51, 63, 74, 82, 92])
mean_value = np.mean(data)
median_value = np.median(data)
std_dev = np.std(data)
variance = np.var(data)
mode_result = stats.mode(data)
print(f"Data: {data}")
print(f"Mean: {mean_value:.2f}")
print(f"Median: {median_value}")
print(f"Standard Deviation: {std_dev:.2f}")
print(f"Variance: {variance:.2f}")
print(f"Mode: {mode_result.mode[0]} (occurs {mode_result.count[0]} times)")
In [3]: # Write a python script to implement the KNN Algorithm over given data set and find the class of (5.5. 5.5).
from sklearn.neighbors import KNeighborsClassifier
data = [
[2, 2, 'Car'],
[5, 3.5, 'Car'],
[3.5, 4.5, 'Car'],
[4.5, 2, 'Car'],
[4.5, 6.5, 'Car'],
[2.5, 4.5, 'Truck'],
[6.5, 5.5, 'Truck'],
[6.5, 2.5, 'Truck'],
[7.5, 3.5, 'Truck'],
[3, 2, 'Truck'],
[4.5, 7, 'Truck'],
[6, 7, 'Bus'],
[6.5, 4.5, 'Bus'],
[7.5, 6.5, 'Bus'],
[5, 3.5, 'Bus'],
[5.5, 5.5, 'Bus']
]
X = [row[:2] for row in data]
Y = [row[2] for row in data]
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X, Y)
point_to_classify = [[5.5, 5.5]]
predicted_class = knn.predict(point_to_classify)
print(f"The class of the point (5.5, 5.5) is: {predicted_class[0]}")
In [ ]: