Machine Learning
Regression
Fit regressors and evaluate with RMSE and R2.
By EZ4Code Team
regression
Code
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
X, y = make_regression(n_samples=500, n_features=10, noise=10, random_state=42)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)
models = {
"linear": LinearRegression(),
"ridge": Ridge(alpha=1.0),
"rf": RandomForestRegressor(n_estimators=100, random_state=42),
}
for name, m in models.items():
m.fit(X_tr, y_tr)
pred = m.predict(X_te)
rmse = np.sqrt(mean_squared_error(y_te, pred))
r2 = r2_score(y_te, pred)
print(f"{name}: rmse={rmse:.3f} r2={r2:.3f}")Explanation
Regressors predict continuous targets and share the same fit/predict interface as classifiers. LinearRegression fits least squares, while Ridge adds L2 regularization to reduce overfitting on collinear features. RMSE reports error in target units and R-squared indicates the share of variance explained by the model.
More Machine Learning Snippets
Data Preprocessing
Scale, encode, and impute features with sklearn.
Train Test Split
Split data into training and evaluation sets.
Classification
Train and predict with common classifiers.
Clustering
Cluster with KMeans and DBSCAN.
Metrics
Evaluate classifiers with confusion matrix and reports.
Pipeline
Chain preprocessing and modeling with Pipeline.