Machine Learning Assignment-2
Machine Learning Assignment-2
ASSIGNMENT-2
1. What is linear regression ?
The equation of simple linear regression (with one independent variable) is:
Y= mX + C
where:
C is the intercept.
Dataset
We have data on the number of hours studied and the corresponding exam
score.
1 50
2 55
3 65
4 70
5 75
Program:
Output:
Slope (m): 6.25
Intercept (C): 43.75
Predicted values: [50. 56.25 62.5 68.75 75. ]
where:
Dataset
Pass (1) / Fail
Hours Studied
(0)
1 0
2 0
3 0
4 1
5 1
Program:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) # Independent variable
Y = np.array([0, 0, 0, 1, 1]) # Dependent variable (Pass/Fail)
model = LogisticRegression()
model.fit(X, Y)
3. What is PCA ?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
iris = load_iris()
X = iris.data # Features (4D)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
4. What is LDA ?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis(n_components=2)
X_lda = lda.fit_transform(X_scaled, y)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X, y)
X_test = np.linspace(0, 6, 100).reshape(-1, 1)
y_prob = model.predict_proba(X_test)[:, 1] # Probability of passing
plt.scatter(X, y, color='blue', label='Actual Data')
plt.plot(X_test, y_prob, color='red', label='Decision Boundary')
plt.xlabel("Hours Studied")
plt.ylabel("Probability of Passing")
plt.legend()
plt.show()