Feeds:
Posts
Comments

Archive for March, 2020

Matplotlib is a plotting library for python. Matplotlib was originally written by John D Hunter during his post-doctoral research in neurobiology to visualize electrocorticography (ECoG) data of epilepsy patients. Then Michael Droettboom lead the development project after John Hunter passed away in August 2012.
Matplotlib version 2.0.x support python version 2.7 – 3.6.
Starting 2020, Matplotlib does not support python 2 anymore.

Using Matplotlib with python is easy.

I will show you how to visualize a simple graph.
In this tutorial, I use IDLE as python editor.
If you don’t have it, you can install using command below from your Linux Terminal.
$ sudo apt-get install idle

Run the idle.
plot00

From menu, select File>New File to display the Editor.
Type the code below and press F5 to run the code.

import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[5,6,7,8]
plt.plot(x,y)
plt.show()

plot01plot02
You have your first graph with Matplotlib.
It’s easy, right?
(more…)

Read Full Post »

Slicing is accessing parts of array content.

The syntax is:
start:stop:step

x[1:5]      → display 1 until 5 → 1,2,3,4,5
x[5:]        → display all after 5 → 6,7,8,9
x[:6]        → display from beginning until 6 → 1,2,3,4,5,6,7,8,9
x[:]          → display all → 1,2,3,4,5,6,7,8,9
x[1:9:2]  → display between 1 to 9, step 2 → 2,4,6,8
x[-1]        → display last item → 9
x[-2]        → display 2nd item from the last → 8
x[:-3]      → display all except the 3 items. → 1,2,3,4,5,6
x[::-1]     → display all in reversed → 9,8,7,6,5,4,3,2,1
x[2::-1]   → display first 3 items, reversed → 3,2,1
x[:-4:-1]  → display the last 3 items, reversed → 9,8,7
x[-2::-1]  → display all except the 1 item, reversed → 8,7,6,5,4,3,2,1

Open your Linux Terminal and practice it.

$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x=np.array([1,2,3,4,5,6,7,8,9])
>>> x
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
 (more…)

Read Full Post »

In this article, I will show you how to do basic operation in array.

>>> import numpy as np
>>> x=np.array([[1,2,3,4],[5,6,7,8]])
>>> x
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> x.ravel()
array([1, 2, 3, 4, 5, 6, 7, 8])

ravel() function will create all array into 1 demensional array.

>>> x
array([[1, 2, 3, 4],
[5, 6, 7, 8]])

But the operation will not change the original array value.

>>> x.min()
1

Minimum data in the array

>>> x.max()
8

Maximum data in the array

>>> x.mean()
4.5

Average data in the array

>>> x.sum()
36

Total value in the array

>>> x.sum(axis=0)
array([ 6, 8, 10, 12])
>>> x.sum(axis=1)
array([10, 26])

math02

>>> np.sqrt(x)
array([[1. , 1.41421356, 1.73205081, 2. ],
[2.23606798, 2.44948974, 2.64575131, 2.82842712]])

Square root each data value in the array.

>>> y=np.array([[1,1,1,1],[1,1,1,1]])
>>> y
array([[1, 1, 1, 1],
[1, 1, 1, 1]])
>>> x+y
array([[2, 3, 4, 5],
[6, 7, 8, 9]])
>>> x-y
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> x*y
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>>

math01

Read Full Post »

The concept of “dimensional” in numpy is different with dimensional in math. In Math, dimensions is defined as the minimum number of coordinates needed to specify points within a space. So, 1 dimension consist of 1 variable X , 2 dimensions consist of 2 variables X &Y and 3 dimensions consist of 3 variables X,Y & Z.

Meanwhile dimensional in python is a container of items of the same type and size.The dimension can consist of of 1 or multi dimensional container. That’s why it named N-dimensional array.
I will give you examples.
Open your Linux Terminal, run python and type the code below.

Sample for 1 dimensional array

$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[nGCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x=np.array([1,2,3])
>>> x
array([1, 2, 3])
>>> x.ndim
1
>>> x.shape
(3,)
>>>

This is 1 dimensional array that consist of 3 columns.

Sample for 2 dimensional array

>>> x=np.array([[1,1],[2,2]])
>>> x
array([[1, 1],
       [2, 2]])
Display the dimension
>>> x.ndim
2

Display the array shape

>>> x.shape
(2, 2)

You can read it as 2 ‘1-dimensional array’ ([1,1] and [2,2]) that each of it consist of 2 columns ([1,1] and [2,2]).

>>> x=np.array([[1,1,1],[2,2,2]])
>>> x
array([[1, 1, 1],
       [2, 2, 2]])
>>> x.ndim
2
>>> x.shape
(2, 3)

You can read it as 2 ‘1-dimensional array’ ([1,1,1] and [2,2,2]) that each of it consist of 3 columns ([1,1,1] and [2,2,2]).

>>> x=np.array([[1,1,1],[2,2,2],[3,3,3]])
>>> x
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])

It’s still 2 dimensional.

>>> x.ndim
2
>>> x.shape
(3, 3)
>>>


(more…)

Read Full Post »

In this article, I will explain in a simple way about Numpy (Numeric Python). Numpy is part of SciPy group, an open source python libraries that are designed for scientific computing that python can’t provide. There are others library that part of SciPy, which are Matplot Lib and Pandas.

By using Numpy, you can increase your python performance in computing numerical calculation significantly. Since, it’s very basic, I will not explain about specific data structure like N-dimensional array.

Numpy was developed by Travis Oliphant and released in 2006. The version Numpy is unified from Numarray which is developed by Jim Hugunin in 1995. Both of the package has their own power. And now, we have it both in one package.

Before we start, make sure you have install numpy in your Linux system.
To install numpy in python, type this command in your Linux Terminal.

$pip install numpy

I use Xubuntu 18.04 and python 3.6.9 when I write this article. So, by default the python version is still 2.7.15+ (default, Oct 7 2019, 17:39:04). Since, I use python 3.6.9 (default, Nov 7 2019, 10:44:02), the ‘pip’ install will be different.

The command is:

$pip3 install numpy

After all done, let’s try. Run your python and type the codes below:

import numpy as np
a=np.arange(0,10)
print(a)

This command, will create an array with consist of “0,1,2,3,4,5,6,7,8,9”.

$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.arange(0,10)
>>> print(a)
[0 1 2 3 4 5 6 7 8 9]
>>>


(more…)

Read Full Post »