image_output_2.png image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import numpy as np

import numpy as np

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)
model = LinearRegression()
model.fit(x, y)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(123)
x = np.linspace(0, 10, 20)
y = 3 * x + 2 + np.random.normal(scale=2, size=len(x))
model = LinearRegression()
model.fit(x.reshape(-1, 1), y)
y_pred = model.predict(x.reshape(-1, 1))
residuals = y - y_pred

SSR = np.sum(residuals ** 2)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)
model = LinearRegression()
model.fit(x, y)
sse = np.sum((y - model.predict(x)) ** 2)
residuals = y - model.predict(x)
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].scatter(x, y, s=10)
axs[0, 0].plot(x, model.predict(x), color='r')
axs[0, 0].set_xlabel('X')
axs[0, 0].set_ylabel('Y')
axs[0, 0].set_title('Original Regression Line')
model_high_slope = LinearRegression()
model_high_slope.fit(x, y + 0.5)
sse_high_slope = np.sum((y - model_high_slope.predict(x)) ** 2)
residuals_high_slope = y - model_high_slope.predict(x)
axs[0, 1].scatter(x, y, s=10)
axs[0, 1].plot(x, model_high_slope.predict(x), color='r')
axs[0, 1].set_xlabel('X')
axs[0, 1].set_ylabel('Y')
axs[0, 1].set_title('Line with Higher Slope')
model_low_slope = LinearRegression()
model_low_slope.fit(x, y - 0.5)
sse_low_slope = np.sum((y - model_low_slope.predict(x)) ** 2)
residuals_low_slope = y - model_low_slope.predict(x)
axs[1, 0].scatter(x, y, s=10)
axs[1, 0].plot(x, model_low_slope.predict(x), color='r')
axs[1, 0].set_xlabel('X')
axs[1, 0].set_ylabel('Y')
axs[1, 0].set_title('Line with Lower Slope')
model_diff_intercept = LinearRegression()
model_diff_intercept.fit(x, y + 1)
sse_diff_intercept = np.sum((y - model_diff_intercept.predict(x)) ** 2)
residuals_diff_intercept = y - model_diff_intercept.predict(x)
axs[1, 1].scatter(x, y, s=10)
axs[1, 1].plot(x, model_diff_intercept.predict(x), color='r')
axs[1, 1].set_xlabel('X')
axs[1, 1].set_ylabel('Y')
axs[1, 1].set_title('Line with Different Intercept')
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].scatter(model.predict(x), residuals, s=10)
axs[0, 0].set_xlabel('Y Predicted')
axs[0, 0].set_ylabel('Residuals')
axs[0, 0].set_title('Residuals for Original Line')
axs[0, 1].scatter(model_high_slope.predict(x), residuals_high_slope, s=10)
axs[0, 1].set_xlabel('Y Predicted')
axs[0, 1].set_ylabel('Residuals')
axs[0, 1].set_title('Residuals for Line with Higher Slope')
axs[1, 0].scatter(model_low_slope.predict(x), residuals_low_slope, s=10)
axs[1, 0].set_xlabel('Y Predicted')
axs[1, 0].set_ylabel('Residuals')
axs[1, 0].set_title('Residuals for Line with Lower Slope')
axs[1, 1].scatter(model_diff_intercept.predict(x), residuals_diff_intercept, s=10)
axs[1, 1].set_xlabel('Y Predicted')
axs[1, 1].set_ylabel('Residuals')
axs[1, 1].set_title('Residuals for Line with Different Intercept')
plt.tight_layout()
plt.show()