
Multi-Class Classification - Logistic Regression
This simple code shows how to use Logistic regression for multi-class classification
using the iris dataset.
The dataset contains 150 samples for 3 different types of irises.
"""
This simple code shows how to use Logistic regression for multi-class classification
using the iris dataset (source: https://archive.ics.uci.edu/ml/datasets/iris).
The dataset contains 150 samples for 3 different types of irises (Setosa, Versicolour
and Virginica).
The rows are the samples and the columns are: Sepal Length, Sepal Width, Petal Length
and Petal Width.
"""
# import libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
# load the iris dataset
iris = datasets.load_iris()
# dimension of independent variables
print(iris.data.shape)
# dimension of target
print(iris.target.shape)
print()
# split the dataset into training and test
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.33, random_state=23)
# fit a logistic regression model to the data
model = LogisticRegression(solver='liblinear', multi_class='ovr')
model.fit(X_train, y_train)
# predict
actual = y_test
predicted = model.predict(X_test)
# model evaluation
# accuracy score
print("Accuracy score:", metrics.accuracy_score(actual, predicted))
print()
# classification report
print("Classification Report:")
print(metrics.classification_report(actual, predicted))
print()
# confusion matrix
print("Confusion Matrix:")
print(metrics.confusion_matrix(actual, predicted))
Comments
Mohammed
Apr 09Show me some thing
Bashua Mubarak
May 06Is there any way you can provide us with the results?
Michael
May 06@Mubarak and others who care: to view the output of the code above, just copy entire code and run on your machine's Jupyter notebook. Thanks!