python logo


Category: tutorials

svm python

A common task in Machine Learning is to classify data. Given a data point cloud, sometimes linear classification is impossible. In those cases we can use a Support Vector Machine instead, but an SVM can also work with linear separation.

Related Course:

Dataset
We loading the Iris data, which we’ll later use to classify. This set has many features, but we’ll use only the first two features:

  • sepal length
  • sepal width
The code below will load the data points on the decision surface.


import matplotlib
matplotlib.use('GTKAgg')

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
h = .02 # step size in the mesh

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title('Data')
plt.show()

svm data

Support Vector Machine Example
Separating two point clouds is easy with a linear line, but what if they cannot be separated by a linear line?

In that case we can use a kernel, a kernel is a function that a domain-expert provides to a machine learning algorithm (a kernel is not limited to an svm).

The example below shows SVM decision surface using 4 different kernels, of which two are linear kernels.


import matplotlib
matplotlib.use('GTKAgg')

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset
y = iris.target

h = .02 # step size in the mesh

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0 # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, y)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)
poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)
lin_svc = svm.LinearSVC(C=C).fit(X, y)

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))

# title for the plots
titles = ['SVC with linear kernel',
'LinearSVC (linear kernel)',
'SVC with RBF kernel',
'SVC with polynomial (degree 3) kernel']

for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
plt.subplot(2, 2, i + 1)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title(titles[i])

plt.show()

svm-classify

Download Examples and Course

python logarithmus


Tipp
Sie können den Python-Interpreter als Taschenrechner verwenden. Dazu starten Sie einfach Python ohne IDE und Dateinamen. Beispiel:


Python 2.7.6 (Standard, 22. Juni 2015, 17:58:13)
[GCC 4.8.2] auf linux2
Geben Sie "help", "copyright", "Credits" oder "Lizenz" für weitere Informationen.
>>> 18 * 17
306
>>> 2 ** 4
16
>>>

Related Course:
Python Programming Bootcamp: Go from zero to hero

Mathematische Funktionen


Python unterstützt eine Vielzahl von mathematischen Funktionen.



python thumbnail

Inspired by Netflix, we decided to implement a focal point algorithm. If you use the generated thumbnails on mobile websites, it may increase your click-through-rate (CTR) for YouTube videos. Eiterway, it’s a fun experiment.

Related course:
Master Computer Vision with OpenCV

Focal Point

All images have a region of interest, usually a person or face.

This algorithm that finds the region of interest is called a focal point algorithm. Given an input image, a new image (thumbnail) will be created based on the region of interest.

Start with an snapshot image that you want to use as a thumbnail. We use Haar features to find the most interesting region in an image. The haar cascade files can be found here:

Download these files in a /data/ directory.

Funktion Gibt Beispiel
Abs(x) Der Absolute Wert von x zurückgibt. {% codeblock lang:python line_number:false %} X =-35 X = abs(x) Print(x) {% endcodeblock %}
CMP(x,y) Gibt-1 zurück, wenn X < y
Gibt 0 zurück, wenn x gleich y
Gibt 1 zurück, wenn X > y.
{% codeblock lang:python line_number:false %} X = 6 y = 4 Drucken (cmp(x,y)) {% endcodeblock %}
EXP(x) Kehrt die exponentielle x {% codeblock lang:python line_number:false %} Import-Mathematik X = 6 Drucken (math.exp(x)) {% endcodeblock %}
Log(x) Den natürlichen Logarithmus von x {% codeblock lang:python line_number:false %} Import-Mathematik X = 6 Drucken (math.log(x)) {% endcodeblock %}
log10(x) Der Logarithmus Base-10 x {% codeblock lang:python line_number:false %} Import-Mathematik X = 6 Drucken (math.log10(x)) {% endcodeblock %}
Pow(x,y) Das Ergebnis von X ** y {% codeblock lang:python line_number:false %} Import-Mathematik X = 6 Drucken (math.pow(x,2)) {% endcodeblock %}
sqrt(x) Die Quadratwurzel von x {% codeblock lang:python line_number:false %} Import-Mathematik X = 6 Drucken (math.sqrt(x)) {% endcodeblock %}
Netflix like Thumbnails Python Netflix like Thumbnails Python. Source: Google videos.

#! /usr/bin/python

import cv2

bodyCascade = cv2.CascadeClassifier('data/haarcascade_mcs_upperbody.xml')
frame = cv2.imread('snapshot.png')
frameHeight, frameWidth, frameChannels = frame.shape
regions = bodyCascade.detectMultiScale(frame, 1.8, 2)
x,y,w,h = regions[0]
cv2.imwrite('thumbnail.png', frame[0:frameHeight,x:x+w])
cv2.rectangle(frame,(x,0),(x+w,frameHeight),(0,255,255),6)
cv2.imshow("Result",frame)
cv2.waitKey(0);

We load the haar cascade file using cv2.CascadeClassifier() and we load the image using cv2.imread()
Then bodyCascade.detectMultiScale() detects regions of interest using the loaded haar features.
The image is saved as thumbnail using cv2.imwrite() and finally we show the image and highlight the region of interest with a rectangle. After running you will have a nice thumbnail for mobile webpages or apps.

If you also want to detect both body and face you can use:


#! /usr/bin/python

import cv2

bodyCascade = cv2.CascadeClassifier('data/haarcascade_mcs_upperbody.xml')
faceCascade = cv2.CascadeClassifier('data/lbpcascade_frontalface.xml')
frame = cv2.imread('snapshot2.png')
frameHeight, frameWidth, frameChannels = frame.shape

regions = bodyCascade.detectMultiScale(frame, 1.5, 2)
x,y,w,h = regions[0]
cv2.imwrite('thumbnail.png', frame[0:frameHeight,x:x+w])
cv2.rectangle(frame,(x,0),(x+w,frameHeight),(0,255,255),6)

faceregions = faceCascade.detectMultiScale(frame, 1.5, 2)
x,y,w,h = faceregions[0]
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),6)

cv2.imshow("Result",frame)
cv2.waitKey(0);
cv2.imwrite('out.png', frame)

Related course:
Master Computer Vision with OpenCV

Operaciones matemáticas


Sugerencia
Puede utilizar el intérprete de Python como calculadora. Para ello simplemente iniciar Python sin un IDE y un nombre de archivo. Ejemplo:


Python 2.7.6 (por defecto, 22 de junio de 2015, 17:58:13)
[GCC 4.8.2] de linux2
Tipo de "ayuda", "copyright", "créditos" o "licencia" para obtener más información.
>>> 18 * 17
306
>>> 2 ** 4
16
>>>


Related Course:
Python Programming Bootcamp: Go from zero to hero

Funciones matemáticas


Python soporta una amplia variedad de funciones matemáticas.



python ide

Instalar un IDE de Python


Un Entorno de escritorio integrado (IDE) es un software para la programación. Además de edición de texto simple que tienen todo tipo de funciones tales como resaltado de sintaxis, completado de código, pestañas, un explorador de la clase y muchos más.

Related Course:
Python Programming Bootcamp: Go from zero to hero


Python online intérpretes


Los intérpretes en línea no funcionen para todo pero funcionarán para la mayoría de los tutoriales para principiantes. Recomiendo usar un IDE desktop o el interprete oficial de Python.


Resumen de IDEs (sólo necesita uno)

