|
| 1 | +#scikit classification example - golixco |
| 2 | +""" |
| 3 | +Advanced Example: Scikit-learn Classification |
| 4 | +
|
| 5 | +This script demonstrates using scikit-learn to perform a classification task. |
| 6 | +It loads a built-in dataset (breast cancer), preprocesses features, |
| 7 | +trains a logistic regression model, and evaluates performance on a test set. |
| 8 | +""" |
| 9 | +from sklearn.datasets import load_breast_cancer |
| 10 | +from sklearn.model_selection import train_test_split |
| 11 | +from sklearn.preprocessing import StandardScaler |
| 12 | +from sklearn.linear_model import LogisticRegression |
| 13 | +from sklearn.metrics import accuracy_score, classification_report |
| 14 | + |
| 15 | +# Load the dataset: features (X) and binary target (y) |
| 16 | +data = load_breast_cancer() |
| 17 | +X, y = data.data, data.target # Features and labels |
| 18 | + |
| 19 | +# Split into training and testing sets (70% train, 30% test) |
| 20 | +X_train, X_test, y_train, y_test = train_test_split( |
| 21 | + X, y, test_size=0.3, random_state=42) |
| 22 | + |
| 23 | +# Preprocess features by standardizing (mean=0, variance=1) |
| 24 | +scaler = StandardScaler() |
| 25 | +X_train_scaled = scaler.fit_transform(X_train) # fit on train, transform train |
| 26 | +X_test_scaled = scaler.transform(X_test) # use same transform on test |
| 27 | + |
| 28 | +# Train a logistic regression classifier |
| 29 | +clf = LogisticRegression() |
| 30 | +clf.fit(X_train_scaled, y_train) # fit model to training data |
| 31 | + |
| 32 | +# Predict on the test set |
| 33 | +y_pred = clf.predict(X_test_scaled) |
| 34 | + |
| 35 | +# Evaluate the classifier: accuracy and detailed report |
| 36 | +accuracy = accuracy_score(y_test, y_pred) |
| 37 | +print(f"Test Accuracy: {accuracy:.2f}") |
| 38 | +print(classification_report(y_test, y_pred, target_names=data.target_names)) |
| 39 | + |
0 commit comments