Path: blob/master/Data Analytics Using Python/1 NumPy.ipynb
3074 views
Kernel: Python 3 (ipykernel)
Numerical Analysis using Python
What is NumPy?
NumPy (Numerical Python) is a Python library for numerical computing. It provides:
Efficient array operations
Mathematical and logical operations
Linear algebra, Fourier transform, and more.
Let's start by importing NumPy.
In [7]:
Out[7]:
array([1, 4, 9])
In [8]:
Out[8]:
1.23.5
1. Array Creation
NumPy arrays are more efficient than Python lists for numerical computations.
In [9]:
Out[9]:
Array: [1 2 3]
Type: <class 'numpy.ndarray'>
In [12]:
Out[12]:
array([[0, 0, 0],
[0, 0, 0]])
In [15]:
Out[15]:
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
In [17]:
Out[17]:
array([[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]]])
In [28]:
Out[28]:
array([[20, 18, 16],
[14, 12, 10]])
In [134]:
Out[134]:
array([ 5, 10, 15])
In [135]:
Out[135]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
Quick Practice:
Create an array using arange function starting with 50 and end with 10.
reshape it to 2D array
In [31]:
Out[31]:
array([0. , 0.5, 1. ])
Different Dimensions
In [32]:
Out[32]:
1D Array:
[1 2 3 4 5]
Shape: (5,)
In [49]:
Out[49]:
2D Array:
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
In [34]:
Out[34]:
3D Array:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
Shape: (2, 2, 3)
Quick Practice:
Create a 4x4 matrix of all fives.
Create an array of 20 numbers between 10 and 50 (inclusive).
2. Array Attributes
In [35]:
Out[35]:
Shape: (2, 3)
Size: 6
Data Type: int32
Dimension: 2
3. Indexing and Slicing
In [46]:
Out[46]:
array([1, 2, 3, 4, 5])
In [48]:
Out[48]:
2
In [50]:
Out[50]:
array([[1, 2, 3],
[4, 5, 6]])
In [54]:
Out[54]:
5
In [59]:
Out[59]:
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
In [63]:
Out[63]:
3
In [65]:
Out[65]:
array([[11, 12]])
In [62]:
Out[62]:
array([ 8, 11])
Quick Practice:
Extract every 2nd element from an array of numbers 0-20.
From a 3x3 matrix, extract the last column.
4. Array Operations (Element-wise)
In [66]:
Out[66]:
Addition: [5 7 9]
Multiplication: [ 4 10 18]
Dot Product: 32
5. Universal Functions (ufuncs)
In [ ]:
6. Reshape and Transpose
In [70]:
Out[70]:
Reshaped: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Transpose: [[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
7. Stacking and Splitting
In [85]:
Out[85]:
array([[1, 2],
[3, 4]])
In [87]:
Out[87]:
(2, 2)
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
In [88]:
Out[88]:
array([[1, 2, 5, 6],
[3, 4, 7, 8]])
8. Broadcasting
Computation between array of differrent shapes
smaller array should always of 1*n shape , where n = number of colums in higher order array
In [100]:
Out[100]:
array([1, 2, 0])
In [101]:
Out[101]:
array([[2, 2, 2],
[1, 1, 1],
[3, 3, 3]])
In [102]:
9. Random Module
In [116]:
Out[116]:
array([[0.59986582, 0.44095902, 0.15436578],
[0.59329516, 0.71232562, 0.70069948],
[0.73242941, 0.11822785, 0.70461349]])
In [123]:
Out[123]:
array([[90, 20],
[37, 39]])
In [128]:
Out[128]:
array([[0.5488135 , 0.71518937, 0.60276338],
[0.54488318, 0.4236548 , 0.64589411],
[0.43758721, 0.891773 , 0.96366276]])
In [125]:
Out[125]:
8
10. Linear Algebra
In [129]:
Out[129]:
Determinant: -2.0000000000000004
Inverse: [[-2. 1. ]
[ 1.5 -0.5]]
Eigenvalues: [-0.37228132 5.37228132]
11. Advanced Functions
In [130]:
Out[130]:
['No' 'No' 'Yes' 'Yes' 'Yes']
In [131]:
Out[131]:
Unique: [-9 1 2 3 4 65 80]
In [132]:
Out[132]:
array([-9, 1, 2, 2, 3, 3, 3, 4, 65, 65, 80])