Talking about Machine Learning (I): Setup
Tags: machinelearning, tutorials, python, beginners
Published: 2019-03-12 20:42:01

---
title: Talking about Machine Learning (I): Setup
published: true
description: Setting up our environment
tags: machinelearning, tutorials, python, beginners
series: Talking about Machine Learning
cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Scikit_learn_logo_small.svg/220px-Scikit_learn_logo_small.svg.png
---

Next couple of post of this series will be a tutorial about machine learning, one of the most popular [branches of AI](http://jmc.stanford.edu/artificial-intelligence/what-is-ai/branches-of-ai.html).

### Environment

I will work with the following libraries([NumPy](https://www.numpy.org/), [SciPy](https://www.scipy.org/), [scikit-learn](https://scikit-learn.org/stable/index.html), [matplotlib](https://matplotlib.org/)). I build a tiny install script.

```bash
mkdir -p talkingaboutml/talkingaboutml
python3 -m virtualenv talkingaboutml/venv
talkingaboutml/venv/bin/pip install numpy scipy scikit-learn matplotlib
```

now, your talkingaboutml dir looks like:

```
talkingaboutml/
├── talkingaboutml (here we store our examples)
└── venv

```


### First example 

On our first example i will use sckit datasets (are avaiable on sklearn.datasets), there are many [example datasets](https://scikit-learn.org/stable/datasets/index.html). I Choose iris. This dataset is a multi-class classification dataset. 

As a First example, i will train a simple classification and run a predict.

We need some imports, datasets, accuracy metric and a linear svc:

```python
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.svm.classes import SVC
```

Load dataset,  this datasets are already divided (data, target).

```python
iris = datasets.load_iris()
X = iris.data # each register, a iris with features
y = iris.target # classification for each register

feature_number = X.shape[1]
```

Create classification, train and predict.
```python

clf = SVC(kernel='linear', C=1.0, probability=True, random_state=0) # 'Linear SVC'

clf.fit(X, y) # Train

y_pred = clf.predict(X)
accuracy = accuracy_score(y, y_pred)
print(accuracy)
```

So... let's do this, in this example i train a single  [classificator](https://en.wikipedia.org/wiki/Statistical_classification) with different C(penalty) values. This parameter tells [svm](https://en.wikipedia.org/wiki/Support-vector_machine) how match you want to avoid misclassifying each training example. A good explanation can be found [here](http://svms.org/parameters/) or [here](https://stats.stackexchange.com/questions/31066/what-is-the-influence-of-c-in-svms-with-linear-kernel#159051).

```python
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.svm.classes import SVC

iris = datasets.load_iris()

X = iris.data # each register, a iris with features
y = iris.target # clasiffication ir each register


feature_number = X.shape[1]

penalties = list(np.arange(0.5,10.0, 0.1))

accs = []

for C in penalties:
    clf = SVC(kernel='linear', C=C, probability=True, random_state=0) # 'Linear SVC'

    clf.fit(X, y) # Train

    y_pred = clf.predict(X)
    accuracy = accuracy_score(y, y_pred)
    accs.append(accuracy)


# plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(penalties, accs, 'r')
plt.show()
```

![penalties result](https://thepracticaldev.s3.amazonaws.com/i/iuj7g9qwhyn65a347mlq.png)

As we can see, the penalty factor. If it is too large, we have too many support vector and it may cause overfit.