Tuesday, February 23, 2016

Python Data Analysis 3 - The NumPy Library

-- The NumPy Installation

sudo apt-get install python-numpy

import numpy as np

-- Ndarray: The Heart of the Library

a=np.arrary([1, 2, 3])
a
type(a)
a.dtype
a.ndim
a.size
a.shape

b = np.array([[1.3, 2.4],[0.3, 4.1]])
b.dtype
b.ndim
b.size
b.shape
b.itemsize
b.data

-- Create an Array
c = np.array([[1, 2, 3],[4, 5, 6]])
c
d = np.array(((1, 2, 3),(4, 5, 6)))
d
e = np.array([(1, 2, 3), [4, 5, 6], (7, 8, 9)])
e

-- Types of Data
g = np.array([[’a’, ’b’],[’c’, ’d’]])
g
g.dtype
g.dtype.name


-- The dtype Option
f = np.array([[1, 2, 3],[4, 5, 6]], dtype=complex)
f

-- Intrinsic Creation of an Array
np.zeros((3, 3))
np.ones((3, 3))
np.arange(0, 10)
np.arange(4, 10)
np.arange(0, 12, 3)
np.arange(0, 6, 0.6)
np.arange(0, 12).reshape(3, 4)
np.linspace(0,10,5)
np.random.random(3)
np.random.random((3,3))

-- Basic Operations
-- Arithmetic Operators
a = np.arange(4)
a
a+4
a*2
b = np.arange(4,8)
a+b
a-b
a*b
a * np.sin(b)
a * np.sqrt(b)

A = np.arange(0, 9).reshape(3, 3)
A
B = np.ones((3, 3))
B
A*B

-- The Matrix Product
np.dot(A,B)
A.dot(B)
np.dot(B,A)

-- Increment and Decrement Operators
a = np.arange(4)
a
a += 1
a
a -= 1
a
array([0, 1, 2, 3])
a += 4
a
a *= 2
a

-- Universal Functions (ufunc)
a = np.arange(1, 5)
a
np.sqrt(a)
np.log(a)
np.sin(a)

-- Aggregate Functions
a = np.array([3.3, 4.5, 1.2, 5.7, 0.3])
a.sum()
a.min()
a.max()
a.mean()
a.std()

-- Indexing, Slicing and Iterating
a = np.arange(10, 16)
a
a[4]
a[-1]
a[-6]
a[[1,3,4]]

A = np.arange(10, 19).reshape((3, 3))
A
A[1,2]

a = np.arange(10, 16)
a
a[1:5]
a[1:5:2]
a[::2]
a[:5:2]
a{:5:]

A = np.arange(10, 19).reshape((3, 3))
A
A[0,:]
A[0:2, 0:2]
A[[0,2], 0:2]

Iterating an Array
for i in a:
print a
for row in A;
print row
for item in A.flat;
print item
np.apply_along_axis(np.mean, axis=0, arr=A)
np.apply_along_axis(np.mean, axis=1, arr=A)

def foo(x):
return x/2

np.apply_along_axis(foo, axis=1, arr=A)
np.apply_along_axis(foo, axis=0, arr=A)

-- Conditions and Boolean Arrays
A = np.random.random((4, 4))
A
A<0.5
A[A<0.5]

-- Shape Manipulation
a = np.random.random(12)
a
A = a.reshape(3, 4)
A

a.shape = (3, 4)
a
a = a.ravel()
a.shape = (12)
a
A.transpose()

-- Array Manipulation

-- Joining Arrays

A = np.ones((3, 3))
B = np.zeros((3, 3))
np.vstack((A, B))
B = np.zeros((3, 3))
np.vstack((A, B))
a = np.array([0, 1, 2])
b = np.array([3, 4, 5])
c = np.array([6, 7, 8])
np.column_stack((a, b, c))
np.row_stack((a, b, c))

-- Splitting Arrays
A = np.arange(16).reshape((4, 4))
A
[B,C] = np.hsplit(A, 2)
B
C
[B,C] = np.vsplit(A, 2)
B
C
[A1,A2,A3] = np.split(A,[1,3],axis=1)
A1
A2
A3
[A1,A2,A3] = np.split(A,[1,3],axis=1)
A1
A2
A3

-- General Concepts

Copies or Views of Objects

a = np.array([1, 2, 3, 4])
b = a
b
a[2] = 0
b
c = a[0:2]
c
a[0] = 0
c
a = np.array([1, 2, 3, 4])
c = a.copy()
c
a[0]=0
c

-- Vectorization

a * b
A * B
for (i = 0; i < rows; i++){
  c[i] = a[i]*b[i];
}
for( i=0; i < rows; i++){
   for(j=0; j < columns; j++){
      c[i][j] = a[i][j]*b[i][j];
   }
}

-- Broadcasting
A = np.arange(16).reshape(4, 4)
b = np.arange(4)
A
b
A+b
m = np.arange(6).reshape(3, 1, 2)
m = np.arange(6).reshape(3, 1, 2)
m
n
m+n

-- Structured Arrays

structured = np.array([(1, ’First’, 0.5, 1+2j),(2, ’Second’, 1.3, 2-2j), (3, ’Third’, 0.8, 1+3j)],dtype=(’i2, a6, f4, c8’))
structured
structured = np.array([(1, ’First’, 0.5, 1+2j),(2, ’Second’, 1.3,2-2j), (3, ’Third’, 0.8, 1+3j)],dtype=(’int16, a6, float32, complex64’))
structured
structured[1]
structured[’f1’]
structured = np.array([(1,’First’,0.5,1+2j),(2,’Second’,1.3,2-2j),(3,’Third’,0.8,1+3j)],dtype=[(’id’,’i2’),(’position’,’a6’),(’value’,’f4’),(’complex’,’c8’)])
structured
structured.dtype.names = (’id’,’order’,’value’,’complex’)
structured[’order’]

Reading and Writing Array Data on Files

Loading and Saving Data in Binary Files

data
np.save(’saved_data’,data)
loaded_data = np.load(’saved_data.npy’)
loaded_data

-- Reading and Writing Array Data on Files
-- Loading and Saving Data in Binary Files
data = np.genfromtxt(’data.csv’, delimiter=’,’, names=True)
data

data2 = np.genfromtxt(’data2.csv’, delimiter=’,’, names=True)
data2
data2['id']

data2[0]

No comments:

Post a Comment

Blog Archive