0% found this document useful (0 votes)
3 views

Numpy Notes Merged

Uploaded by

HARSH Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Numpy Notes Merged

Uploaded by

HARSH Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

import numpy as np

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([[ 7., 7.],


# [ 7., 7.]])

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.int64 Signed 64-bit integer types


# np.float32 Standard double-precision floating point
# np.complex Complex numbers represented by 128 floats
# np.bool Boolean type storing TRUE and FALSE values
# np.object Python object type
# np.string_ Fixed-length string type
# np.unicode_ Fixed-length unicode type

# Saving and Loading text files

# np.loadtxt("myfile.txt")
# np.genfromtxt("my_file.csv", delimiter=',')
# np.savetxt("myarray.txt", a, delimiter=" ")

# comparison
# 1. Element-wise comparison
a == b

# array([[False, True, True],


# [False, False, False]], dtype=bool)

# 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 ,slicing and Indexing

# 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])

## very very important


b[[1, 0, 1, 0]][:,[0,1,2,0]]
# Select a subset of the matrix’s rows
# array([[ 4. ,5. , 6. , 4. ], and columns
# [ 1.5, 2. , 3. , 1.5],
# [ 4. , 5. , 6. , 4. ],
# [ 1.5, 2. , 3. , 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

2. Changing Array Shape


b.ravel()
# Flatten the array
g.reshape(3,-2)
# Reshape, but don’t change data

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

array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])

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

array([99, 99, 99, 99, 99, 99])

arr

array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

arr_copy=arr.copy()
arr_copy

array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

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

array([[ 5, 10, 15],


[20, 25, 30],
[35, 40, 45]])

# arr_2d[0][2]
# arr_2d[1]
arr_2d[0,1]

10

arr_2d[:2,0:]

array([[ 5, 10, 15],


[20, 25, 30]])

arr_2d[:2]

array([[ 5, 10, 15],


[20, 25, 30]])

arr=np.arange(1,11)
arr

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

bool_arr=arr>5
bool_arr

array([False, False, False, False, False, True, True, True, True,


True])

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

ZeroDivisionError: division by zero

arr/arr

<ipython-input-46-50b4ced5627e>:1: RuntimeWarning: invalid value encountered


arr/arr
array([nan, 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

1/arr

<ipython-input-47-016353831300>:1: RuntimeWarning: divide by zero encountered


1/arr
array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,
0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111,
0.1 ])

arr**2

array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100])

np.sqrt(arr)

array([0. , 1. , 1.41421356, 1.73205081, 2. ,


2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ,
3.16227766])

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)

array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,


5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
2.98095799e+03, 8.10308393e+03, 2.20264658e+04])

np.max(arr)

10

arr.max()

10

np.sin(arr)

array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,


-0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849,
-0.54402111])

np.log(arr)

<ipython-input-55-a67b4ae04e95>:1: RuntimeWarning: divide by zero encountered


np.log(arr)
array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,
1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458,
2.30258509])

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)

array([[1., 0., 0.],


[0., 1., 0.],
[0., 0., 1.]])

np.random.rand()

0.8372832019902213

np.random.randn(25)

array([-0.53594062, 0.63815652, -1.547196 , -0.54044022, 0.49966071,


1.74185488, -2.52323902, 0.11759212, 0.17514215, -2.0782001 ,
-0.66915319, -0.8834319 , -0.66285592, 1.40269786, 1.56719378,
-0.80242937, 0.68253086, -0.49091883, 1.27686093, -0.04379424,
0.20053335, 0.26919564, -1.34220613, -1.01839723, 0.18912552])

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)

array([0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,


0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,
0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,
0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])

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:]

array([[12, 13, 14, 15],


[17, 18, 19, 20],
[22, 23, 24, 25]])

mat[3,4]

20

mat[:3,1].reshape(3,1)

array([[ 2],
[ 7],
[12]])

mat[4,]

array([21, 22, 23, 24, 25])

# 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)

array([55, 60, 65, 70, 75])

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)

array([0., 0., 0.])

np.ones(4)

array([1., 1., 1., 1.])

# np.zeros(3)
# # np.zeros((2,3))
# np.ones(4)
# np.ones((3,4))

array([[1., 1., 1., 1.],


[1., 1., 1., 1.],
[1., 1., 1., 1.]])

np.linspace(0,5,10)

array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,


2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])

np.eye(4)

array([[1., 0., 0., 0.],


[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

# np.random.rand(5)
# np.random.rand(2,3)
np.random.randn(2)

array([-0.34938613, 0.67991274])

np.random.randn(3,3)

array([[ 1.14741213, -1.25097847, -0.1229062 ],


[-0.98719806, 0.07323487, -0.12773502],

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

array([ 9, 30, 31, 16, 2, 18, 13, 26, 16, 39])

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')

from numpy.random import randint


# randint(2,10)

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

You might also like