fit() trains the model using input data and known categories.
predict() uses the trained model to guess categories for new data.
Examples
Train a decision tree to classify data into categories.
ML Python
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Use logistic regression to predict categories like 'yes' or 'no'.
ML Python
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Sample Model
This program trains a decision tree to classify iris flowers into species. It shows the predicted categories and how accurate the model is.
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load example data
iris = load_iris()
X, y = iris.data, iris.target
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create and train the model
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)
# Predict categories for test data
predictions = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)
print(f"Predictions: {predictions}")
print(f"Accuracy: {accuracy:.2f}")
OutputSuccess
Important Notes
Classification models learn from examples with known categories.
Accuracy shows how often the model guesses correctly.
Categories must be clearly defined before training.
Summary
Classification predicts which group new data belongs to.
It is useful for sorting and decision-making tasks.
Models learn by training on labeled examples.
Practice
(1/5)
1. Why does a classification model predict categories instead of numbers?
easy
A. Because it sorts data into distinct groups based on labels
B. Because it calculates the average value of data points
C. Because it predicts continuous values like temperature
D. Because it generates random outputs for data
Solution
Step 1: Understand classification purpose
Classification models are designed to assign inputs to specific categories or groups.
Step 2: Differentiate from regression
Regression models predict continuous numbers, while classification predicts labels or categories.
Final Answer:
Because it sorts data into distinct groups based on labels -> Option A
Quick Check:
Classification = category prediction [OK]
Hint: Classification = sorting into groups, not numbers [OK]
Common Mistakes:
Confusing classification with regression
Thinking classification predicts exact numbers
Assuming classification outputs random values
2. Which of the following is the correct way to define a classification model in Python using scikit-learn?
easy
A. model = KNeighborsClassifier()
B. model = LinearRegression()
C. model = RandomForestRegressor()
D. model = PCA()
Solution
Step 1: Identify classification model
KNeighborsClassifier is a classification model used to predict categories.
Step 2: Exclude non-classification models
LinearRegression and RandomForestRegressor are for regression; PCA is for dimensionality reduction.
Final Answer:
model = KNeighborsClassifier() -> Option A
Quick Check:
KNeighborsClassifier is classification model [OK]
Hint: Look for 'Classifier' in model name for classification [OK]
Common Mistakes:
Choosing regression models for classification tasks
Confusing PCA with classification
Using models without 'Classifier' suffix
3. Given this code snippet, what will be the predicted category for input X_new = [[5.1, 3.5]]?
With n_neighbors=1, the model predicts the label of the closest training point to X_new.
Step 2: Find closest training point
Distances: to [5.0,3.6] is small; label is 'A'. Others are farther with label 'B'.
Final Answer:
['A'] -> Option C
Quick Check:
Nearest neighbor label = 'A' [OK]
Hint: Nearest neighbor label is prediction with k=1 [OK]
Common Mistakes:
Ignoring nearest neighbor distance
Assuming majority vote with k=1
Confusing labels or input shapes
4. Identify the error in this classification code and fix it:
from sklearn.tree import DecisionTreeClassifier
X = [[1, 2], [3, 4]]
y = [0, 1]
model = DecisionTreeClassifier()
model.fit(X, y)
pred = model.predict([1, 2])
print(pred)
medium
A. No error, code runs fine
B. Change predict input to [[1, 2]]
C. Use DecisionTreeRegressor instead
D. Change y to [[0], [1]]
Solution
Step 1: Check input shape for predict
Model expects 2D array for predict; [1, 2] is 1D, causing error.
Step 2: Fix input shape
Wrap input in another list: [[1, 2]] to make it 2D.
Final Answer:
Change predict input to [[1, 2]] -> Option B
Quick Check:
Predict input must be 2D array [OK]
Hint: Predict input must be 2D array, even for one sample [OK]
Common Mistakes:
Passing 1D array to predict
Changing label shape unnecessarily
Switching to regression model incorrectly
5. You have a dataset with three categories: 'cat', 'dog', and 'rabbit'. You want to build a classification model that predicts these categories from features. Which approach best explains why classification predicts categories here?
hard
A. The model calculates the average feature values and outputs a number
B. The model predicts continuous values that represent category probabilities
C. The model randomly assigns categories without learning
D. The model learns patterns from labeled examples to assign new inputs to one of the three groups
Solution
Step 1: Understand classification learning
The model uses labeled data to learn how features relate to categories.
Step 2: Explain prediction process
It predicts the category by matching new input features to learned patterns for each group.
Final Answer:
The model learns patterns from labeled examples to assign new inputs to one of the three groups -> Option D
Quick Check:
Classification = learning patterns to predict categories [OK]
Hint: Classification learns from labels to predict groups [OK]
Common Mistakes:
Thinking classification outputs numbers directly
Assuming random assignment without training
Confusing classification with regression probabilities
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.