Imagine you have a basket of fruits and you want to sort them into apples, oranges, and bananas. Why does a classification model predict categories instead of numbers?
Think about sorting items into named groups rather than measuring something.
Classification models are designed to assign inputs to distinct groups or categories, like labeling fruits as apples or oranges. They do not predict continuous values but discrete labels.
What is the output of this code that uses a simple classification model to predict the category of a flower?
from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier iris = load_iris() X, y = iris.data, iris.target model = DecisionTreeClassifier(random_state=0) model.fit(X, y) prediction = model.predict([[5.1, 3.5, 1.4, 0.2]]) print(prediction)
Look at the first sample in the iris dataset and its label.
The first iris sample belongs to class 0 (setosa). The model predicts the category label as 0.
You want to build a model that predicts if an email is spam or not (two categories). Which model is best suited for this classification task?
Think about models designed for predicting categories, not continuous values or unsupervised grouping.
Logistic Regression is a classification model that predicts categories like spam or not spam. Linear Regression predicts continuous values. K-Means and PCA are unsupervised methods.
You trained a classification model and want to know how often it predicts the correct category. Which metric should you use?
Think about a metric that counts correct predictions over total predictions.
Accuracy measures the proportion of correct category predictions out of all predictions. MSE and RMSE are for regression errors. R-squared measures variance explained in regression.
Consider this code snippet that tries to predict categories but raises an error. What is the cause?
from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier() model.fit([[1, 2], [3, 4]], [0, 1]) prediction = model.predict([1, 2]) print(prediction)
Check the shape of the input passed to the predict method.
The predict method expects a 2D array (list of samples). Passing a 1D list causes a shape error.
