Untitled

 avatar
unknown
plain_text
23 days ago
1.1 kB
5
Indexable
Linear regression using skicitLearn

Program 1:

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
Y = np.array([2, 4, 5, 4, 5])

model = LinearRegression()
model.fit(X, Y)

print("Intercept:", model.intercept_)
print("Slope:", model.coef_[0])

x_new = np.array([[6]])
y_pred = model.predict(x_new)

print("Prediction for x = 6:", y_pred[0])

Output:

Intercept: 2.1999999999999993
Slope: 0.6000000000000002
Prediction for x = 6: 5.800000000000001



 Modified Program 2:

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([2, 4, 5, 8, 10]).reshape(-1, 1)
Y = np.array([1, 2, 4, 7, 8])

model = LinearRegression()
model.fit(X, Y)

print("Intercept:", model.intercept_)
print("Slope:", model.coef_[0])

y_new = np.array([[12]])
x_pred = model.predict(y_new)

print("Prediction for y = 12:", x_pred[0])

Output:

Intercept: -1.0588235294117654
Slope: 0.9411764705882355
Prediction for y = 12: 10.235294117647062

Editor is loading...
Leave a Comment