Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Analysis using Python/NUMpy.ipynb
3074 views
Kernel: Python 3

Numpy:Introduction

  • NumPy is a Open Source Python package. It stands for Numerical Python. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.

  • NumPy is the fundamental package for scientific computing with Python , having following important functionalities:

    • A powerful N-dimensional array object

    • A sophisticated (broadcasting) functions

    • Contains tools for integrating C/C++ and Fortran code

    • Have useful linear algebra, Fourier transform, and random number capabilities

Why NUMpy?

  • Mathematical and logical operations on arrays.

  • Efficient storage and manipulation of numerical arrays is which is fundamental in the process of data science.

  • NumPy arrays form the core of nearly the entire ecosystem of data science tools in Python,

image.png

Installation

  • Anaconda: A free distribution of Python with scientific packages. Supports Linux, Windows and Mac

    To install numpy type : conda install -c anaconda numpy
  • pip:Most major projects upload official packages to the Python Package index. They can be installed on most operating systems using Python’s standard pip package manager.

You can install packages via one of the following Commands:

python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose pip install numpy

Importing Nympy & Checking Version

import numpy numpy.__version__
'1.15.4'

Bounding numpy package to local variable

The numpy package is bound to the local variable numpy. The import as syntax simply allows you to bind the import to the local variable name of your choice (usually to avoid name collisions, shorten verbose module names, or standardize access to modules with compatible APIs).

import numpy as np a =(1,2,3,4,5) b=np.array(a) print (b)
[1 2 3 4 5]
import numpy as arr a =(1,2,3,4,5) b=arr.array(a) print (b)
[1 2 3 4 5]

Speed Comaprison numpy vs core python

import time import numpy as np size_1=1000000 def pure_python_version(): time_python =time.time() list1=range(size_1) list2=range(size_1) sum_list=[list1[i]+list2[i] for i in range(len(list1))] return time.time() - time_python def numpy_version(): time_numpy = time.time() array1 = np.arange(size_1) array2 = np.arange(size_1) sum_array = array1 + array2 return time.time() - time_numpy python_time = pure_python_version() numpy_time = numpy_version() print("pure python version=",format(python_time)) print("numpy =",format(numpy_time))
pure python version= 0.5971319675445557 numpy = 0.015619039535522461

Creating Arrays

The basic ndarray is created using an array function in NumPy which creates an ndarray from any object exposing array interface, or from any method that returns an array.

my_list=[2,3,4,6,7,8]
my_array=np.array(my_list) my_array
array([2, 3, 4, 6, 7, 8])
array_2 = [[1,2,3],[4,5,6],[7,8,9]] array_3= np.array(array_2) array_3 type(array_3)
numpy.ndarray
a = np.array([1, 2, 3,7,5,1], ndmin = 2) print (a) np.ndim(a)
[[1 2 3 7 5 1]]
2
a = np.array([1, 2, 3], dtype = complex) print (a)
[1.+0.j 2.+0.j 3.+0.j]
array_3
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
len(array_3)
3
array_3[2] #Indexing 3rd row
array([7, 8, 9])
array_3[0]
array([1, 2, 3])
array_3[:,1] # Acessing Column
array([2, 5, 8])
array_3[0,1] # (row
array([[1, 2, 3]])

Array Atrributes

Numpy Arrays are conveinent and fast as compared to Python Lists. 1.Shape(no of rows & columns)

array_3.shape #Rows*Column
(3, 3)
my_array.ndim #dimension of array
1
array_3.ndim
2
  • ndim for checking dimension of array

array_3.dtype
dtype('int32')
  • dtype for checking data type of array

Array Initilization

np.arange(1,10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(1,10,3) #(start,end,space)
array([1, 4, 7])
np.zeros(3,dtype=int)
array([0, 0, 0])
np.zeros((3,2))
array([[0., 0.], [0., 0.], [0., 0.]])
np.ones((2,3))
array([[1., 1., 1.], [1., 1., 1.]])
np.linspace(0,4,10) # (start,end,no of values)
array([0. , 0.44444444, 0.88888889, 1.33333333, 1.77777778, 2.22222222, 2.66666667, 3.11111111, 3.55555556, 4. ])
np.eye(4,4) #Identity matrix
array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])

Array Initilization with Random numbers

In various applications( like assigning weights in Artificial Neural Networks) arrays need to be initialised randomly. for this purpose there are various predefined functions in Numpy(reshape and random)

np.random.rand(2)
array([0.00713721, 0.46012828])
np.random.rand(2,2)
array([[0.39055311, 0.01083326], [0.08344101, 0.79291519]])
array_3.reshape(1,9)
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
np.random.rand(9).reshape(3,3)
array([[0.84174647, 0.1263212 , 0.54511868], [0.44885434, 0.41476612, 0.82282794], [0.16713274, 0.82020216, 0.390902 ]])

Indexing arrays

