MNIST Single Predict

MNIST Single Prediction

We have set up a very simple SVC to classify the MNIST digits to make one single shoot predict. First we load the libraries and the dataset:

A notebook you find at:

https://github.com/maxkleiner/maXbox4/blob/master/MNISTSinglePredict.ipynb

#sign:max: MAXBOX8: 13/03/2021 07:46:37 
import numpy as np
import matplotlib.pyplot as plt
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn import datasets
from sklearn.metrics import accuracy_score

# [height, weight, 8*8 pixels of digits 0..9]
dimages = datasets.load_digits()
print(type(dimages), len(dimages.data), 'samples')
<class 'sklearn.utils.Bunch'> 1797 samples

Then we setup the Support Vector Classifier with the training data X and the target y:

sclf = SVC(gamma=0.001, C=100, kernel='linear')

X= dimages.data[:-10]
y= dimages.target[:-10]
print('train set samples:',len(X))
train set samples: 1787

Gamma is the learning rate and the higher the value of gamma the more precise the decision boundary would be. C (regularization) is the penalty of the fault tolerance. Having a larger C will lead to smaller values for the slack variables. This means that the number of support vectors will decrease. When you run the prediction, it will need to calculate the indicator function for each support vector. Now we train (fit) the samples:

sclf.fit(X,y)
SVC(C=100, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma=0.001, kernel='linear',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)

In the last step we predict a specific digit from the test set (only the last 10 samples are unseen), means we pass an actual image and SVC makes the prediction of which digit belongs to the image:

you can choose the testimage by yourself -6 means the 6.th last of the dataset.

testimage = -6

s_prediction = sclf.predict([dimages.data[testimage]])
print ('the image maybe belongs to ',s_prediction)
plt.imshow(dimages.images[testimage], cmap=plt.cm.gray_r, interpolation="nearest")
plt.show()
the image maybe belongs to  [4]
image 4 jupyter

The same fit we try with a Random Forest Classifier to finish this lesson:

#RandomForestClassifier
rfc_clf = RandomForestClassifier()
rfc_clf.fit(X,y)
rfc_prediction = rfc_clf.predict([dimages.data[testimage]])
print ('predict with RFC ',rfc_prediction)
predict with RFC  [4]
BPM 72
#92
metrics of a binary classifier
Classifier Overview

did a direct import from wiki with the press freedom index

>>> df.head(9).iloc[:,[0,1,2,9,10]]
>>> df.sort_values(by=[‘Rank2020’],inplace=True,ascending=True)

RangeIndex: 180 entries, 0 to 179
Data columns (total 14 columns):
# Column Non-Null Count Dtype
— —— ————– —–
0 ISO 180 non-null object
1 Rank2020 180 non-null int64
2 FR_Country 180 non-null object
3 EN_country 180 non-null object
4 ES_country 180 non-null object
5 Score A 180 non-null object
6 Sco Exa 180 non-null object
7 Score 2020 180 non-null object
8 Progression RANK 180 non-null int64
9 Rank 2019 180 non-null int64
10 Score 2019 180 non-null object
11 Zone 180 non-null object
12 AR_country 180 non-null object
13 FA_country 180 non-null object
dtypes: int64(3), object(11)
memory usage: 19.8+ KB

df.sort_values(by=[‘Rank2020’], inplace=True, ascending=True)

df.head(9).iloc[:,[0,1,2,9,10]]

     ISO  Rank2020        FR_Country  Rank 2019 Score 2019
     ISO  Rank2020        FR_Country  Rank 2019 Score 2019
120  NOR         1           Norvège          1       7,82
53   FIN         2          Finlande          2        7,9
44   DNK         3          Danemark          5       9,87
150  SWE         4             Suede          3       8,31
119  NLD         5          Pays-Bas          4       8,63
80   JAM         6          Jamaïque          8      11,13
37   CRI         7        Costa Rica         10      12,24
27   CHE         8            Suisse          6      10,52
122  NZL         9  Nouvelle-Zélande          7      10,75

The story of a parcel tracing (from 9.2- 25.2) analysis:

If you want to ask post.ch the response from the remote server was:

550 #5.1.0 Address rejected.

  • 25. Feb 2021 12:11 Erfolgreiche Zustellung
  • Voraussichtliche Zustellung
  • am 25.02.2021 zwischen 11.00 und 15.00 Uhr
    • 23. Feb 2021 11:35 Beladung
    • 23. Feb 2021 11:30 Zollfreigabe
    • 16. Feb 2021 14:55 Weiterleitung zur Exportbearbeitung
    • 16. Feb 2021 14:55 Entladung
    • 16. Feb 2021 14:55
    • 11. Feb 2021 7:34 Beladung
    • 10. Feb 2021 23:39 Weiterleitung zur Exportbearbeitung
    • 9. Feb 2021 18:41 Weiterleitung zur Exportbearbeitung
    • 9. Feb 2021 16:06 Filiale / Agentur
    • 9. Feb 2021 16:45 Sendungsnummer angegeben

Dienstag, 23. Februar 2021 12:00 Datenübermittlung durch Versender 11:35
Sendung wurde sortiert und weitergeleitet 897000 CH-8970 Urdorf EO

Dienstag, 16. Februar 2021 14:55 Verzollungsprozess 897000 CH-8970 Urdorf EO
14:55 Ankunft Bestimmungsland 897000 CH-8970 Urdorf EO
Donnerstag, 11. Februar 2021 07:34 Abgang Grenzstelle Aufgabeland IFS Speyer

16. Feb 2021 14:55 Weiterleitung zur Exportbearbeitung

16. Feb 2021 14:55 Entladung

11. Feb 2021 7:34 Beladung

10. Feb 2021 23:39 Weiterleitung zur Exportbearbeitung

9. Feb 2021 18:41 Weiterleitung zur Exportbearbeitung

9. Feb 2021 16:06 Filiale / Agentur

9. Feb 2021  16:45  Sendungsnummer angegeben

Transportation to destination country/destination area.

Destination country/region: Switzerland

International shipment number: CY542939469DE
Th, 11.02.2021, 07:34 hours
NOTE: As soon as the shipment arrives in the destination country/destination area, you will receive updated information in the detailed tracking history.

Tu, 16.02.2021, 14:55
Shipment is prepared for customs clearance in the destination country/destination area

Tu, 16.02.2021, 14:55
The shipment has arrived in the destination country/destination area
Th, 11.02.2021, 07:34, Speyer, Germany
The shipment will be transported to the destination country/destination area and, from there, handed over to the delivery organization. (Homepage / online shipment tracking: http://www.post.ch)
We, 10.02.2021, 23:39, Speyer, Germany
The international shipment is being prepared for onward transport.
Tu, 09.02.2021, 18:41, Rüdersdorf, Germany
The international shipment has been processed in the parcel center of origin
Tu, 09.02.2021, 16:06 The shipment has been posted by the sender at the retail outlet

Published by maxbox4

Code till the End

Leave a comment

Design a site like this with WordPress.com
Get started