Función Devuelve Ejemplo
ABS(x) Devuelve el valor absoluto de x. {% codeblock lang:python line_number:false %} x = -35 x = abs(x) Print (x) {% endcodeblock %}
CMP(x,y) Devuelve -1 Si x < y
Devuelve 0 si x es igual a y
Devuelve 1 Si x &gt; y.
{% codeblock lang:python line_number:false %} x = 6 y = 4 Imprimir (cmp(x,y)) {% endcodeblock %}
exp (x) Devuelve la exponencial de x {% codeblock lang:python line_number:false %} importación matemáticas x = 6 Imprimir (math.exp(x)) {% endcodeblock %}
Cienc El logaritmo natural de x {% codeblock lang:python line_number:false %} importación matemáticas x = 6 Imprimir (math.log(x)) {% endcodeblock %}
log10(x) El logaritmo en base 10 de x {% codeblock lang:python line_number:false %} importación matemáticas x = 6 Imprimir (math.log10(x)) {% endcodeblock %}
Pow(x,y) El resultado de x ** y {% codeblock lang:python line_number:false %} importación matemáticas x = 6 Imprimir (math.pow(x,2)) {% endcodeblock %}
sqrt(x) La raíz cuadrada de x {% codeblock lang:python line_number:false %} importación matemáticas x = 6 Imprimir (math.sqrt(x)) {% endcodeblock %}

sl4a

Python scripts can be run on Android using the Scripting Layer For Android (SL4A) in combination with a Python interpreter for Android.

Related courses:
You may like:
Building Android Apps in Python Using Kivy

SL4A
The SL4A project makes scripting on Android possible, it supports many programming languages including Python, Perl, Lua, BeanShell, JavaScript, JRuby and shell. The SL4A project has a lot of contributors from Google but it is not an official Google project.

Scripts can access Android specific features such as calling, text message (SMS), take picture, text to speech, bluetooth and many more.

In this article you will learn how to run Python on Android devices using SL4A.


SL4A is designed for developers


Keep in mind that SL4A is designed for developers and in alpha quality software.


Install SL4A




First enable installation of programs from unknown sources. By default Android devices can only install apps from the Google Play Store.

You have to enable the permission ‘Install from Unknown Sources’, by going to Settings -> Security -> Unknown Sources and tap the box.

After you have have updated these settings donwload the SL4A APK. Visit https://github.com/kuri65536/sl4a on your Android device and download the SL4A APK (or use the QR code on the right).
Once downloaded an installation menu will popup, requesting all permissions on your Android device.


Install Python 3 for Android




Install the Py4A app. The Python for Android app is built to run solely on

Android devices. You should use this app together with SL4A.

You can pick any version of Py4A, but bare in mind the supported version on Android:

  • Python 2 requires Android Device >= 1.6

  • Python 3 requires Android Device >= 2.3.1


The git repository is: https://github.com/kuri65536/python-for-android/releases

You could also use the QR code on the right using a QR scanner on your Android device.

Once Py4A is installed, start the app and press install. This will install the Python interpreter.



 

SL4A


Open SL4A again. Many scripts will appear (in a list). You can now run Python scripts on your Android Device!



 

Press on a program such as speak.py A little popup will be shown. Pressing on the terminal icon will start the Python script.



The third button (the pencil) will open an editor. This is not a full blown IDE but a simple editor.
It doesn’t have syntax highlighting.

Scripting on Android


You may prefer your favorite Python editor whatever it may be (vim/emacs fans here? PyCharm? Atom?)
All scripts are stored in /sl4a/scripts/

Note: File extension


If you installed the Python 3 interpreter, the programs will show with a .py3 extension instead of a .py extension.

A simple program (Spaceship Launch):

