Data Science Tools – NumPy

NumPy
Multidimensional arrays + matrix, mathematical functions useful for statistical analysis (mean,median, standard deviation)

Installation
C:\Python27\Scripts>easy_install numpy
throws error error: Setup script exited with error: Unable to find vcvarsall.bat
Download and install manually http://sourceforge.net/projects/numpy/files/

import numpy
numbers = [1,2,3,4,5]
print numpy.mean(numbers) #3.0
print numpy.median(numbers) #3.0
print numpy.std(numbers) # 1.41

NumPy arrays are different from python lists. Numpy much faster than Python lists directly.

import numpy as np
array = np.array([1, 4, 5, 8], float)  #[ 1.  4.  5.  8.]
#array indexing and slicing
print array[1] #4.0, array[:2] #[ 1.  4.]
array[1] = 5.0 #array[1] = 5.0

# a 2D array/Matrix , Matrix indexing and slicing

array  = np.array([[1, 2, 3], [4, 5, 6]], float)  # [[ 1.  2.  3.]  [ 4.  5.  6.]]
array [1][1] #5.0
array[1, :] #[ 4.  5.  6.]
array[:, 2] #[ 3.  6.]

#Arithmetic operations

array1 = np.array([1, 2, 3], float)
array2 = np.array([5, 2, 6], float)
array1 + array2 #[ 6.  4.  9.]
array1 - array2 #[-4.  0. -3.]
array1 * array2 #[  5.   4.  18.]

#Matrix arithmetic

array = np.array([[1, 2], [3, 4]], float)
array2 = np.array([[5, 6], [7, 8]], float)
array1 + array2 #[[  6.   8.] [ 10.  12.]]
array1 - array2 #[[-4. -4.] [-4. -4.]]
array1 * array2 #[[  5.  12.] [ 21.  32.]]

#Standard arithmetic operations

array1 = np.array([1, 2, 3], float)
array2 = np.array([[6], [7], [8]], float)
np.mean(array1) #2.0
np.dot(array1, array2) #[ 44.]

NumPy Dot

Advertisement

One response to “Data Science Tools – NumPy

  1. Pingback: Jupyter Notebook | {Algorithm;}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

%d bloggers like this: