0
0
ML Pythonml~5 mins

Multi-class classification in ML Python

Choose your learning style9 modes available
Introduction
Multi-class classification helps us teach a computer to sort things into more than two groups, like sorting fruits into apples, bananas, or oranges.
When you want to recognize handwritten digits from 0 to 9.
When sorting emails into categories like work, personal, or spam.
When identifying the type of animal in a photo among cats, dogs, and birds.
When classifying news articles into topics such as sports, politics, or technology.
Syntax
ML Python
model = SomeClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
y_train contains labels with more than two classes, like 0, 1, 2, etc.
The model learns to pick one class from many possible classes.
Examples
Using logistic regression for multi-class classification by setting multi_class='multinomial'.
ML Python
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(multi_class='multinomial', solver='lbfgs')
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Decision trees naturally handle multiple classes without extra settings.
ML Python
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Sample Model
This program trains a logistic regression model to classify iris flowers into three species. It prints the predicted classes and the accuracy score.
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Load iris flower data with 3 classes
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 logistic regression model for multi-class
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)

# Train the model
model.fit(X_train, y_train)

# Predict on 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
Multi-class classification means more than two classes, unlike binary classification which has only two.
Some algorithms need special settings to handle multiple classes, like logistic regression.
Accuracy is a simple way to check how well the model sorts the data into correct classes.
Summary
Multi-class classification sorts data into three or more groups.
It is useful for tasks like recognizing digits, animals, or topics.
Many machine learning models can handle multi-class problems with little or no changes.