scikit-learn: Machine learning in Python for beginners
In short: scikit-learn is the best-known Python library for classical machine learning and offers uniform tools for data, models, training and evaluation.
In more detail: Not every AI task needs a large neural network. For tables and smaller datasets classical methods such as linear regression, decision trees or random forests often suffice. scikit-learn (sklearn) provides them with a uniform interface.
In Depth
The typical workflow
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train) # train
prediction = model.predict(X_test) # predict
print(accuracy_score(y_test, prediction))Always the same pattern: fit (learn), predict (apply), score (evaluate). This holds for almost all models.
Important building blocks
- Models:
LinearRegression,LogisticRegression,DecisionTreeClassifier,RandomForest…,KNeighbors…,SVC,KMeans. - Preprocessing:
StandardScaler,OneHotEncoder,SimpleImputer(missing values). - Pipeline: chains steps (
make_pipeline(StandardScaler(), SVC())) and prevents data leakage (scaling learned from training data only). - Evaluation:
accuracy_score,f1_score,confusion_matrix,cross_val_score. - Search:
GridSearchCV,RandomizedSearchCVfor hyperparameters.
Good practice
Split data before anything else into training and test. Use cross-validation for reliable estimates. Check a simple baseline first (for example majority class or linear model) before using more complicated methods. Watch for imbalanced classes, data leakage and the meaning of the metric (accuracy alone deceives with rare events).
Limits and neighbours
scikit-learn is not meant for deep learning or GPUs: use PyTorch, TensorFlow/Keras, JAX for that. For boosting on tables XGBoost, LightGBM and CatBoost are popular. With pandas you load and clean the data, with matplotlib you plot results.
See also: Training, validation and test, Metrics, Ensemble methods, pandas, Hyperparameter search