Machine Learning with Scikit-Learn
103 words
1 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Machine Learning with Scikit-Learn [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Python Refresher**](/notes/04-degree-electives-bsda4001-ds-ai-lab-week01-01-python-refresher)[Next**Feature Engineering**](/notes/04-degree-electives-bsda4001-ds-ai-lab-week02-02b-feature-engineering)

Machine Learning with Scikit-Learn
pythonfrom sklearn.datasets import load_iris from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import classification_report # Load data iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # Preprocess scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # Model model = RandomForestClassifier(n_estimators=100) model.fit(X_train, y_train) # Evaluate y_pred = model.predict(X_test) print(classification_report(y_test, y_pred)) # Hyperparameter tuning param_grid = {'n_estimators': [50, 100, 200], 'max_depth': [3, 5, None]} grid = GridSearchCV(RandomForestClassifier(), param_grid, cv=5) grid.fit(X_train, y_train) print(f"Best params: {grid.best_params_}")