Numpy - Jupyter Notebook
Numpy - Jupyter Notebook
NumPy is a general-purpose array-processing Python library which provides handy methods/functions for
working n-dimensional arrays. NumPy is a short form for “Numerical Python“. It provides various
computing tools such as comprehensive mathematical functions, and linear algebra routines.
In [3]: a = np.arange(15).reshape(3,5)
a
a.shape (3, 5) a.ndim 2 a.dtype.name 'int64' a.itemsize 8 a.size 15 type(a) <class 'numpy.ndarray'> b =
np.array([6, 7, 8]) b array([6, 7, 8]) type(b) <class 'numpy.ndarray'>
In [4]: # Shape
a.shape
Out[4]: (3, 5)
In [5]: # dimension
a.ndim
Out[5]: 2
Out[6]: dtype('int32')
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [7]: # item size
a.size
Out[7]: 15
In [8]: # type
print(type(a))
<class 'numpy.ndarray'>
Array Creation
In [9]: b = np.array([2,4,6,8])
b
Shape : (4,)
Size : 4
Data Type : int32
Type : <class 'numpy.ndarray'>
Shape : (4,)
Size : 4
Data Type : float64
Type : <class 'numpy.ndarray'>
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [13]: # 2 dimension array creation
d = np.array([(1,3,5,7),(2,4,6,8)])
d
Shape : (2, 4)
Size : 8
Data Type : int32
Type : <class 'numpy.ndarray'>
Out[16]: dtype('complex128')
In [17]: ab = np.zeros((3,4))
ab
In [18]: ac = np.ones((2,4))
ac
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [19]: # data type, shape, size
print(f"Zeros Array\nData Type : {ab.dtype}")
print(f"Item Size : {ab.size}")
print(f"Shape : {ab.shape}\n")
print(f"Ones Array\nData Type : {ac.dtype}")
print(f"Item Size : {ac.size}")
print(f"Shape : {ac.shape}")
Zeros Array
Data Type : float64
Item Size : 12
Shape : (3, 4)
Ones Array
Data Type : float64
Item Size : 8
Shape : (2, 4)
Ones Array
[[1 1 1]
[1 1 1]
[1 1 1]]
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 5 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [22]: three = np.zeros((3,3))
three[1,1] = 5
inone = np.ones((5,5))
inone[1:-1,1:4] = three
print(f"Zeros with 5 \n\n{three}\n")
print(f"Ones & Zeros with 5 \n\n{inone}")
Zeros with 5
[[0. 0. 0.]
[0. 5. 0.]
[0. 0. 0.]]
[[1. 1. 1. 1. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 5. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 1. 1. 1. 1.]]
[[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 0. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]
[[1. 1. 1.]
[1. 0. 1.]
[1. 1. 1.]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [25]: # copy numpy
onn = np.ones((3,2), dtype='int8')
onc = onn.copy()
onc[1,:] = 0
print('Original - \n', onn)
print('\nCopy - \n', onc)
Original -
[[1 1]
[1 1]
[1 1]]
Copy -
[[1 1]
[0 0]
[1 1]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [31]: rng_reshape = np.arange(15).reshape(5,3)
rng_reshape
Shape : (4,)
Size : 4
Data Type : int32
Type : <class 'numpy.ndarray'>
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [37]: zeros_like = np.zeros_like(x_like)
zeros_like
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
Basic Operations
In [44]: a = np.array([10,20,30,40])
b = np.arange(4)
add = a + b
sub = a - b # Subtraction (a-b)
mul = a * b
print(f"Array A : {a}\nArray B : {b}\n")
print(f"Addition (a+b)\n{add}\n")
print(f"Subtraction (a-b)\n{sub}\n")
print(f"Multiplication (a*b)\n{mul}\n")
Addition (a+b)
[10 21 32 43]
Subtraction (a-b)
[10 19 28 37]
Multiplication (a*b)
[ 0 20 60 120]
In [47]: a = np.array([[1,1],
[0,1]])
b = np.array([[2,0],
[3,4]])
a * b # elementwise product
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [48]: # matrix product
a @ b
In [51]: b += a
b
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_10432\1729391664.py in <module>
----> 1 a += b # b is not automatically converted to integer type
2
3 a
In [53]: a = np.array([10,15,20,25,30])
a.sum()
Out[53]: 100
In [54]: a.min()
Out[54]: 10
In [55]: a.max()
Out[55]: 30
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [56]: b = np.arange(12).reshape(3, 4)
b
Universal Function
In [65]: a = np.array([10,20,30])
np.exp(a)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [66]: np.sqrt(81)
Out[66]: 9.0
In [67]: b = np.array([4,9,16,25])
x = np.sqrt(b)
x
In [68]: y = np.array([1.,-2.,3.,-3.])
np.add(x, y)
[2 4 6 8]
(4,)
Out[71]: 8
[[1 2 3 4]
[5 6 7 8]]
(2, 4)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [75]: b[1, 2] # print 7
Out[75]: 7
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
(2, 2, 3)
In [77]: c[0]
In [78]: c[1]
In [79]: c[0,0]
In [80]: c[1,0]
Out[81]: 6
In [82]: c[:, 1, 0]
Out[85]: dtype('<U16')
In [86]: b = np.array([100,200,300])
b.dtype
Out[86]: dtype('int32')
Out[87]: dtype('float64')
Out[88]: dtype('bool')
Out[89]: dtype('complex128')
The copy owns the data and any changes made to the copy will not affect original array, and any changes
made to the original array will not affect the copy.
The view does not own the data and any changes made to the view will affect the original array, and any
changes made to the original array will affect the view.
In [90]: a = np.array([1,2,3,4,5])
x = a.copy()
a[1] = 10
print(a)
print(x)
[ 1 10 3 4 5]
[1 2 3 4 5]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [91]: a = np.array([1,2,3,4,5])
x = a.copy()
x[1] = 7
print(a)
print(x)
[1 2 3 4 5]
[1 7 3 4 5]
In [92]: a = np.array([1,2,3,4,5])
x = a.view()
a[1] = 10
print(a)
print(x)
[ 1 10 3 4 5]
[ 1 10 3 4 5]
In [93]: a = np.array([1,2,3,4,5])
x = a.view()
x[-1] = 20
print(a)
print(x)
[ 1 2 3 4 20]
[ 1 2 3 4 20]
In [94]: a = np.arange((12))
a
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [96]: a3 = a.reshape(2,3,2) # Reshape 1D to 3D array
a3
[[ 6, 7],
[ 8, 9],
[10, 11]]])
a Shape: (12,)
a2 Shape: (4, 3)
a3 Shape: (2, 3, 2)
In [98]: for i in a:
print(i)
0
1
2
3
4
5
6
7
8
9
10
11
[0 1 2]
[3 4 5]
[6 7 8]
[ 9 10 11]
[[0 1]
[2 3]
[4 5]]
[[ 6 7]
[ 8 9]
[10 11]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
NumPy Array Join
In [101]: a = np.array([1,2,3])
b = np.array([4,5,6])
ab = np.concatenate((a, b))
ab
In [102]: a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
ab = np.concatenate((a, b))
ab
In [103]: a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
ab = np.concatenate((a, b), axis=1)
ab
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [105]: a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
ab = np.hstack((a,b))
ab
In [106]: a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
ab = np.hstack((a, b))
ab
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [110]: a = np.array([1, 2, 3, 4, 5, 6])
ab = np.array_split(a, 3)
ab
In [112]: ab[0]
In [113]: ab[1]
In [114]: ab[2]
Out[114]: array([5])
In [115]: ab[3]
Out[115]: array([6])
2D Arrays Split
In [116]: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]
ab = np.array_split(a, 2)
ab
In [117]: ab[0]
In [119]: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]
ab = np.array_split(a, 3)
ab
In [120]: ab[0]
In [121]: ab[1]
In [122]: ab[2]
In [123]: a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
ab = np.array_split(a, 4)
ab
In [124]: ab[0]
In [125]: ab[1]
In [126]: ab[2]
Out[128]: [array([[1],
[4],
[7]]),
array([[2],
[5],
[8]]),
array([[3],
[6],
[9]])]
In [129]: a = np.array([[1,2],[3,4],[5,6]])
ab = np.hsplit(a, 2)
ab
Out[129]: [array([[1],
[3],
[5]]),
array([[2],
[4],
[6]])]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [130]: a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
ab = np.where(a == 5)
ab
Out[134]: 2
Out[135]: 7
Out[136]: 6
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [137]: a = np.array([6, 7, 8, 9, 10, 11, 12, 13])
ab = np.searchsorted(a, [1,3,8,9,10])
ab
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [143]: arr = np.array([1, 2, 3, 4, 5, 6, 7])
# Create an empty list
filter_arr = []
# go through each element in arr
for element in arr:
# if the element is completely divisble by 2, set the value to True, otherwise False
if element % 2 == 0:
filter_arr.append(True)
else:
filter_arr.append(False)
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
Random Numbers in NumPy
In [147]: x = random.randint(100)
print(x)
39
In [148]: x = random.rand()
x
Out[148]: 0.33150184362238155
[92 25 61]
In [150]: # Generate a 2-D array with 2 rows, each row containing 3 random integers from 0 to 100:
x = random.randint(100, size=(2,3))
x
In [152]: # Generate a 2-D array with 3 rows, each row containing 2 random numbers:
x = random.rand(3, 2)
print(x)
[[0.94764137 0.43819239]
[0.52815542 0.01067087]
[0.72109457 0.10579046]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [153]: # Return one of the values in an array:
x = random.choice([3, 5, 7, 9])
print(x)
In [154]: # The choice() method also allows you to return an array of values.
# Add a size parameter to specify the shape of the array.
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)
[[9 7 5 7 3]
[5 5 7 7 5]
[3 3 5 3 5]]
Data Distribution is a list of all possible values, and how often each value occurs. Such lists are important
when working with statistics and data science. The random module offer methods that returns randomly
generated data distributions.
ExampleGet your own Python Server Generate a 1-D array containing 100 values, where each value
has to be 3, 5, 7 or 9.
[5 7 3 7 7 7 3 7 7 7 7 7 7 5 7 3 7 7 5 7 5 5 7 3 7 7 5 7 7 7 7 7 5 7 5 7 7
7 7 7 7 7 7 7 7 7 7 5 5 5 5 5 7 7 7 7 7 5 7 3 3 7 5 5 7 7 7 5 5 5 5 5 5 5
7 7 5 7 7 5 5 7 5 7 7 5 7 7 7 7 7 7 7 7 5 7 3 5 5 7]
In [156]: # Same example as above, but return a 2-D array with 3 rows, each containing 5 values.
x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(3, 5))
print(x)
[[7 7 5 5 3]
[5 5 7 7 3]
[7 5 7 7 3]]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
Random Permutations of Elements
Shuffling Arrays
[1 2 4 3 5]
[1 5 4 2 3]
In [160]: # Generate a random normal distribution of size 2x3 with mean at 3 and standard deviation
x = random.normal(loc=3, scale=2, size=(2, 3))
print(x)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [161]: import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.normal(size=1000), hist=False)
plt.show()
C:\Users\User\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning:
`distplot` is a deprecated function and will be removed in a future version. Please ada
pt your code to use either `displot` (a figure-level function with similar flexibility)
or `kdeplot` (an axes-level function for kernel density plots).
warnings.warn(msg, FutureWarning)
Binomial Distribution
[ 8 11 7 9 11 11 11 14 7 7]
C:\Users\User\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning:
`distplot` is a deprecated function and will be removed in a future version. Please ada
pt your code to use either `displot` (a figure-level function with similar flexibility)
or `kdeplot` (an axes-level function for kernel density plots).
warnings.warn(msg, FutureWarning)
[0 2 1 1 1 1 2 0 3 2]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [166]: from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.poisson(lam=2, size=1000), kde=False)
plt.show()
C:\Users\User\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning:
`distplot` is a deprecated function and will be removed in a future version. Please ada
pt your code to use either `displot` (a figure-level function with similar flexibility)
or `histplot` (an axes-level function for histograms).
warnings.warn(msg, FutureWarning)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [167]: from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.poisson(lam=3, size=1000))
plt.show()
Uniform Distribution
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
Visualization of Uniform Distribution
C:\Users\User\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning:
`distplot` is a deprecated function and will be removed in a future version. Please ada
pt your code to use either `displot` (a figure-level function with similar flexibility)
or `kdeplot` (an axes-level function for kernel density plots).
warnings.warn(msg, FutureWarning)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [170]: sns.distplot(random.uniform(size=30), hist=False)
plt.show()
C:\Users\User\anaconda3\lib\site-packages\seaborn\distributions.py:2619: FutureWarning:
`distplot` is a deprecated function and will be removed in a future version. Please ada
pt your code to use either `displot` (a figure-level function with similar flexibility)
or `kdeplot` (an axes-level function for kernel density plots).
warnings.warn(msg, FutureWarning)
NumPy ufuncs
In [171]: x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = []
for i, j in zip(x, y):
z.append(i + j)
print(z)
[5, 7, 9, 11]
In [172]: x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = np.add(x, y) # add() function
print(z)
[ 5 7 9 11]
[6 8 10 12]
In [175]: print(type(np.add))
<class 'numpy.ufunc'>
In [176]: print(type(np.concatenate))
<class 'function'>
add is ufunc
In [178]: try:
if type(np.blahblah) == np.ufunc:
print('blahblah is ufunc')
else:
print('blahblah is not ufunc')
except:
print('blahblah is not ufunc in NumPy.')
NumPy Arithmetic
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [179]: a = np.array([10,20,30,40])
b = np.array([100,200,300,400])
add = np.add(a, b)
print(add)
In [180]: a = np.array([10,20,30,40])
b = np.array([100,200,300,400])
subtract = np.subtract(b, a)
print(subtract)
In [181]: a = np.array([10,20,30,40])
b = np.array([100,200,300,400])
multiply = np.multiply(a, b)
print(multiply)
In [182]: a = np.array([10,20,30,40])
b = np.array([100,200,300,400])
divide = np.divide(b, a)
print(divide)
In [183]: a = np.array([10,20,30,40])
b = np.array([1,2,3,4])
power = np.power(a, b)
print(power)
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [184]: a = np.array([10,20,30,40])
b = np.array([3,4,6,7])
mod = np.mod(a, b)
print(mod)
[1 0 0 5]
In [185]: a = np.array([10,20,30,40])
b = np.array([3,4,6,7])
remainder = np.remainder(a, b)
print(remainder)
[1 0 0 5]
[-3. 3.]
[-3. 3.]
[-3. 4.]
[-4. 3.]
[-3. 4.]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
NumPy Logs
In [191]: import numpy as np
ab = np.arange(1, 6)
log2 = np.log2(ab)
print(log2)
In [192]: ab = np.arange(1, 6)
log10 = np.log10(ab)
print(log10)
In [193]: ab = np.arange(1, 6)
log = np.log(ab)
print(log)
NumPy does not provide any function to take log at any base, so we can use the frompyfunc() function
along with inbuilt function math.log() with two input parameters and one output parameter
1.7005483074552052
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
NumPy Products
24
40320
[ 24 1680]
[ 5 12 21 32]
[ 2 6 24 120]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [200]: a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
x = np.cumprod([a , b])
print(x)
[[ 1 2 6 24]
[ 5 30 210 1680]]
[[ 1 2 3 4]
[ 5 12 21 32]
[ 45 120 231 384]]
NumPy Differences
E.g. for [1, 2, 3, 4], the discrete difference would be [2-1, 3-2, 4-3] = [1, 1, 1]
[ 5 10 -20]
E.g. for [1, 2, 3, 4], the discrete difference with n = 2 would be [2-1, 3-2, 4-3] = [1, 1, 1] , then, since n=2, we
will do it once more, with the new result: [1-1, 1-1] = [0, 0]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [204]:
a = np.array([10, 15, 25, 5])
diff = np.diff(a, n=2)
print(diff)
[ 5 -30]
[0 0]
The Lowest Common Multiple is the smallest number that is a common multiple of two numbers.
In [206]: num1 = 4
num2 = 6
x = np.lcm(num1, num2)
print(x)
12
The reduce() method will use the ufunc, in this case the lcm() function, on each element, and reduce the
array by one dimension.
18
Returns: 18 because that is the lowest common multiple of all three numbers (36=18, 63=18 and 9*2=18).
In [208]: num1 = 6
num2 = 9
x = np.gcd(num1, num2)
print(x)
3
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
Returns: 3 because that is the highest number both numbers can be divided by (6/3=2 and 9/3=3).
The reduce() method will use the ufunc, in this case the gcd() function, on each element, and reduce the
array by one dimension.
Returns: 4 because that is the highest number all values can be divided by.
NumPy provides the ufuncs sin(), cos() and tan() that take values in radians and produce the
corresponding sin, cos and tan values.
In [210]: x = np.sin(np.pi/2)
print(x)
1.0
In [214]: x = np.arcsin(1.0)
print(x)
1.5707963267948966
In [217]: # Hypotenues
base = 3
perp = 4
x = np.hypot(base, perp)
print(x)
5.0
NumPy provides the ufuncs sinh(), cosh() and tanh() that take values in radians and produce the
corresponding sinh, cosh and tanh values.
In [218]: x = np.sinh(np.pi/2)
x
Out[218]: 2.3012989023072947
In [219]: x = np.cosh(np.pi/2)
x
Out[219]: 2.5091784786580567
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [220]: x = np.tanh(np.pi/2)
x
Out[220]: 0.9171523356672744
Finding Angles
Numpy provides ufuncs arcsinh(), arccosh() and arctanh() that produce radian values for corresponding
sinh, cosh and tanh values given.
In [221]: x = np.arcsinh(1.0)
print(x)
0.881373587019543
[1 2 3 4 5 6 7]
[1 2 3 4 5 6]
[3 4]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js
In [225]: a = np.array([1, 2, 3, 4])
b = np.array([3, 4, 5, 6])
x = np.setdiff1d(a, b, assume_unique=True)
print(x)
[1 2]
[1 2 5 6]
Loading [MathJax]/jax/output/HTML-CSS/fonts/STIX-Web/fontdata.js