Utilizziamo i cookie per migliorare la tua esperienza sul sito
CodeWorlds

Scikit-learn - klasyczne ML

Scikit-learn to najpopularniejsza biblioteka do klasycznego ML w Pythonie!

Instalacja

1pip install scikit-learn

Modele klasyfikacji

Decision Tree

1from sklearn.tree import DecisionTreeClassifier
2from sklearn.datasets import load_iris
3
4# Dane
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# Ocena
13accuracy = tree.score(X_test, y_test)
14
15# Wizualizacja drzewa
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 - las losowych drzew
4forest = RandomForestClassifier(
5    n_estimators=100,    # liczba drzew
6    max_depth=10,        # maksymalna głębokość
7    min_samples_split=5, # min próbek do podziału
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  # włącz predict_proba
9)
10svm.fit(X_train, y_train)
11
12# Przewidywanie z prawdopodobieństwami
13proba = svm.predict_proba(X_test)

K-Nearest Neighbors (KNN)

1from sklearn.neighbors import KNeighborsClassifier
2
3# Model
4knn = KNeighborsClassifier(
5    n_neighbors=5,      # liczba sąsiadów
6    weights='uniform',  # 'uniform' lub 'distance'
7    metric='euclidean'
8)
9knn.fit(X_train, y_train)
10predictions = knn.predict(X_test)

Modele regresji

Linear Regression

1from sklearn.linear_model import LinearRegression, Ridge, Lasso
2
3# Basic
4linear = LinearRegression()
5linear.fit(X_train, y_train)
6
7# Ridge - z regularyzacją L2
8ridge = Ridge(alpha=1.0)
9ridge.fit(X_train, y_train)
10
11# Lasso - z regularyzacją L1 (może zerować cechy)
12lasso = Lasso(alpha=0.1)
13lasso.fit(X_train, y_train)
14
15# Współczynniki
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# Standaryzacja (mean=0, std=1)
5scaler = StandardScaler()
6X_scaled = scaler.fit_transform(X_train)
7X_test_scaled = scaler.transform(X_test)
8
9# Normalizacja (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 łączy preprocessing z modelem
6pipeline = Pipeline([
7    ('scaler', StandardScaler()),
8    ('classifier', RandomForestClassifier(n_estimators=100))
9])
10
11# Trenuj cały pipeline
12pipeline.fit(X_train, y_train)
13
14# Przewiduj
15predictions = pipeline.predict(X_test)
Vai a CodeWorlds