We use cookies to enhance your experience on the site
CodeWorlds

Scikit-learn - Classic ML

Scikit-learn is the most popular library for classic ML in Python!

Installation

1pip install scikit-learn

Classification Models

Decision Tree

1from sklearn.tree import DecisionTreeClassifier
2from sklearn.datasets import load_iris
3
4# Data
5iris = load_iris()
6X, y = iris.data, iris.target
7
8# Model
9tree = DecisionTreeClassifier(max_depth=3, random_state=42)
10tree.fit(X_train, y_train)
11
12# Evaluation
13accuracy = tree.score(X_test, y_test)
14
15# Tree visualization
16from sklearn.tree import plot_tree
17import matplotlib.pyplot as plt
18
19plt.figure(figsize=(20, 10))
20plot_tree(tree, feature_names=iris.feature_names, class_names=iris.target_names, filled=True)
21plt.show()

Random Forest

1from sklearn.ensemble import RandomForestClassifier
2
3# Model - a forest of random trees
4forest = RandomForestClassifier(
5    n_estimators=100,    # number of trees
6    max_depth=10,        # maximum depth
7    min_samples_split=5, # min samples to split
8    random_state=42
9)
10forest.fit(X_train, y_train)
11
12# Feature importance
13importance = forest.feature_importances_
14for name, imp in zip(feature_names, importance):
15    print(f"{name}: {imp:.4f}")

Support Vector Machine (SVM)

1from sklearn.svm import SVC
2
3# Model
4svm = SVC(
5    kernel='rbf',     # 'linear', 'poly', 'rbf', 'sigmoid'
6    C=1.0,            # regularization
7    gamma='scale',
8    probability=True  # enable predict_proba
9)
10svm.fit(X_train, y_train)
11
12# Predictions with probabilities
13proba = svm.predict_proba(X_test)

K-Nearest Neighbors (KNN)

1from sklearn.neighbors import KNeighborsClassifier
2
3# Model
4knn = KNeighborsClassifier(
5    n_neighbors=5,      # number of neighbors
6    weights='uniform',  # 'uniform' or 'distance'
7    metric='euclidean'
8)
9knn.fit(X_train, y_train)
10predictions = knn.predict(X_test)

Regression Models

Linear Regression

1from sklearn.linear_model import LinearRegression, Ridge, Lasso
2
3# Basic
4linear = LinearRegression()
5linear.fit(X_train, y_train)
6
7# Ridge - with L2 regularization
8ridge = Ridge(alpha=1.0)
9ridge.fit(X_train, y_train)
10
11# Lasso - with L1 regularization (can zero out features)
12lasso = Lasso(alpha=0.1)
13lasso.fit(X_train, y_train)
14
15# Coefficients
16print(f"Coefficients: {linear.coef_}")
17print(f"Intercept: {linear.intercept_}")

Random Forest Regressor

1from sklearn.ensemble import RandomForestRegressor
2
3# Model
4rf_reg = RandomForestRegressor(n_estimators=100, random_state=42)
5rf_reg.fit(X_train, y_train)
6predictions = rf_reg.predict(X_test)

Preprocessing

1from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder
2from sklearn.preprocessing import OneHotEncoder
3
4# Standardization (mean=0, std=1)
5scaler = StandardScaler()
6X_scaled = scaler.fit_transform(X_train)
7X_test_scaled = scaler.transform(X_test)
8
9# Normalization (0-1)
10minmax = MinMaxScaler()
11X_normalized = minmax.fit_transform(X_train)
12
13# Label Encoding
14le = LabelEncoder()
15y_encoded = le.fit_transform(['lion', 'elephant', 'cheetah'])
16
17# One-Hot Encoding
18ohe = OneHotEncoder(sparse_output=False)
19species_onehot = ohe.fit_transform(species_array.reshape(-1, 1))

Pipeline

1from sklearn.pipeline import Pipeline
2from sklearn.preprocessing import StandardScaler
3from sklearn.ensemble import RandomForestClassifier
4
5# Pipeline combines preprocessing with the model
6pipeline = Pipeline([
7    ('scaler', StandardScaler()),
8    ('classifier', RandomForestClassifier(n_estimators=100))
9])
10
11# Train the entire pipeline
12pipeline.fit(X_train, y_train)
13
14# Predict
15predictions = pipeline.predict(X_test)
Go to CodeWorlds