Python Machine Learning fun
Tags: python, machinelearning
Published: 2019-07-10 14:12:54

---
title: Python Machine Learning fun
published: true
description: python, machine learning
tags: python, machine learning
cover_image: https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fs3.amazonaws.com%2Ffiles.cnas.org%2Ftitle%2Fai-and-global-security-hero-allen.jpg%3Fmtime%3D20170720163102&f=1
---
Machine Learning is great. You can use data you already have and make all kinds of apps. 

Why data? Machine Learning algorithms use data. Without data, no Machine Learning. So what can you do with data?

One example is making predictions. It's hard to make intelligent machines if the algorithm is programmed, because the number of possible situations is greater than the programmers time.

<img src="https://media.giphy.com/media/lj11uKKMhr4Os/giphy.gif">

So you need to use data and train the algorithm.
The module sklearn is popular for making Machine Learning apps.

Oh blimey! Take notes

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxkmQfH1TB0dLW%2Fgiphy.gif&f=1">

### Machine Learning Algorithm

First load the data. Say you have the data in csv format. 
<a href="https://gist.githubusercontent.com/ktisha/c21e73a1bd1700294ef790c56c8aec1f/raw/819b69b5736821ccee93d05b51de0510bea00294/pima-indians-diabetes.csv">pima indians diabetes datset</a>

I took the indian diabetes set. But the principle works with any data set. Load it:

    #!/usr/bin/python3
    # load the CSV file as a numpy matrix                                                                                                                                               
    dataset = np.loadtxt("./pima-indians-diabetes.csv", delimiter=",")

    # separate the data from the target attributes                                                                                                                          
    X = dataset[:,0:7]
    y = dataset[:,8]

So you have X and y. You need to use an algorithm that uses this data and makes predictions

    model = LogisticRegression()
    model.fit(X, y)

What is fit? Fit is the algorithm learning from data. Ok.
Then you can make predictions:
                                                                                                                                             
    expected = y
    predicted = model.predict(X)

Because you know the expected and predicted, you can measure how well the algorithms predictions are.

![](https://thepracticaldev.s3.amazonaws.com/i/d2r4x2cs2dm9jv6lnuix.png)

### Run the App

Because we have X and y, you can see how well the predictor is doing.


    #!/usr/bin/python3
    from sklearn import metrics
    from sklearn.linear_model import LogisticRegression
    import numpy as np                                               
    
    # load the CSV file as a numpy matrix                                                                                                                                               
    dataset = np.loadtxt("./pima-indians-diabetes.csv", delimiter=",")
    
    # separate the data from the target attributes                                                                                                                          
    X = dataset[:,0:7]
    y = dataset[:,8]

    
    # make predictions                                                                                                                                                         
    expected = y
    predicted = model.predict(X)
    
    # summarize the fit of the model                                                                                                                                       
    print('RESULT')
    print(metrics.classification_report(expected, predicted))
    print('CONFUSION MATRIX')

That was fun!

Related links:

* <a href="https://gumroad.com/l/MnRYU">Machine Learning course & examples</a>
* <a href="https://pythonprogramminglanguage.com/python-machine-learning/">Python Machine Learning blog</a>
* <a href="https://scikit-learn.org/stable/index.html">Sklearn Machine Learning module</a>
* <a href="https://pythonbasics.org/why-python-for-machine-learning/">Why Python for Machine Learning</a>