ML Lab Record
ML Lab Record
Write a python program to compute Central Tendency Measures: Mean, Median, Mode Measure of
Dispersion: Variance, Standard Deviation
Code :
import statistics
data = []
for i in range(n):
data.append(num)
mean = statistics.mean(data)
median = statistics.median(data)
mode = statistics.mode(data)
variance = statistics.variance(data)
std_deviation = statistics.stdev(data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
print("Variance:", variance)
OUTPUT:
2. Study of Python Basic Libraries such as Statistics, Math, Numpy and Scipy
CODE:
print("STATISTICS MODULE")
print("Mean:", st.mean(data1))
print("Median:", st.median(data1))
print("Mode:", st.mode(data1))
print("Variance:", st.variance(data1))
print("\nMATH MODULE")
print("Factorial:", math.factorial(num2))
print("\nNUMPY MODULE")
print("Mean:", np.mean(data2))
print("Median:", np.median(data2))
print("Variance:", np.var(data2))
print("Mode:", mode_result.mode[0])
output:
Code
import pandas as pd
data = {
df = pd.DataFrame(data)
print(df)
print("Describe:\n", df.describe())
print("Names:\n", df['Name'])
print("\nMATPLOTLIB MODULE")
x = [1, 2, 3, 4]
plt.plot(x, y, marker='o')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
OUTPUT
. 4. Write a Python program to implement Simple Linear Regression
import numpy as np
x = []
y = []
print("Enter values for X:")
for i in range(n):
x.append(val)
for i in range(n):
y.append(val)
x = np.array(x).reshape(-1, 1)
y = np.array(y)
model = LinearRegression()
model.fit(x, y)
slope = model.coef_[0]
intercept = model.intercept_
y_pred = model.predict(x)
print("\nPredicted Y values:")
for i in range(n):
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT
Enter the number of data points: 5
Enter values for X:
X[1]: 1
X[2]: 2
X[3]: 3
X[4]: 4
X[5]: 5
Enter corresponding values for Y:
Y[1]: 2
Y[2]: 4
Y[3]: 6
Y[4]: 8
Y[5]: 10
Predicted Y values:
For X = 1.0, Predicted Y = 2.0
For X = 2.0, Predicted Y = 4.0
For X = 3.0, Predicted Y = 6.0
For X = 4.0, Predicted Y = 8.0
For X = 5.0, Predicted Y = 10.0
3. Implementation of Multiple Linear Regression for House Price Prediction using sklearn
import numpy as np
x_train = np.array([
[1400, 3, 2],
[1600, 3, 2],
[1700, 3, 3],
[1875, 4, 4],
[1100, 2, 1],
[1550, 3, 3],
[2350, 4, 3],
[2450, 4, 4],
[1425, 3, 2],
[1700, 3, 3]
])
y_train = np.array([245000, 312000, 279000, 308000, 199000, 219000, 405000, 324000, 319000,
255000])
model = LinearRegression()
model.fit(x_train, y_train)
coefficients = model.coef_
intercept = model.intercept_
print("Coefficients:", coefficients)
print("Intercept:", intercept)
print("\nEnter features for new house (size in sqft, number of bedrooms, and age of the house):")
new_features = list(map(float, input("Enter size, bedrooms, and age separated by space: ").split()))
predicted_price = model.predict([new_features])
predicted_prices_train = model.predict(x_train)
plt.figure(figsize=(10, 6))
plt.xlabel('House Number')
plt.ylabel('Price ($)')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT
Multiple Linear Regression Model for House Price Prediction
Coefficients: [ 7.02856369e+01 1.05581291e+05 -5.77946392e+04]
Intercept: -15854.474075709237
Enter features for new house (size in sqft, number of bedrooms, and age of
the house):
Enter size, bedrooms, and age separated by space: 1500 2 4
Predicted Price for new house: $69,558.01
6. Implementation of Decision tree using sklearn and its parameter tuning
import numpy as np
model = DecisionTreeRegressor(random_state=42)
param_grid = {
'min_samples_leaf': [1, 2, 4]
grid_search.fit(X, y)
best_model = grid_search.best_estimator_
best_params = grid_search.best_params_
print("\nBest Parameters:", best_params)
y_pred = best_model.predict(X)
plt.figure(figsize=(10, 6))
plt.xlabel('Feature')
plt.ylabel('Target')
plt.legend()
plt.grid(True)
plt.show()
output:
import numpy as np
X = []
y = []
for i in range(n):
X.append([feature])
for i in range(n):
y.append(target)
X = np.array(X)
y = np.array(y)
model = KNeighborsRegressor(n_neighbors=k)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
plt.xlabel('Feature')
plt.ylabel('Target')
plt.legend()
plt.grid(True)
plt.show()
output
X = []
y = []
for i in range(n):
for i in range(n):
X = np.array(X).reshape(-1, 1)
y = np.array(y)
model = LogisticRegression()
model.fit(X_train, y_train)
x_min = min(X)[0] - 1
x_max = max(X)[0] + 1
y_prob = model.predict_proba(x_range)[:, 1]
plt.figure(figsize=(10, 6))
plt.xlabel('Feature')
plt.ylabel('Probability')
plt.legend()
plt.grid(True)
plt.show()
import numpy as np
X = []
for i in range(n):
X.append(point)
X = np.array(X)
model.fit(X)
labels = model.labels_
centroids = model.cluster_centers_
print("\nCluster Centers:")
plt.figure(figsize=(10, 6))
for i in range(k):
cluster_points = X[labels == i]
plt.title('K-Means Clustering')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT