Scikit-learn Basics
Importing Datasets
Scikit-learn provides several built-in toy datasets for learning and testing. Common datasets include load_iris(), load_digits(), load_wine(), and load_breast_cancer().
Example: Load the Iris Dataset
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.feature_names)
print(iris.target_names)
The load_iris() function returns a dictionary-like object with keys such as data, target, feature_names, and target_names.
Features and Targets
In machine learning, the features (also called independent variables) are the inputs to the model, while the target (also called labels or dependent variables) is the output we want to predict.
Accessing Features and Targets
X = iris.data # Features
y = iris.target # Target labels
print("Feature shape:", X.shape)
print("Target shape:", y.shape)
Here, X contains measurements like sepal length and petal width, while y contains the corresponding species classification (e.g., 0 = setosa, 1 = versicolor, 2 = virginica).
Train/Test Split
To evaluate a machine learning model, it is essential to split the dataset into a training set and a test set. Scikit-learn provides a function called train_test_split to do this.
Splitting 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.3, random_state=42)
print("Training set size:", X_train.shape)
print("Test set size:", X_test.shape)
The test_size parameter defines the proportion of the dataset to include in the test split. The random_state ensures reproducibility.
Basic Pipeline of a Machine Learning Model
A typical machine learning pipeline in Scikit-learn consists of the following steps:
- Load the dataset
- Split into training and testing sets
- Choose a model
- Train the model
- Make predictions
- Evaluate the model
Example: Classify Iris Flowers Using k-Nearest Neighbors
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Step 1: Load data (already done)
# Step 2: Split data (already done)
# Step 3: Choose and create the model
model = KNeighborsClassifier(n_neighbors=3)
# Step 4: Train the model
model.fit(X_train, y_train)
# Step 5: Make predictions
y_pred = model.predict(X_test)
# Step 6: Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print("Model accuracy:", accuracy)
In this example, we used the KNeighborsClassifier with 3 neighbors. The final accuracy tells us how well our model performs on the unseen test data.