Linear Regression with scikit-learn
Train a model on (X, y) data, predict on new X, score with MSE + R². The 'hello world' of ML.
🚀 Mini Project · 3 filesProject files
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import pickle, matplotlib.pyplot as plt
# Generate synthetic data: y = 3x + 2 + noise
np.random.seed(42)
X = np.random.uniform(0, 10, size=(100, 1))
y = 3 * X.ravel() + 2 + np.random.normal(0, 1, 100)
# Split into train (80%) + test (20%)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)
# Train
model = LinearRegression()
model.fit(X_tr, y_tr)
print(f'coef={model.coef_}, intercept={model.intercept_:.3f}')
# Predict + score
preds = model.predict(X_te)
print(f'MSE = {mean_squared_error(y_te, preds):.3f}')
print(f'R² = {r2_score(y_te, preds):.3f}')
# Save the model to a pickle file (this is what OpenBenchML uploads)
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
print('saved model.pkl')
# Plot
plt.scatter(X_te, y_te, label='actual', alpha=0.6)
plt.plot(X_te, preds, 'r-', label='predicted')
plt.legend(); plt.xlabel('X'); plt.ylabel('y')
plt.savefig('plot.png', dpi=100, bbox_inches='tight')
import pickle, numpy as np
# Load the saved model
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
# Predict on new data
X_new = np.array([[5.0], [7.5], [10.0]])
print('predictions:', model.predict(X_new))
pip install scikit-learn numpy matplotlib python train.py # coef=[2.99], intercept=2.02 # MSE = 0.81, R² = 0.97 # saved model.pkl python predict.py # predictions: [17.0 24.5 32.0]
How it works
Linear regression fits a line y = mx + b to the data. The model finds m (coef_) and b (intercept_) that minimise the mean squared error between predictions and actual y. Steps: (1) Generate synthetic data — here y = 3x + 2 + noise, so a good model should recover m≈3, b≈2. (2) train_test_split holds out 20% of the data for honest evaluation — never score on training data, it's cheating. (3) model.fit(X_tr, y_tr) runs the actual learning. (4) model.predict(X_te) generates predictions. (5) mean_squared_error and r2_score measure quality. R²=0.97 means the model explains 97% of the variance — excellent. (6) pickle.dump saves the trained model to a file. This .pkl file is exactly what OpenBenchML's upload page accepts — you train locally, upload the pickle, and the platform benchmarks it against a holdout set.
⚡ Open in Notebook