0% found this document useful (0 votes)
61 views6 pages

8 - Logistic - Regression - Multiclass - Ipynb - Colaboratory

This document demonstrates using logistic regression for multiclass classification with the digits dataset. It loads and visualizes the data, trains a logistic regression model on a train-test split, measures an accuracy of 93% on the test set, and generates a confusion matrix to evaluate predictions. It then provides an exercise to apply a similar workflow using logistic regression on the iris dataset to classify flowers into three categories based on sepal and petal features.

Uploaded by

duryodhan sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views6 pages

8 - Logistic - Regression - Multiclass - Ipynb - Colaboratory

This document demonstrates using logistic regression for multiclass classification with the digits dataset. It loads and visualizes the data, trains a logistic regression model on a train-test split, measures an accuracy of 93% on the test set, and generates a confusion matrix to evaluate predictions. It then provides an exercise to apply a similar workflow using logistic regression on the iris dataset to classify flowers into three categories based on sepal and petal features.

Uploaded by

duryodhan sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Logistic Regression: Multiclass Classification

In this tutorial we will see how to use logistic regression for multiclass classification.

from sklearn.datasets import load_digits
%matplotlib inline
import matplotlib.pyplot as plt
digits = load_digits()

plt.gray() 
for i in range(5):
    plt.matshow(digits.images[i]) 
<matplotlib.figure.Figure at 0x244baced518>

dir(digits)

['DESCR', 'data', 'images', 'target', 'target_names']

digits.data[0]

array([ 0., 0., 5., 13., 9., 1., 0., 0., 0., 0., 13.,

15., 10., 15., 5., 0., 0., 3., 15., 2., 0., 11.,

8., 0., 0., 4., 12., 0., 0., 8., 8., 0., 0.,

5., 8., 0., 0., 9., 8., 0., 0., 4., 11., 0.,

1., 12., 7., 0., 0., 2., 14., 5., 10., 12., 0.,

0., 0., 0., 6., 13., 10., 0., 0., 0.])

Create and train logistic regression model

from sklearn.linear_model import LogisticRegression

model = LogisticRegression()

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(digits.data,digits.target, tes

model.fit(X_train, y_train)

LogisticRegression(C=1.0, class_weight=None, dual=False,


fit_intercept=True,

intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,

penalty='l2', random_state=None, solver='liblinear', tol=0.0001,

verbose=0, warm_start=False)

Measure accuracy of our model

model.score(X_test, y_test)

0.93333333333333335

model.predict(digits.data[0:5])

array([0, 1, 2, 3, 4])

Confusion Matrix

y_predicted = model.predict(X_test)

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_predicted)

cm

array([[31, 0, 0, 0, 0, 1, 0, 0, 0, 0],

[ 0, 47, 0, 0, 0, 0, 0, 0, 1, 0],

[ 0, 0, 34, 1, 0, 0, 0, 0, 1, 0],

[ 0, 0, 0, 35, 0, 0, 0, 1, 4, 2],

[ 0, 2, 0, 0, 29, 0, 0, 0, 0, 0],

[ 0, 0, 0, 1, 0, 32, 0, 1, 0, 0],

[ 0, 0, 0, 0, 0, 0, 41, 0, 1, 0],

[ 0, 0, 0, 1, 0, 0, 0, 32, 0, 1],

[ 0, 4, 0, 0, 0, 0, 0, 0, 29, 0],

[ 0, 0, 0, 1, 1, 0, 0, 0, 0, 26]], dtype=int64)

import seaborn as sn

plt.figure(figsize = (10,7))

sn.heatmap(cm, annot=True)

plt.xlabel('Predicted')
plt.ylabel('Truth')

Text(69,0.5,'Truth')

Exercise

Use sklearn.datasets iris flower dataset to train your model using logistic regression.
You need to figure out accuracy of your model and use that to predict different samples
in your test dataset. In iris dataset there are 150 samples containing following features,

1. Sepal Length
2. Sepal Width
3. Petal Length
4. Petal Width

Using above 4 features you will clasify a flower in one of the three categories,

1. Setosa
2. Versicolour
3. Virginica

Colab paid products


-
Cancel contracts here

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy