Linear Regression in Python

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

# Generating some sample data
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Creating and fitting the model
model = LinearRegression()
model.fit(X, y)

# Predicting
X_new = np.array([[0], [2]])
y_predict = model.predict(X_new)

# Plotting
plt.scatter(X, y)
plt.plot(X_new, y_predict, color='red', linewidth=2)
plt.xlabel('X')
plt.ylabel('y')
plt.title('Simple Linear Regression')
plt.show()
from sklearn.model_selection import train_test_split

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Fitting the model on the training data
model.fit(X_train, y_train)

# Predicting on the test data
y_test_pred = model.predict(X_test)
from sklearn.metrics import mean_squared_error, r2_score

# Mean Squared Error
mse = mean_squared_error(y_test, y_test_pred)
print(f"Mean Squared Error: {mse}")

# R-squared
r2 = r2_score(y_test, y_test_pred)
print(f"R-squared: {r2}")
Mean Squared Error: 1.0434333815695171
R-squared: 0.7424452332071367
# Plotting training data and the model
plt.scatter(X_train, y_train, color='blue', label='Training data')
plt.plot(X_new, model.predict(X_new), color='red', linewidth=2, label='Regression line')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression Training Data')
plt.legend()
plt.show()

# Plotting test data and the model
plt.scatter(X_test, y_test, color='green', label='Test data')
plt.plot(X_new, model.predict(X_new), color='red', linewidth=2, label='Regression line')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression Test Data')
plt.legend()
plt.show()