Coursera Machine Learning Challenges in Python: ex3 (Handwritten Number Recognition with Logistic Regression)

Introduction

Coursera Machine Learning has become the world's leading introduction to machine learning. This is the third in a series of trying to implement in Python after studying the programming tasks of Matlab / Octave.

This time, in the first half of ex3, the task of recognizing handwritten numbers using logistic regression. The dataset is a subset of MNIST, which is given 5000 20x20 pixel grayscale images in the Matlab / Octave .mat data format. In fact, scikit-learn has a function called fetch_mldata (), which also allows you to download MNIST data (28x28 pixels, 70,000 sheets) (see this article: [Handwriting MNIST with Multilayer Perceptron] Number recognition](http://aidiary.hatenablog.com/entry/20140205/1391601418), but this time I will use the above .mat data for comparison.

The latter half of ex3 is a little half-finished content that only the forward propagation part of the neural network is created, so I will omit it.

code

The data is also well-formed and it's simple code as it just uses scikit-learn's LogisticRegression class. Matlab's .mat format data can be read using Scipy's scipy.io.loadmat () function.

ex3.py


import numpy as np
import matplotlib.pyplot as plt
import scipy.io as scio
from sklearn import linear_model

# scipy.io.loadmat()Load matlab data using
data = scio.loadmat('ex3data1.mat')
X = data['X']  #X is a 5000x400 matrix
y = data['y'].ravel()  #y is 5000 x 1 matrix, ravel()Convert to 5000 dimensional vector using

model = linear_model.LogisticRegression(penalty='l2', C=10.0) #Model definition
model.fit(X,y)    #Learning with training data
model.score(X,y)  #Correct answer rate in training data

When executed, the correct answer rate for character recognition in the training data was displayed as 0.96499999999999997.

Machine learning points

The parameter $ \ lambda $, which indicates the strength of regularization, was $ \ lambda = 0.1 $ in Coursera. As introduced in the previous article, in the sklearn.linear_model.LogisticRegression class, the regularization parameter is specified by $ C $ (corresponding to the reciprocal of $ \ lambda $), so this time the model is set as C = 10.0. Defined.

As a result, the correct answer rate in the training data was 96.5% as mentioned above. The result with Matlab / Octave was 94.9%, so is it a little overfit? I'm not sure why.

Other points

This alone is too easy, so I wrote a code to display the data that was misrecognized. When training with the above model, 175 out of 5000 training data are incorrectly determined. I will display the label (how I made a mistake) along with the image for 25 randomly selected ones.

ex3-wrong.py


wrong_index = np.array(np.nonzero(np.array([model.predict(X) != y]).ravel())).ravel()
wrong_sample_index = np.random.randint(0,len(wrong_index),25)
fig = plt.figure()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=0.5)
for i in range(0,25):
    ax = fig.add_subplot(5,5,i+1)
    ax.axis('off')
    ax.imshow(X[wrong_index[wrong_sample_index[i]]].reshape(20,20).T, cmap = plt.get_cmap('gray'))
    ax.set_title(str(model.predict(X[wrong_index[wrong_sample_index[i]]])[0]))
plt.show()

The result is as follows. ex3-2.png

There are some convincing mistakes, such as mistaken 4 for 9, and vice versa, but there are also others that are not. Well, it may be something like this because I just logistically regressed the pixel data without extracting any features. It may be a little overfitting than that, so it seems that Coursera should select the appropriate regularization parameter $ C $ by using Cross Validation etc. that will appear in a later module.

Recommended Posts

Coursera Machine Learning Challenges in Python: ex3 (Handwritten Number Recognition with Logistic Regression)
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Coursera Machine Learning Challenges in Python: ex7-1 (Image compression with K-means clustering)
Coursera Machine Learning Challenges in Python: ex7-2 (Principal Component Analysis)
Coursera Machine Learning Challenges in Python: ex5 (Adjustment of Regularization Parameters)
Coursera Machine Learning Challenges in Python: ex6 (How to Adjust SVM Parameters)
Number recognition in images with Python
Machine learning with python (2) Simple regression analysis
Python & Machine Learning Study Memo ⑥: Number Recognition
Machine learning logistic regression
Machine learning with Python! Preparation
Beginning with Python machine learning
Machine learning algorithm (logistic regression)
Machine learning with python (1) Overall classification
Classification and regression in machine learning
Python: Preprocessing in Machine Learning: Overview
Logistic regression analysis Self-made with python
"Scraping & machine learning with Python" Learning memo
"Gaussian process and machine learning" Gaussian process regression implemented only with Python numpy
<Course> Machine Learning Chapter 3: Logistic Regression Model
Amplify images for machine learning with python
[python] Frequently used techniques in machine learning
Python: Preprocessing in machine learning: Data acquisition
[Shakyo] Encounter with Python for machine learning
I implemented Cousera's logistic regression in Python
[Python] Saving learning results (models) in machine learning
Python: Preprocessing in machine learning: Data conversion
Build AI / machine learning environment with Python
Align the number of samples between classes of data for machine learning with Python
[Python] Easy introduction to machine learning with python (SVM)
Machine learning starting with Python Personal memorandum Part2
Machine learning starting with Python Personal memorandum Part1
EV3 x Python Machine Learning Part 2 Linear Regression
[Python] Collect images with Icrawler for machine learning [1000 images]
[Python3] Let's analyze data using machine learning! (Regression)
Get a glimpse of machine learning in Python
I started machine learning with Python Data preprocessing
Build a Python machine learning environment with a container
Learn collaborative filtering along with Coursera Machine Learning materials
Run a machine learning pipeline with Cloud Dataflow (Python)
Tool MALSS (application) that supports machine learning in Python
Tool MALSS (basic) that supports machine learning in Python
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Build a machine learning application development environment with Python
Summary of the basic flow of machine learning with Python
Attempt to include machine learning model in python package
Cross-entropy to review in Coursera Machine Learning week 2 assignments
MALSS, a tool that supports machine learning in Python
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
[Machine learning] Write the k-nearest neighbor method (k-nearest neighbor method) in python by yourself and recognize handwritten numbers.
Logistic distribution in Python
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Learning Python with ChemTHEATER 05-1
Learning Python with ChemTHEATER 02
Machine learning linear regression
Speech recognition in Python
Learning Python with ChemTHEATER 01
Python: Supervised Learning (Regression)
Prime number 2 in Python