Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
904 B
1
Indexable
Never

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split


np.random.seed(42)
X = 2 * np.random.rand(100, 2)

y = 4 + 3 * X[:, 0] + 2 * X[:, 1] + np.random.randn(100)


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


model = LinearRegression()


model.fit(X_train, y_train)


y_pred = model.predict(X_test)


print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)


plt.scatter(X_train[:, 0], y_train, color='blue', label='Training Data')
plt.scatter(X_test[:, 0], y_test, color='red', label='Test Data')


x_line = np.linspace(0, 2, 100) 
y_line = model.coef_[0] * x_line + model.intercept_
plt.plot(x_line, y_line, color='black', linewidth=3, label='Fitted Line (Feature 1)')

plt.xlabel('Feature 1')
plt.ylabel('y')
plt.legend()
plt.show()
Leave a Comment