python
16 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
models = {
'linear': LinearRegression(),
'ridge': Ridge(alpha=1.0),
'lasso': Lasso(alpha=0.01),
'elastic_net': ElasticNet(alpha=0.01, l1_ratio=0.5),
}
for name, model in models.items():
model.fit(X_train, y_train)
predictions = model.predict(X_valid)
mae = mean_absolute_error(y_valid, predictions)
rmse = root_mean_squared_error(y_valid, predictions)
print(name, round(mae, 3), round(rmse, 3))
1 file · python
Explain with highlit
For numeric targets I usually start simple and make regularization earn its keep. Ridge is stable, Lasso helps with sparsity, and ElasticNet is a practical compromise when correlated features exist. The main goal is not just minimizing RMSE but understanding which variables carry usable signal.
Related snips
python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
Encoding categorical variables without creating leakage
categorical-encoding
preprocessing
scikit-learn
by Dr. Elena Vasquez
1 tab
python
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
grid_search = GridSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_grid={
Hyperparameter tuning with GridSearchCV and randomized search
hyperparameter-tuning
gridsearch
scikit-learn
by Dr. Elena Vasquez
1 tab
python
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title='Churn Prediction API')
Serving scikit-learn models behind a FastAPI prediction API
fastapi
scikit-learn
model-serving
by Dr. Elena Vasquez
1 tab
python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report
pipeline = Pipeline([
Text vectorization with TF-IDF for strong classical baselines
tf-idf
nlp
text-classification
by Dr. Elena Vasquez
1 tab
python
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import RandomForestClassifier
ColumnTransformer pipelines that keep preprocessing honest
scikit-learn
pipelines
columntransformer
by Dr. Elena Vasquez
1 tab
python
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_validate
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
Train test split and stratified cross validation done properly
cross-validation
evaluation
scikit-learn
by Dr. Elena Vasquez
1 tab
Share this code
Here's the card — post it anywhere.