5/25/25, 7:23 AM Hours_Studied.
ipynb - Colab
keyboard_arrow_down Model (C)
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
keyboard_arrow_down Step 1: Create the dataset
data = {
'Hours_Studied': [1.5, 3.0, 4.5, 6.0, 7.5],
'Test_Score': [55, 65, 75, 85, 95]
}
df = pd.DataFrame(data)
keyboard_arrow_down Step 2: Define the input (X) and output (y)
X = df[['Hours_Studied']] # 2D array
y = df['Test_Score'] # 1D array
keyboard_arrow_down Step 3: Create and train the model
model = LinearRegression()
model.fit(X, y)
▾ LinearRegression i ?
LinearRegression()
keyboard_arrow_down Step 4: Print the coefficients
print(f"Intercept (b): {model.intercept_}")
print(f"Slope (m): {model.coef_[0]}")
print(f"Equation: Test Score = {model.coef_[0]:.2f} * Hours Studied + {model.intercept_:.2f}")
Intercept (b): 45.0
Slope (m): 6.666666666666666
Equation: Test Score = 6.67 * Hours Studied + 45.00
keyboard_arrow_down Step 5: Predict values
df['Predicted_Score'] = model.predict(X)
keyboard_arrow_down Step 6: Plot the data and regression line (OPTIONAL)
plt.scatter(df['Hours_Studied'], df['Test_Score'], color='blue', label='Actual Scores')
plt.plot(df['Hours_Studied'], df['Predicted_Score'], color='red', label='Regression Line')
plt.title('Test Score vs Hours Studied')
plt.xlabel('Hours Studied')
plt.ylabel('Test Score')
plt.legend()
plt.grid(True)
plt.show()
https://colab.research.google.com/drive/1NS1N_HeVyzGUNlm3HgaRkYgLscXtay1w#printMode=true 1/2
5/25/25, 7:23 AM Hours_Studied.ipynb - Colab
https://colab.research.google.com/drive/1NS1N_HeVyzGUNlm3HgaRkYgLscXtay1w#printMode=true 2/2