Introduction
Linear regression helps us find a straight line that best fits data points, so we can predict one value from another easily.
y = b0 + b1 * x
y = 2 + 3 * x
y = 5 - 0.5 * x
from sklearn.linear_model import LinearRegression import numpy as np # Sample data: hours studied vs exam score X = np.array([[1], [2], [3], [4], [5]]) # hours y = np.array([2, 4, 5, 4, 5]) # scores # Create and train the model model = LinearRegression() model.fit(X, y) # Print the slope and intercept print(f"Slope (b1): {model.coef_[0]:.2f}") print(f"Intercept (b0): {model.intercept_:.2f}") # Predict score for 6 hours studied predicted_score = model.predict([[6]])[0] print(f"Predicted score for 6 hours: {predicted_score:.2f}")
y = 2x + 3, what is the predicted value when x = 4?def predict(x):
return 2 * x +
print(predict(5))Pass the test and this pattern moves unknown