Lesson 46 of 60 · python
Introduction to Machine Learning with Scikit‑learn
Duration: 20 minutes
Machine Learning Overview
Scikit‑learn (sklearn) is the most popular library for classical machine learning in Python.
Installing scikit‑learn
pip install scikit-learn
Typical ML workflow
- Define problem (regression, classification, clustering).
- Load & preprocess data (cleaning, scaling, encoding).
- Split into training and test sets.
- Select a model (e.g., LinearRegression).
- Train (
model.fit). - Evaluate (
metrics). - Tune hyperparameters.
- Deploy.
Example: Simple Linear Regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
X = df[['feature1', 'feature2']]
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
preds = model.predict(X_test)
rmse = mean_squared_error(y_test, preds, squared=False)
print('RMSE:', rmse)
Key concepts
- Estimator: any object with a
fitmethod. - Transformer: implements
fitandtransform(e.g.,StandardScaler). - Pipeline: chains transformers and an estimator.