IDE Autor Plataforma Descripción Precio Descargar
PyCharm JetBrains Windows, Mac OS X, Linux/UNIX Python IDE. Características como: completación de código, inspecciones de código, error sobre la marcha destacando y soluciones rápidas 89 € / 1er año. ($ 97,90) Descarga PyCharm 
Átomo (+ script plugin) GitHub Windows, Mac OS X, Linux/UNIX Python IDE. Necesita descargar el plugin de secuencia de comandos después de instalar el átomo. Gratis. Descargar Atom.
Pythonista OMZ:software Apple iOS (iPhone, iPad) Las características incluyen: resaltado de sintaxis, completado de código, sistema interactivo, módulos estándar y de iOS. € 9. ($ 9,90) Descargar Pythonista.
Eclipse con PyDev Aleks Totic Windows, Mac OS X, Linux/UNIX Las características incluyen: sintaxis resaltado, refactorización de código, depuración gráfica y mucho más. Gratis Descargar 
Eric Python IDE Detlev Offenbach Windows, Linux/UNIX Las características incluyen: resaltado de sintaxis, autocompletado, clase navegador y más. Gratis Descargar 
Wing IDE Wingware Windows, Mac OS X, Linux/UNIX Características: Sintaxis resaltado, auto-completado, refactorización, pruebas unitarias y versión controlan. $45 a $245 por usuario por licencia. Descargar 
Komodo IDE Komodo Windows, Mac OS X, Linux/UNIX Características: Sintaxis, navegador de documentación, ejecutar código en línea, marcadores rápidos y mucho más. € 40 a € 223. ($99 a $295). Descargar 
Repl.it Amjad Masad, Haya Odeh, Faris Masad y Max Shawabkeh. Web Intérprete de Python Gratis Ejecutar en línea 
Ideone Ideone Web Intérprete de Python Gratis Ejecutar en línea 
CodePad Hazel de Steven Web Intérprete de Python Gratis Ejecutar en línea 
SL4A-APK QR: Link to SL4A APKSL4A-Permissions SL4A-Permissionsqr-python-3-for-android QR Code Python 3 for AndroidAndroid-Python-3-Install Android Python 3 InstallPython-On-Android Python-On-AndroidSL4A-Python-Menu SL4A-Python-Menu

"""TTS Rocket Launch."""

__author__ = 'Frank <[email protected]>'

import android

droid = android.Android()
message = "Python on Android"
droid.ttsSpeak(message)

for i in range(10,0,-1):
droid.ttsSpeak(str(i))

droid.ttsSpeak("We have lift off!")
droid.ttsSpeak("rrrrrr")

More examples:
http://www.mattcutts.com/blog/android-barcode-scanner/
https://github.com/damonkohler/sl4a/blob/wiki/Tutorials.md

python web development

image histogram

python png

OpenCV (cv2) can be used to extract data from images and do operations on them. We demonstrate some examples of that below:

Related courses:

Image properties
We can extract the width, height and color depth using the code below:

import cv2
import numpy as np

# read image into matrix.
m = cv2.imread("python.png")

# get image properties.
h,w,bpp = np.shape(m)

# print image properties.
print "width: " + str(w)
print "height: " + str(h)
print "bpp: " + str(bpp)

Access pixel data
We can access the pixel data of an image directly using the matrix, example:

import cv2
import numpy as np

# read image into matrix.
m = cv2.imread("python.png")

# get image properties.
h,w,bpp = np.shape(m)

# print pixel value
y = 1
x = 1
print m[y][x]

To iterate over all pixels in the image you can use:

import cv2
import numpy as np

# read image into matrix.
m = cv2.imread("python.png")

# get image properties.
h,w,bpp = np.shape(m)

# iterate over the entire image.
for py in range(0,h):
for px in range(0,w):
print m[py][px]

Image manipulation
You can modify the pixels and pixel channels (r,g,b) directly. In the example below we remove one color channel:

import cv2
import numpy as np

# read image into matrix.
m = cv2.imread("python.png")

# get image properties.
h,w,bpp = np.shape(m)

# iterate over the entire image.
for py in range(0,h):
for px in range(0,w):
m[py][px][0] = 0

# display image
cv2.imshow('matrix', m)
cv2.waitKey(0)

To change the entire image, you’ll have to change all channels:   m[py][px][0], m[py][px][1], m[py][px][2].

Save image
You can save a modified image to the disk using:

cv2.imwrite('filename.png',m)

Download Computer Vision Examples + Course

face detection python

In this tutorial you will learn how to apply face detection with Python. As input video we will use a Google Hangouts video. There are tons of Google Hangouts videos around the web and in these videos the face is usually large enough for the software to detect the faces.

Detection of faces is achieved using the OpenCV (Open Computer Vision) library. The most common face detection method is to extract cascades. This technique is known to work well with face detection. You need to have the cascade files (included in OpenCV) in the same directory as your program.

Related course
Master Computer Vision with OpenCV and Python

Video with Python OpenCV

To analyse the input video we extract each frame.  Each frame is shown for a brief period of time. Start with this basic program:

#! /usr/bin/python

import cv2

vc = cv2.VideoCapture('video.mp4')
c=1
fps = 24

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

while rval:
rval, frame = vc.read()
cv2.imshow("Result",frame)
cv2.waitKey(1000 / fps);
vc.release()

Upon execution you will see the video played without sound. (OpenCV does not support sound). Inside the while loop we have every video frame inside the variable frame. 

Face detection with OpenCV


We will display a rectangle on top of the face. To avoid flickering of the rectangle, we will show it at it latest known position if the face is not detected.

#! /usr/bin/python

import cv2

face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface.xml')
vc = cv2.VideoCapture('video.mp4')

if vc.isOpened():
rval , frame = vc.read()
else:
rval = False

roi = [0,0,0,0]

while rval:
rval, frame = vc.read()

# resize frame for speed.
frame = cv2.resize(frame, (300,200))

# face detection.
faces = face_cascade.detectMultiScale(frame, 1.8, 2)
nfaces = 0
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
nfaces = nfaces + 1
roi = [x,y,w,h]

# undetected face, show old on position.
if nfaces == 0:
cv2.rectangle(frame,(roi[0],roi[1]),(roi[0]+roi[2],roi[1]+roi[3]),(0,0,255),2)

# show result
cv2.imshow("Result",frame)
cv2.waitKey(1);
vc.release()

In this program we simply assumed there is one face in the video screen. We reduced the size of the screen to speed up the processing time. This is fine in most cases because detection will work fine in lower resolutions.  If you want to execute the face detection in “real time”, keeping the computational cycle short is mandatory. An alternative to this implementation is to process first and display later.

A limitation of this technique is that it does not always detect faces and faces that are very small or occluded may not be detected. It may show false positives such as a bag detected as face.  This technique works quite well on certain type of input videos.

Download Computer Vision Examples and Course

create csv file python

word cloud python

wordcloud python word cloud based on gmail

We have created a python program that generates a wordcloud based on your gmail account. The output may look something like this depending on the contents of your emails.

First you will need a small script that interacts with the gmail service. We have created a small script that interact with gmail. It relies on gmaillib installed and you will need to set: allow “less-secure” applications to access gmail server: https://www.google.com/settings/security/lesssecureapps

Related Course:
Python Programming Bootcamp: Go from zero to hero

Gmail example:

#!/usr/bin/env python
import gmaillib
from collections import Counter


def getMails(cnt,account, start,amount):
emails = account.inbox(start, amount)

for email in emails:
cnt[email.sender_addr] += 1

amountOfMails = 100
cnt = Counter()
username = raw_input("Gmail account: ")
password = raw_input("Password: ")

account = gmaillib.account(username, password)

getMails(cnt,account,0,amountOfMails)
print(cnt)

If this script runs successfully you have almost all requirements installed. You will also need the library called wordcloud. We rebuild the system such that we get one long string containing the message bodies, which we feed as input to the wordcloud instance. The variable amount contains the number of mails to fetch. We have set it to 100 but you could set it to all messages using get_inbox_count() or you could simply fetch all emails of the last week.

Final program:

#!/usr/bin/env python
import gmaillib
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt

amount = 100
cnt = Counter()
username = raw_input("Gmail account: ")
password = raw_input("Password: ")

account = gmaillib.account(username, password)

emails = account.inbox(0, amount)

data = ""
for email in emails:
data = data + str(email.body)

wordcloud = WordCloud().generate(data)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

python requests library