Numpy Notes Merged
Numpy Notes Merged
a = np.array([1,2,3])
b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]],
dtype = float)
# Arithmetic Operations
# Subtraction
g = a - b
# print(np.subtract(a,b))
# array([[-0.5, 0. , 0. ],
# [-3. , -3. , -3. ]])
# Addition
b + a
# print(np.add(b,a))
# array([[ 2.5, 4. , 6. ],
# [ 5. , 7. , 9. ]])
# Division
a / b
# print(np.divide(a,b))
# array([[ 0.66666667, 1. , 1. ],
# [ 0.25 , 0.4 , 0.5 ]])
# Multiplication
a * b
# print(np.multiply(a,b))
# array([[ 1.5, 4. , 9. ],
# [ 4. , 10. , 18. ]])
# np.exp(b)
# Exponentiation
# np.sqrt(b)
# Square root
# np.sin(a)
# Print sines of an array
# np.cos(b)
# Element-wise cosine
# np.log(a)
# Element-wise natural logarithm
# Dot product
# e.dot(f)
array([[ 1.5, 4. , 9. ],
[ 4. , 10. , 18. ]])
# inspecting Array
a.shape
# Array dimensions
len(a)
# Length of array
b.ndim
# Number of array dimensions
e.size
# Number of array elements
b.dtype
# Data type of array elements
b.dtype.name
# Name of data type
b.astype(int)
# Convert an array to a different type
np.info(np.ndarray.dtype)
# Asking for help
a=np.zeros((3,4))
# Create an array of zeros
b=np.ones((2,3,4),dtype=np.int16)
# Create an array of ones
d = np.arange(10,25,5)
# Create an array of evenly spaced values (step value)
c=np.linspace(0,2,9)
# Create an array of evenly spaced values (number of samples)
e = np.full((2,2),7)
# Create a constant array
f = np.eye(2)
# Create a 2X2 identity matrix
g=np.random.random((2,2))
# Create an array with random values
h=np.empty((3,2))
# Create an empty array
# print(a)
# print(b)
# print(c)
# print(e)
# print(f)
# print(g)
# print(h)
# Data Types
# np.loadtxt("myfile.txt")
# np.genfromtxt("my_file.csv", delimiter=',')
# np.savetxt("myarray.txt", a, delimiter=" ")
# comparison
# 1. Element-wise comparison
a == b
# 2. Element-wise comparison
a < 2
# array([True, False, False], dtype=bool)
np.array_equal(a, b)
#Aggregate Functions
a.sum()
# Array-wise sum
a.min()
# Array-wise minimum value
b.max(axis=0)
# Maximum value of an array row
b.cumsum(axis=1)
# Cumulative sum of the elements
a.mean()
# Mean
b.median()
# Median
a.corrcoef()
# Correlation coefficient
np.std(b)
# Standard deviation
# Sorting Arrays
a.sort()
# Sort an array
c.sort(axis=0)
# Sort the elements of an array's axis
# Subsetting
a[2]
# Select the element at the 2nd index 3
b[1,2]
# Select the element at row 1 column 2 - 6.0 (equivalent to b[1][2])
# Slicing
a[0:2]
# Select items at index 0 and 1 ~ array([1, 2])
b[0:2,1]
# Select items at rows 0 and 1 in column 1 ~ array([ 2., 5.])
b[:1]
# Select all items at row 0 ~ array([[1.5, 2., 3.]]) (equivalent to
b[0:1, :])
c[1,...]
# Same as [1,:,:]array ~([[[ 3., 2., 1.], 4., 5., 6.]]])
a[ : :-1]
# Reversed array a array([3, 2, 1])
# Boolean Indexing
a[a<2]
# Select elements from a less than 2
array([1])
# Fancy Indexing
b[[1, 0, 1, 0],[0, 1, 2, 0]]
# Select elements (1,0),(0,1),(1,2) and (0,0) ~ array([ 4. , 2. , 6. ,
1.5])
a = np.array([1,2,3])
b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]],
# Array Manipulation
1. Transposing Array
i = np.transpose(b)
# Permute array dimensions
i.T
# Permute array dimensions
5. Adding/Removing Elements
h.resize((2,6))
# Return a new array with shape (2,6)
np.append(h,g)
# Append items to an array
np.insert(a, 1, 5)
# Insert items in an array
np.delete(a,[1])
# Delete items from an array
3. Combining Arrays
np.concatenate((a,d),axis=0)
# Concatenate arrays
# array([ 1, 2, 3, 10, 15, 20])
np.vstack((a,b))
# Stack arrays vertically (row-wise)
# array([[ 1. , 2. , 3. ],
# [ 1.5, 2. , 3. ],
# [ 4. , 5. , 6. ]])
np.r_[e,f]
# Stack arrays vertically (row-wise)
np.hstack((e,f))
# Stack arrays horizontally (column-wise)
# array([[ 7., 7., 1., 0.],
# [ 7., 7., 0., 1.]])
np.column_stack((a,d))
# Create stacked column-wise arrays
# array([[ 1, 10],
# [ 2, 15],
# [ 3, 20]])
np.c_[a,d]
# Create stacked column-wise arrays
4. Splitting Arrays
np.hsplit(a,3)
# Split the array horizontally at the 3rd
# [array([1]),array([2]),array([3])] index
np.vsplit(c,2)
# Split the array vertically at the 2nd index
# [array([[[ 1.5, 2. , 1. ],
# [ 4. , 5. , 6. ]]]),
# array([[[ 3., 2., 3.],
# [ 4., 5., 6.]]])]
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
import numpy as np
arr=np.arange(0,11)
arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# arr[8]
arr[1:5]
arr[0:5]
arr[0:6]
arr[5:]
array([ 5, 6, 7, 8, 9, 10])
arr[0:5]=100
arr
arr=np.arange(0,11)
arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
slice=arr[0:6]
slice
array([0, 1, 2, 3, 4, 5])
slice[:]=99
slice
arr
arr_copy=arr.copy()
arr_copy
arr_copy[:]=100
arr_copy
array([100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100])
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 1/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
arr_2d=np.array([[5,10,15],[20,25,30],[35,40,45]])
arr_2d
# arr_2d[0][2]
# arr_2d[1]
arr_2d[0,1]
10
arr_2d[:2,0:]
arr_2d[:2]
arr=np.arange(1,11)
arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bool_arr=arr>5
bool_arr
arr[bool_arr]
arr[arr>5]
array([ 6, 7, 8, 9, 10])
arr[arr<3]
array([1, 2])
arr_2d=np.arange(50).reshape(5,10)
arr_2d
arr_2d[:,:]
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 2/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]])
Numpy Operations
arr=np.arange(0,11)
arr
arr+arr
arr-arr
arr*arr
arr+100
arr*100
array([ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000])
1/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-45-9e1622b385b6> in <cell line: 1>()
----> 1 1/0
arr/arr
1/arr
arr**2
np.sqrt(arr)
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 3/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
np.exp(arr)
np.max(arr)
10
arr.max()
10
np.sin(arr)
np.log(arr)
import numpy as np
arr=np.zeros(10)
arr
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones(10)*5
array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])
np.zeros(10)+5
array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])
np.arange(10,51)
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50])
np.arange(10,51,2)
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 4/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
44, 46, 48, 50])
# arr_2d=np.arange(9)
# arr_2d.reshape(3,3)
np.arange(9).reshape(3,3)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
np.eye(3)
np.random.rand()
0.8372832019902213
np.random.randn(25)
np.random.randn(25).shape
(25,)
x=np.arange(1,101)/100
x.reshape(10,10)
array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
[0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
np.linspace(0.01,1,100).reshape(10,10)
array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],
[0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],
[0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 5/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
[0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],
[0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],
[0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],
[0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],
[0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],
[0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],
[0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])
np.linspace(0,1,20)
mat=np.arange(1,26).reshape(5,5)
mat
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
mat[2:,1:]
mat[3,4]
20
mat[:3,1].reshape(3,1)
array([[ 2],
[ 7],
[12]])
mat[4,]
# np.sum(mat)
mat.sum()
325
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 6/7
04/08/2024, 17:57 Numpy_indexing_and_selection - Colab
np.std(mat)
mat.std()
# standard Deviation
7.211102550927978
mat.sum(axis=0)
https://colab.research.google.com/drive/1nJSuXYvQFgtW8AEuMtRp5FhyLm1GD_35#printMode=true 7/7
04/08/2024, 17:57 Colab_Numpy_project - Colab
import numpy as np
my_list=[1,2,3]
arr=np.array(my_list)
arr
array([1, 2, 3])
my_mat=[[1,2,3],[4,5,6],[7,8,9]]
np.array(my_mat)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.arange(0,10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,11,2)
array([ 0, 2, 4, 6, 8, 10])
np.zeros(3)
np.ones(4)
# np.zeros(3)
# # np.zeros((2,3))
# np.ones(4)
# np.ones((3,4))
np.linspace(0,5,10)
np.eye(4)
# np.random.rand(5)
# np.random.rand(2,3)
np.random.randn(2)
array([-0.34938613, 0.67991274])
np.random.randn(3,3)
https://colab.research.google.com/drive/1qLuVoNuaC7h4vVUZR9zyqIra2O0zG8On#scrollTo=YqPczDebSgY_&printMode=true 1/3
04/08/2024, 17:57 Colab_Numpy_project - Colab
[ 0.33541048, 1.3689871 , 1.50550368]])
# np.random.randint(1,100,10)
np.random.randint(1,100)
94
# arr=np.arange(25)
# arr
ranarr=np.random.randint(0,50,10)
ranarr
arr.reshape(5,5)
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
# arr.reshape(5,10)
# ranarr.max()
# ranarr.min()
# ranarr.argmax()
ranarr.argmin()
arr=arr.reshape(5,5)
arr.shape
(5, 5)
arr.dtype
dtype('int64')
https://colab.research.google.com/drive/1qLuVoNuaC7h4vVUZR9zyqIra2O0zG8On#scrollTo=YqPczDebSgY_&printMode=true 2/3
04/08/2024, 17:57 Colab_Numpy_project - Colab
https://colab.research.google.com/drive/1qLuVoNuaC7h4vVUZR9zyqIra2O0zG8On#scrollTo=YqPczDebSgY_&printMode=true 3/3