Data science with Python? No problem!

Maybe you think what is the purpose of using Python when MATLAB is around (or maybe Maple, Mathematica or even R). First of all I lived in paradigm that Python is only good for learning and for proper coding I would rather use C++ and for scientific computation MATLAB or R.

My EEG expertize is based on MATLABs toolbox EEGLAB. I faced too many times compatability issues (newer version of MATLAB versus newer version of EEGLAB) and so slow performace (in some tasks). Since then I re-discovered a Python programming language and started to experiment with it.

Then i found excelent tool for EEG analysis Python-MNE, machine learning and others. Not even that works everywhere and it is open-source but it is soooo faster than MATLAB. Did I mention that every Python module I have tried had great documentation with gallery of most wanted exampled from scratch to final product? And tutorials on GitHub? No? Well, that’s why I try to switch to Python as many scientists are nowadays.

Install Python on Windows

I strongly recommend to install some package with includes all dependencies such as Anaconda. It is available for Linux and macOS (but these systems had Python 2.7 included in distribution).

First download Anaconda for Python 2.7 (and yes, Python 3.5 is the future, but if you will follow tutorials on internet it will be probably in Python 2.7 so it is more practical strategy to use Python 2.7).

After instlaltion you have installed Python 2.7 distributio on your system with many modules. Most useful of them are:

  • matplotlib – plottling module (very similar plot as in MATLAB is)
  • numpy – numerical module
  • scipy – scientific module
  • scikit-learn – machine learning
  • jupyter notebook – excelent tool for learning python
  • and others

If you want to install some custom modules such as Python-MNE you need to open an commandline and write:

pip install mne

Which work very simple as

conda install name-of-your-package

Then you only need to import module in your script as (example):

 from matplotlib import pyplot as plt
import numpy as np
import mne
import sklearn 

Install Python in macOS

In macOS you have two options:

  1. use user Python distribution
  2. use Anacdona as well
  3. use Python 3 or Python 2 official distribution
  4. use brew (if you don’t know what brew is, then you won’t install Python in it)

For Anaconda it is basically the same process so I cover user Python distribution (which i prefer because I control all modules, versions etc. which comes handy)

Open terminal (⌘ + space and then enter terminal).

Chceck if you have installed Python on your macOS like:

which python

and then you would get something like:

Snímek obrazovky 2016-08-29 v 13.08.27

After then you can install any module you want BUT in users directory. From macOS Yosemite and El Capitan it is restricted to install modules in root directory. So syntax for installing modules is this:

pip install --user mne
pip install --user numpy

or multiple modules at once

pip install --user scipy, scikit-learn, matplotlib

You would need install every module (even matplotlib or numpy when you use user Python on macOS because it doesnt contain any modules at all just vanilla Pyhton 2.7).

After then you can import modules in your script same as described before.

If you want to know which modules you have installed then you can write this command:

pip list

Jupyter notebook

The Jupyter Notebook is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, machine learning and much more.

And why this is so cool? All code is evaluated at server which runs localy on your computer and you interact with Python and all modules via your internet browser. You can connect it to Python or R, MATLAB or Ruby.

You would install it via pip like

pip install --user jupyter

or (with formerly name iPython)

pip install --user ipython

Or via Anaconda promt command line (but it should be included in Windows version out-of-box as application shortcut)

conda install juypter

After it is installed you can simply enter

jupyter notebook

This command should open your browser and you can start new iPyhton notebook.



One thought on “Data science with Python? No problem!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s