array_1 = np.arange(1,15) array_1
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
array_1[2]
3
array_1[2:6]
array([3, 4, 5, 6])
array_1[-3]
12
array_1[:-3] # before values
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
array_1[-3:] #after values
array([12, 13, 14])
array_1[2:6]=8 #mutable i.e to change the member value of array
array_1
array([ 1, 2, 8, 8, 8, 8, 7, 8, 9, 10, 11, 12, 13, 14])
array_2=[[1,2,3],[4,5,6],[7,8,9]] array_2
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
np.array(array_2)
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
import numpy as np np.eye(4,2)
array([[1., 0.], [0., 1.], [0., 0.], [0., 0.]])
np.arange(2,2,16)
array([], dtype=int32)
np.eye(3,3)
array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
a=np.eye(3,3)
a[1:3]
array([[0., 1., 0.], [0., 0., 1.]])
a[1:0]
array([], shape=(0, 3), dtype=float64)
a[1:2]
array([[0., 1., 0.]])
a=[[1,2,3],[4,5,6],[8,8,9]] z=np.array(a) a
[[1, 2, 3], [4, 5, 6], [8, 8, 9]]
Z.min()
1
z.max()
9
z.argmin()
0
z.argmax()
8
x.reshape(4,4) # 1D to 2D
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
x.flatten() #2D to 1D
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x.transpose()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
z=x.transpose()
z
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

Maths

import numpy as np A = np.array([ [4, 10, 11], [21, 22, 23], [31, 32, 33] ]) B = np.ones((3,3)) print("Adding to arrays: ") print(A + B) print("Substracting Two Arrays") print(A-B) print("\nMultiplying two arrays: ") print(A * B) print("Multiplying one Array with scalar") print(3*A) print("Matrix Multiplication") print(np.dot (A,B))
Adding to arrays: [[ 5. 11. 12.] [22. 23. 24.] [32. 33. 34.]] Substracting Two Arrays [[ 3. 9. 10.] [20. 21. 22.] [30. 31. 32.]] Multiplying two arrays: [[ 4. 10. 11.] [21. 22. 23.] [31. 32. 33.]] Multiplying one Array with scalar [[12 30 33] [63 66 69] [93 96 99]] Matrix Multiplication [[25. 25. 25.] [66. 66. 66.] [96. 96. 96.]]
np.sqrt(A)
array([[2. , 3.16227766, 3.31662479], [4.58257569, 4.69041576, 4.79583152], [5.56776436, 5.65685425, 5.74456265]])
np.exp(A)
array([[5.45981500e+01, 2.20264658e+04, 5.98741417e+04], [1.31881573e+09, 3.58491285e+09, 9.74480345e+09], [2.90488497e+13, 7.89629602e+13, 2.14643580e+14]])
a = np.array([0,30,45,60,90]) print ('Sine of different angles:' ) # Convert to radians by multiplying with pi/180 print (np.sin(a*np.pi/180) ) print ("\n") print ('Cosine values for angles in array:') print (np.cos(a*np.pi/180) ) print ("\n") print ('Tangent values for given angles:' ) print (np.tan(a*np.pi/180) )
Sine of different angles: [0. 0.5 0.70710678 0.8660254 1. ] Cosine values for angles in array: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17] Tangent values for given angles: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]
a=np.array([[11.667,23.662],[33.21,45.887]]) print (np.around(a)) print (np.around(a, decimals = 1)) print (np.around(a, decimals = 2))
[[12. 24.] [33. 46.]] [[11.7 23.7] [33.2 45.9]] [[11.67 23.66] [33.21 45.89]]

Stats

A.sum()
187
A.mean()
20.77777777777778
np.std(A)
9.88576729757121
np.median(A)
22.0
np.mod(A,B)
C:\Users\HP\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in remainder """Entry point for launching an IPython kernel.
array([[1, 2, 3], [0, 0, 0]], dtype=int32)
np.var(A)#Variance
97.72839506172839

Binary Universal Functions

A=[[1,2,3],[3,4,5]] B=[[3,3,4],[1,2,0]] a=np.array(A) b=np.array(B) np.greater_equal(a,b)
array([[False, False, False], [ True, True, True]])
np.less(a,b)
array([[ True, True, True], [False, False, False]])
np.less(b,a)
array([[False, False, False], [ True, True, True]])

Subsetting array

subset1=a[:,0]=1 ## sub set element before postion 0 to value 1
print(a)
[[1 2 3] [1 4 5]]

Concatenating Arrays

np.concatenate([a,b],axis=1)
array([[1, 2, 3, 3, 3, 4], [1, 4, 5, 1, 2, 0]])
np.concatenate([a,b],axis=0)
array([[1, 2, 3], [1, 4, 5], [3, 3, 4], [1, 2, 0]])

Broadcasting

Broadcasting is a method to overcome size of the smaller array by duplicacy so that it is the dimensionality and size as the larger array.

Why Broadcasting?

  • To solve the problem of arithmetic with arrays with different sizes.

import numpy as np a = np.array([1,2,3,4]) b = np.array([10,20,30,40]) c = a * b print (c)
[ 10 40 90 160]
np.ndim(b)
1

Array with different shapes

image.png

a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) b = np.array([0.0,1.0,2.0]) print ('First array:\n',a ) print ('Second array:\n',b ) print ("\n") print ('First Array + Second Array=\n',a+b)
First array: [[ 0. 0. 0.] [10. 10. 10.] [20. 20. 20.] [30. 30. 30.]] Second array: [0. 1. 2.] First Array + Second Array= [[ 0. 1. 2.] [10. 11. 12.] [20. 21. 22.] [30. 31. 32.]]

File Input Output

#The np.save stores the input array in a disk file with npy extension. a = np.array([11,2,30,4,5,13]) np.save('dar',a)
# To reconstruct array from demofile.npy, use load() function. b = np.load('dar.npy') print (b)
[11 2 30 4 5 13]
  • The storage and retrieval of array data in simple text file format is done with savetxt() and loadtxt() functions.

np.savetxt('dar.txt',a) b = np.loadtxt('dar.txt') print (b)
[11. 2. 30. 4. 5. 13.]