Ein Lernalgorithmus in Python für ein Perzeptron

(Last Updated On: 10. Oktober 2018)

Was man sich vorher ansehen sollte.
NumPy: Quickstart tutorial bzw. NumPy v1.12.dev0 Manual oder Tentative NumPy Tutorial bzw. auf GitHub scipy-tentative-numpy-tutorials. Wer ein älteres pdf bevorzugt findet es hier doc_numpy-pdf_1

Pandas: https://pandas.pydata.org/pandas-docs/stable/tutorials.html
Matplotlib: https://matplotlib.org/users/beginner.html

Von Python sollte eine 3er Version installiert sein. python -V sagt mir, dass ich Python 3.5.2 installiert habe, womit ich zufrieden bin.
Auf Python 3.5.2 documentation findet man die Doku zu Python und um pip braucht man sich seit der 3.4 Version auch nicht mehr kümmern, da der Paketmanager für PyPI schon automatisch mitinstalliert wird.
Wir verwenden also NumPy, pandas und matplotlib zum Einlesen, Verarbeiten und Visualisieren der Daten und wir werden einen einfachen Algorithmus zur linearen Klassifikation implementieren.
Zuerst schaue ich einmal, ob ich die neueste Version von pip habe und komme mit „pip install –upgrade pip“ auf pip-9.0.1 und schon habe ich falsch gedacht. Ich muss ja pip für Python 3.x installieren mit sudo apt install python3-pip Dann wieder ein –upgrade und ein sudo pip3 install numpy

 

 

 

 

 

Zum Code:

import numpy as np
class Perceptron(object):
"""Perceptron classifier.

Parameters
------------
eta : float
Learning rate (between 0.0 and 1.0)
n_iter : int
Passes over the training dataset.

Attributes
-----------
w_ : 1d-array
Weights after fitting.
errors_ : list
Number of misclassifications in every epoch.

"""
def __init__(self, eta=0.01, n_iter=10):
self.eta = eta
self.n_iter = n_iter

def fit(self, X, y):
"""Fit training data.

Parameters
----------
X : {array-like}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.

Returns
-------
self : object

"""
self.w_ = np.zeros(1 + X.shape[1])
self.errors_ = []

for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self

def net_input(self, X):
"""Calculate net input"""
return np.dot(X, self.w_[1:]) + self.w_[0]

def predict(self, X):
"""Return class label after unit step"""
return np.where(self.net_input(X) >= 0.0, 1, -1)

 

 
So, soweit funktioniert es und nächstes mal werde ich das Perzeptron-Modell mit den Iris Datensätzen trainieren.


Quelle: Python Machine Learning von Sebastian Raschka; Published 2015-09-23
Weblinks:
httpss://github.com/arnaudbenard/python-machine-learning
https://stackoverflow.com/questions/36532585/passing-an-array-to-numpy-dot-in-python-implementation-of-perceptron-learning
httpss://github.com/rasbt/python-machine-learning-book/blob/master/code/optional-py-scripts/ch02.py
https://natureofcode.com/book/chapter-10-neural-networks/
https://stackoverflow.com/questions/36532585/passing-an-array-to-numpy-dot-in-python-implementation-of-perceptron-learning

(274)


History

Ein Gedanke zu „Ein Lernalgorithmus in Python für ein Perzeptron“

  1. Ach ja, das jupyter notebook ist auch recht praktisch.

    $ pip install jupyter notebook

    To open a Jupyter notebook, we `cd` to the directory that contains your code examples, e.g,.

    $ cd ~/code/python-machine-learning-book

    and launch `jupyter notebook` by executing

    $ jupyter notebook

    Jupyter will start in our default browser (typically running at [https://localhost:8888/](„https://localhost:8888/“)). Now, we can simply select the notebook you wish to open from the Jupyter menu.

Schreibe einen Kommentar zu Helmut Hirner Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert