ML Lab Manual
ML Lab Manual
ML Lab Manual
Lab Manual
Machine Learning
(CS-401P)
1. Implement a Linear Regression model to predict house prices based on a dataset.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Sample data
data = pd.DataFrame({
'Size': [650, 800, 1200, 1500, 1700, 2000],
'Price': [70000, 85000, 130000, 160000, 185000, 210000]
})
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Make predictions
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("Predictions:", y_pred)
print("Mean Squared Error:", mse)
Output:
Predictions: [175000.]
Mean Squared Error: 625000000.0
# Load dataset
iris = load_iris()
X = iris.data
Output:
Transformed Data (PCA): [[-2.68412563 0.31939725]
[-2.71414169 -0.17700123]
...
[ 1.56939475 -0.55628366]]
3. Implement a K-Nearest Neighbors (KNN) classifier to classify species in the Iris dataset.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
output:
Predicted labels: [1 0 2 ...]
Accuracy: 0.97
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
output:
Predicted labels: [1 0 2 ...]
Accuracy: 0.97
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
output:
Accuracy: 0.9777777777777777
Classification Report:
precision recall f1-score support
accuracy 0.98 45
macro avg 0.98 0.98 0.98 45
weighted avg 0.98 0.98 0.98 45
Confusion Matrix:
[[16 0 0]
[ 0 16 0]
[ 0 1 12]]
# Load dataset
iris = load_iris()
X = iris.data
y = iris.target
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
7. Implement K-Means clustering to group similar data points in the Iris dataset.
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
import pandas as pd
# Load dataset
iris = load_iris()
X = iris.data
Output:
Cluster Centers: [[5.006 3.418 1.464 0.244]
[5.901 2.748 4.393 1.433]
[6.85 3.073 5.742 2.071]]
Labels: [0 0 1 2 ...]