Exp 1 121a1047 Lavanya Kurup ML
Exp 1 121a1047 Lavanya Kurup ML
Exp 1 121a1047 Lavanya Kurup ML
ROLL NO : 121A1047
C3
THEORY :
Key Concepts
Applications
Model Equation
Where:
values: minimize∑i=1n(Yi−(β^0+β^1Xi1+β^2Xi2+⋯
the squared differences between observed values and predicted
CODE:
1. LINEAR REGRESSION
Using Formula
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
loc = "/content/Salary_Data.csv"
df = pd.read_csv(loc)
X = df.iloc[:, 0]
y = df.iloc[:, 1]
mean_X = np.mean(X)
mean_y = np.mean(y)
n = len(X)
numer = 0
denom = 0
for i in range(n):
numer += (X[i] - mean_X) * (y[i] - mean_y)
denom += (X[i] - mean_X) ** 2
b1 = numer / denom
b0 = mean_y - (b1 * mean_X)
Using SkLearn
import pandas as pd
import numpy as np
import sklearn.metrics as sm
loc = "/content/Salary_Data.csv"
df = pd.read_csv(loc)
df.head()
print(df.isnull().sum())
df.dropna(inplace=True)
x = df['YearsExperience']
y = df['Salary']
x_train=np.array(x_train).reshape(len(x_train),1)
x_test=np.array(x_test).reshape(len(x_test),1)
y_train=np.array(y_train).reshape(len(y_train),1)
y_test=np.array(y_test).reshape(len(y_test),1)
model=LinearRegression()
model.fit(x_train,y_train)
y_train_pred=model.predict(x_train)
plt.figure()
y_test_pred=model.predict(x_test)
plt.figure()
plt.xlabel("years of Experience")
plt.ylabel("Salary")
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(0)
X1 = 2 * np.random.rand(100, 1)
X2 = 3 * np.random.rand(100, 1)
X = np.hstack((X1, X2))
y = 4 + 3*X1 + 2*X2 + np.random.randn(100, 1)
model_X1 = LinearRegression()
model_X1.fit(X1, y)
model_X2 = LinearRegression()
model_X2.fit(X2, y)
y_pred_X1 = model_X1.predict(X1)
y_pred_X2 = model_X2.predict(X2)
plt.figure(figsize=(10, 6))
plt.show()
OUTPUT:
1. LINEAR REGRESSION:
USING FORMULA: