Getting Started with Scikit‑Learn
Scikit‑learn is a powerful, user‑friendly Python library for machine learning. This guide walks you through the essential steps to build your first model using scikit‑learn.
1️⃣ Install the library
pip install scikit-learn
2️⃣ Load a dataset
We’ll use the classic Iris dataset.
from sklearn import datasets
iris = datasets.load_iris()
X, y = iris.data, iris.target
3️⃣ Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
4️⃣ Choose a model
We'll train a simple Logistic Regression classifier.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=200)
5️⃣ Train the model
model.fit(X_train, y_train)
6️⃣ Evaluate performance
from sklearn.metrics import accuracy_score
pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, pred))
🚀 Next steps
- Explore data preprocessing techniques.
- Try other algorithms like Random Forest or K‑Means clustering.
- Read about model evaluation metrics to fine‑tune your models.