31/03/2023, 22:02 NumPy - Jupyter Notebook
NumPy - Numerical Python
[Link] ([Link]
NumPy is the fundamental package for scientific computing in Python
NumPy is a Python library that provides a multidimensional array object, various derived objects and and an assortment of routines for fast operations on arrays,
including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random
simulation and much more.
Agenda
What is Numpy ?
Installation of Numpy
Attributes of NumPy
Common mistakes while creating numpy arrays
Creating numpy arrays
Creating & Accessing 1D arrays
Creating & Accessing 2D arrays
Basic methods
Basic operations
Purpose of learning Numpy
Points to Remember
1. NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the
original.
2. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory. The exception: one can have arrays of
(Python, including NumPy) objects, thereby allowing for arrays of different sized elements.
3. NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and
with less code than is possible using Python’s built-in sequences.
Installation of NumPy module
pip install numpy
conda install numpy
Installation of matplotlib
pip install matplotlib
Installation of pandas
pip install pandas
Getting started with NumPy
In [290]:
import numpy as np
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 1/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
Creating an array
In [21]:
arr = [Link]([2, 3, 4])
print(type(arr))
print(arr)
<class '[Link]'>
[2 3 4]
ndarray(array) attributes
[Link] - the number of axes (dimensions) of the array.
[Link] - the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
[Link] - an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy
provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
[Link]- the size in bytes of each element of the array.
[Link] - the total number of elements of the array. This is equal to the product of the elements of shape.
In [22]:
[Link]
Out[22]:
In [23]:
[Link]
Out[23]:
(3,)
In [20]:
[Link]
Out[20]:
dtype('int32')
In [21]:
[Link]
Out[21]:
In [22]:
[Link]
Out[22]:
Common mistakes while creating numpy arrays
In [28]:
a = [Link](1, 2, 3, 4) # Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/[Link] in <module>
----> 1 a = [Link](1, 2, 3, 4) # Error
TypeError: array() takes from 1 to 2 positional arguments but 4 were given
In [29]:
a = [Link]([1,2,3,4]) # Error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/[Link] in <module>
----> 1 a = [Link]([1,2,3,4]) # Error
NameError: name 'numpy' is not defined
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 2/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [32]:
a = [Link][1,2,3,4] # Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/[Link] in <module>
----> 1 a = [Link][1,2,3,4] # Error
TypeError: 'builtin_function_or_method' object is not subscriptable
In [36]:
a = [Link]()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/[Link] in <module>
----> 1 a = [Link]()
TypeError: array() missing required argument 'object' (pos 1)
In [37]:
a = [Link]([1,2,3],[4,5,6])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/[Link] in <module>
----> 1 a = [Link]([1,2,3],[4,5,6])
TypeError: Field elements must be 2- or 3-tuples, got '4'
Diffrent Ways of creating an array in NumPy
In [24]:
arr = [Link]([1,2,3])
arr
Out[24]:
array([1, 2, 3])
In [25]:
arr1 = [Link]((1,2,3)) # Implicitly tuple of elements converted as list type
arr1
Out[25]:
array([1, 2, 3])
In [26]:
arr2 = [Link]([i for i in range(10)])
arr2
Out[26]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [27]:
arr2 = [Link](10)
arr2
Out[27]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [58]:
arr3 = [Link](10,20)
arr3
Out[58]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 3/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [59]:
arr4 = [Link](10,20,2)
arr4
Out[59]:
array([10, 12, 14, 16, 18])
In [60]:
arr5 = [Link](0,10,0.5)
arr5
Out[60]:
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ,
6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])
In [30]:
arr6 = [Link](0,2,10)
print(arr6)
print([Link])
[0. 0.22222222 0.44444444 0.66666667 0.88888889 1.11111111
1.33333333 1.55555556 1.77777778 2. ]
10
Creating different datatype elements into array
In [32]:
arr1 = [Link]([1,2,3,4,5],dtype=int)
print(arr1)
[1 2 3 4 5]
In [33]:
arr2 = [Link]([1,2,3,4,5],dtype=float)
arr2
Out[33]:
array([1., 2., 3., 4., 5.])
In [34]:
arr2 = [Link]([1,2,3,4,5],dtype=complex)
arr2
Out[34]:
array([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j])
In [91]:
arr3 = [Link](10)
arr3
Out[91]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [38]:
arr3 = [Link](10)
arr3
Out[38]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [130]:
arr4 = [Link](4) # creating random numbers depends on size of memory
print(arr4)
[ 7.2 72.01090909 13.575 82.01090909]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 4/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
Creating 1D arrays
In [41]:
arr_1d =[Link]([1,2,3,4,5,6])
print(arr_1d)
[1 2 3 4 5 6]
In [42]:
arr_1d = [Link]([1,2,3,4,5,6])
print(arr_1d)
[1 2 3 4 5 6]
In [43]:
print(arr_1d.ndim)
print(arr_1d.shape)
print(arr_1d.size)
print(arr_1d.itemsize)
1
(6,)
6
4
[Link]() Vs [Link]()
In [237]:
arr1 =[Link]([1,2,3,4,5,6])
print(arr1)
a = [Link](arr1)
arr1[0]=10
print(a)
[1 2 3 4 5 6]
[1 2 3 4 5 6]
In [238]:
arr1 =[Link]([1,2,3,4,5,6])
print(arr1)
a = [Link](arr1)
arr1[0]=10
print(a)
[1 2 3 4 5 6]
[10 2 3 4 5 6]
Accessing 1D arrays
In [109]:
print(arr_1d)
[1 2 3 4 5 6]
In [44]:
print(arr_1d[1]) # accessing through indexing
In [45]:
print(arr_1d[1:6]) # accesing start and stop indices
[2 3 4 5 6]
In [117]:
print(arr_1d[0:4:2]) # accesing start , stop and step indices
[1 3]
In [46]:
for i in arr_1d: # accessing individual elements
print(i,end=" ")
1 2 3 4 5 6
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 5/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [47]:
for i in range(arr_1d.size):
print(arr_1d[i],end=' ')
1 2 3 4 5 6
In [272]:
for i in [Link](arr_1d):
print(i,end=" ")
1 2 3 4 5 6
Creating 2D arrays
In [291]:
arr_2d =[Link]([[1,2,3],[3,4,5]])
print(arr_2d)
[[1 2 3]
[3 4 5]]
In [292]:
print(arr_2d.ndim)
print(arr_2d.shape)
print(arr_2d.size)
print(arr_2d.itemsize)
2
(2, 3)
6
4
Accessing 2D arrays
In [56]:
print(arr_2d[0][0]) # accessing individual elements through indexes
print(arr_2d[0][2])
1
3
In [58]:
for i in range(arr_2d.shape[0]):
for j in range(arr_2d.shape[1]):
print(arr_2d[i][j],end=' ')
print()
1 2 3
3 4 5
In [59]:
for i in [Link](arr_2d):
print(i,end=" ")
1 2 3 3 4 5
In [247]:
print(arr_2d[0,:]) # accessing rows
print(arr_2d[[0,1],:])
[1 2 3]
[[1 2 3]
[3 4 5]]
In [286]:
print(arr_2d[:,0]) # accessing columns
print(arr_2d[:,[0,1]])
[1 3]
[[1 2]
[3 4]]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 6/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
Creating 3D arrays
In [252]:
arr_3d =[Link]([[[1,2,3],[3,4,5]]])
print(arr_3d)
[[[1 2 3]
[3 4 5]]]
In [253]:
print(arr_3d.ndim)
print(arr_3d.shape)
print(arr_3d.size)
print(arr_3d.itemsize)
3
(1, 2, 3)
6
4
Basic methods
reshape()
In [293]:
arr = [Link]([1,2,3,4,5,6])
print(arr)
[1 2 3 4 5 6]
In [298]:
arr1 = [Link](3,2)
print(arr1)
[[1 2]
[3 4]
[5 6]]
In [299]:
arr2 = [Link](1,2,3)
print(arr2)
[[[1 2 3]
[4 5 6]]]
In [305]:
arr3 = [Link](20).reshape(5,4)
In [306]:
print(arr3)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
zeroes()
In [308]:
arr = [Link](20, dtype=int)
print(arr)
print([Link])
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
20
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 7/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [309]:
arr = [Link](20).reshape(10,2)
print(arr)
print(arr[0].dtype)
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
float64
ones()
In [310]:
arr = [Link](20, dtype=int)
print(arr)
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
In [312]:
arr = [Link](15).reshape(3,5)
print(arr)
print(arr[0].dtype)
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
float64
full()
In [315]:
arr = [Link](20,100,dtype=int) # [Link](size,specific_element,dtype(optional))
print(arr)
[100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100]
In [140]:
arr = [Link]((20,10),100,dtype=int) # [Link](size,specific_element,dtype(optional))
print(arr)
print([Link])
[[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]]
(20, 10)
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 8/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
random.function_name()
In [316]:
arr = [Link](5) # [Link](high)
print(arr)
arr = [Link](5,10) # [Link](low,high)
print(arr)
arr = [Link](5,10,(5,4)) # [Link](low,high,size)
print(arr)
4
8
[[8 7 6 7]
[5 5 6 6]
[5 9 5 6]
[5 8 6 6]
[7 9 5 6]]
In [319]:
arr = [Link](5) # [Link](size)
print(arr)
arr = [Link](10,5) # [Link](size)
print([Link](2)*100)
[0.92195194 0.66749654 0.32349116 0.34175689 0.08117425]
[[61. 29. 69. 14. 63.]
[59. 61. 62. 88. 23.]
[51. 85. 76. 28. 97.]
[ 1. 68. 99. 4. 34.]
[99. 86. 79. 11. 86.]
[75. 52. 32. 34. 1.]
[77. 81. 2. 1. 61.]
[36. 68. 81. 84. 27.]
[ 3. 47. 45. 49. 84.]
[53. 20. 99. 38. 27.]]
In [320]:
arr = [Link](5) # [Link](size) # Normal distribution
print(arr)
arr = [Link](5,5) # [Link](size) # Normal distribution
print(arr)
[ 1.7527149 -1.73022947 -1.90443615 1.16617911 0.70855123]
[[ 0.36581035 0.84286698 -1.13385003 -0.07308287 -0.23091336]
[-0.60498866 -0.63880708 -0.24001324 -0.14369223 -0.31448265]
[ 0.54214294 1.95614276 -0.08657075 -0.6290693 0.61503756]
[ 0.78482507 1.65237007 0.15619992 -1.96165718 0.29487076]
[-0.188973 0.70256688 -1.25555483 -2.08202672 0.03872913]]
In [321]:
arr = [Link].random_integers(5) # [Link].random_integers(high)
print(arr)
arr = [Link].random_integers(5,10) # [Link].random_integers(low,high)
print(arr)
arr = [Link].random_integers(5,10,(5,4)) # [Link].random_integers(low,high,size)
print(arr)
1
10
[[ 7 8 7 5]
[ 7 5 6 7]
[ 9 9 10 5]
[10 10 7 10]
[10 8 8 10]]
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/[Link]: DeprecationWarning: This function is deprecated. Please
call randint(1, 5 + 1) instead
arr = [Link].random_integers(5) # [Link].random_integers(high)
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/[Link]: DeprecationWarning: This function is deprecated. Please
call randint(5, 10 + 1) instead
arr = [Link].random_integers(5,10) # [Link].random_integers(low,high)
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/[Link]: DeprecationWarning: This function is deprecated. Please
call randint(5, 10 + 1) instead
arr = [Link].random_integers(5,10,(5,4)) # [Link].random_integers(low,high,size)
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 9/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [322]:
arr = [Link]((5)) # [Link]((size))
print(arr)
arr = [Link]((5,5)) # [Link](size))
print(arr)
arr = [Link]((3,3))*100 # [Link]((size))
print(arr)
[0.79664026 0.97866786 0.44653673 0.22461895 0.97056016]
[[0.759017 0.51371729 0.22325438 0.89690992 0.56237335]
[0.99145375 0.55458184 0.5171857 0.8407703 0.34305884]
[0.06633229 0.99230944 0.69754348 0.58391495 0.84018751]
[0.54468708 0.74748239 0.34187153 0.03774866 0.92692183]
[0.44398463 0.56919038 0.4833841 0.3015593 0.50388838]]
[[12.02046778 41.3972354 68.67775175]
[40.60679156 11.64386541 64.59442756]
[58.63871789 3.39190296 28.17041254]]
In [323]:
arr = [Link](10) # [Link](high)
print(arr)
arr = [Link](20,3) # [Link](high,size)
print(arr)
arr = [Link](20,(2,3)) # [Link](high,size)
print(arr)
arr = [Link](['sreenivas','Kranthi','Ram','Sam'],20) # [Link](collection,size)
print(arr)
arr = [Link](['sreenivas','Kranthi','Ram','Sam'],(5,5)) # [Link](collection,size)
print(arr)
1
[11 19 8]
[[17 19 13]
[10 15 11]]
['Ram' 'Kranthi' 'sreenivas' 'Ram' 'Ram' 'Ram' 'Sam' 'Kranthi' 'sreenivas'
'sreenivas' 'Sam' 'sreenivas' 'sreenivas' 'Sam' 'Kranthi' 'Ram'
'sreenivas' 'sreenivas' 'Kranthi' 'sreenivas']
[['sreenivas' 'Sam' 'Kranthi' 'sreenivas' 'Sam']
['Kranthi' 'Kranthi' 'Ram' 'Sam' 'Kranthi']
['Kranthi' 'sreenivas' 'Sam' 'Sam' 'Sam']
['Kranthi' 'sreenivas' 'Kranthi' 'Sam' 'sreenivas']
['Kranthi' 'sreenivas' 'Ram' 'Sam' 'Ram']]
In [324]:
arr = [Link](20)
print(arr)
[Link](arr) # [Link](collection)
print(arr)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[12 2 16 17 1 6 9 13 3 5 0 11 10 18 8 7 19 14 4 15]
vstack()
In [230]:
arr1 = [Link](20).reshape(5,4)
print(arr1)
arr2 = [Link](20).reshape(5,4)
print(arr2)
print([Link]((arr1,arr2)))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 10/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
hstack()
In [231]:
arr1 = [Link](20).reshape(5,4)
print(arr1)
arr2 = [Link](20).reshape(5,4)
print(arr2)
print([Link]((arr1,arr2)))
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
[[ 0 1 2 3 0 1 2 3]
[ 4 5 6 7 4 5 6 7]
[ 8 9 10 11 8 9 10 11]
[12 13 14 15 12 13 14 15]
[16 17 18 19 16 17 18 19]]
append() and insert()
In [325]:
arr = [Link](20)
print(arr)
print([Link](arr,30)) # [Link](existed_collection,element)
a = [Link](arr,[10,203,33]) # [Link](existed_collection,collections)
print(a)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 30]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 10 203 33]
In [237]:
arr = [Link](20)
print(arr)
a = [Link](arr,3,30) # [Link](existed_collection,index,element) # addding one element
print(a)
a = [Link](arr,3,[100,200,300,400]) # [Link](existed_collection,index,collection) # adding multiple element
print(a)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 2 30 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 2 100 200 300 400 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19]
delete()
In [326]:
arr = [Link](20)
print(arr)
arr = [Link](arr,5) # [Link](existed_collection,element) # deleting one element
print(arr)
arr = [Link](arr,[2,3,4]) # [Link](existed_collection,collection) # deleting multiple element
print(arr)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 11/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
where()
In [259]:
arr = [Link](20)
print(arr)
arr1 = [Link](arr>10) # [Link](condition)
print(arr1)
print(arr1[0])
arr = [Link]((arr > 10) & (arr<15)) # [Link](multiple conditions)
print(arr)
print(arr[0])
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
(array([11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int64),)
[11 12 13 14 15 16 17 18 19]
(array([11, 12, 13, 14], dtype=int64),)
[11 12 13 14]
Basic operations
In [421]:
# Operations on one array
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
print(arr1+10) # Addition
print(arr1-10) # Subtraction
print(arr1*10) # Multiplication
print(arr1/12) # Division
print(arr1//2) # Integer Division
print(arr1**2) # Power
[[10 11 12 13 14]
[15 16 17 18 19]]
[[20 21 22 23 24]
[25 26 27 28 29]]
[[0 1 2 3 4]
[5 6 7 8 9]]
[[100 110 120 130 140]
[150 160 170 180 190]]
[[0.83333333 0.91666667 1. 1.08333333 1.16666667]
[1.25 1.33333333 1.41666667 1.5 1.58333333]]
[[5 5 6 6 7]
[7 8 8 9 9]]
[[100 121 144 169 196]
[225 256 289 324 361]]
In [260]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print([Link](arr1,arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[20 22 24 26 28]
[30 32 34 36 38]]
In [261]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print([Link](arr1,arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[0 0 0 0 0]
[0 0 0 0 0]]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 12/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [262]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print([Link](arr1,arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[100 121 144 169 196]
[225 256 289 324 361]]
In [263]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print([Link](arr1,arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
In [327]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print(arr1*arr2)
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
[[100 121 144 169 196]
[225 256 289 324 361]]
In [328]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(5,2)
print(arr2)
print(arr1@arr2)
print([Link](arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
[[ 860 920]
[1210 1295]]
[[ 860 920]
[1210 1295]]
In [267]:
arr2 = [Link](10,20).reshape(5,2)
print(arr2)
print(arr2.T)
[[10 11]
[12 13]
[14 15]
[16 17]
[18 19]]
[[10 12 14 16 18]
[11 13 15 17 19]]
In [329]:
arr2 = [Link]([[1,0],[0,1]])
print(arr2)
print([Link](arr2))
[[1 0]
[0 1]]
[[1. 0.]
[0. 1.]]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 13/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [269]:
arr1 = [Link](10,20).reshape(2,5)
print(arr1)
arr2 = [Link](10,20).reshape(2,5)
print(arr2)
print(np.array_equal(arr1,arr2))
[[10 11 12 13 14]
[15 16 17 18 19]]
[[10 11 12 13 14]
[15 16 17 18 19]]
True
In [330]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
[[2.20264658e+04 5.98741417e+04 1.62754791e+05 4.42413392e+05
1.20260428e+06]
[3.26901737e+06 8.88611052e+06 2.41549528e+07 6.56599691e+07
1.78482301e+08]]
In [331]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
[[3.16227766 3.31662479 3.46410162 3.60555128 3.74165739]
[3.87298335 4. 4.12310563 4.24264069 4.35889894]]
In [332]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
10
In [433]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
19
In [333]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
14.5
In [334]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
14.5
In [335]:
arr1 = [Link](10,20).reshape(2,5)
print([Link](arr1))
145
In [336]:
arr1 = [Link]([10,2,532,32,3,23])
print([Link](arr1))
[ 2 3 10 23 32 532]
In [447]:
arr1 = [Link]([[10,2,532],[32,3,23]])
print([Link](arr1,axis=1))
[[ 2 10 532]
[ 3 23 32]]
In [448]:
arr1 = [Link]([[10,2,532],[32,3,23]])
print([Link](arr1,axis=0))
[[ 10 2 23]
[ 32 3 532]]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 14/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [449]:
arr1 = [Link]([[10,2,532],[32,3,23]])
print([Link](arr1))
[[ 2 10 532]
[ 3 23 32]]
Purpose of Learning NumPy
In [337]:
import pandas as pd
In [338]:
sno = [Link](low = 1, high =1000, size = [10000,])
names = [Link](['Sreenivas','Ram','Kranthi','Mani','Sam'],10000)
gender = [Link](['Male','Female'],10000)
In [339]:
df = [Link]({"Serial Number":sno," Name ":names,"Gender":gender})
df
Out[339]:
Serial Number Name Gender
0 919 Ram Female
1 979 Ram Female
2 668 Kranthi Male
3 534 Ram Female
4 475 Mani Male
... ... ... ...
9995 886 Mani Female
9996 85 Kranthi Female
9997 644 Ram Female
9998 991 Mani Female
9999 570 Ram Male
10000 rows × 3 columns
In [340]:
[Link]
Out[340]:
(10000, 3)
In [341]:
import [Link] as plt
In [273]:
x = [Link](10)
y = [Link](10,20)
z = [Link](20,30)
print(x)
print(y)
[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 15/16
31/03/2023, 22:02 NumPy - Jupyter Notebook
In [274]:
[Link](y,x,c='g',marker='*',s=100)
[Link](z,x,c='r',s=100)
[Link]()
In [284]:
x = [Link].random_integers(5,500,100)
print(x)
y = [Link].random_integers(5,500,100)
print(y)
[Link](x,y,c='blue',s=20)
[Link]()
[ 93 139 421 249 289 312 245 488 401 150 292 180 244 58 47 470 330 487
390 104 256 413 174 147 293 422 345 286 356 98 346 268 364 79 379 314
125 101 95 223 187 168 111 203 121 137 13 355 496 51 323 416 436 351
61 55 492 367 451 190 268 43 200 336 343 427 64 236 9 393 226 93
131 431 413 431 489 85 219 244 77 327 159 41 240 211 48 220 16 302
240 115 364 347 137 466 168 453 98 241]
[342 229 316 97 490 236 77 473 490 272 333 282 317 107 248 415 67 132
91 109 272 349 194 326 90 18 173 345 110 205 489 385 264 426 173 216
474 131 178 112 82 367 70 37 172 294 161 176 251 277 192 459 333 177
485 290 466 420 132 383 322 461 186 342 319 426 485 424 126 112 74 428
471 332 401 267 46 310 76 49 219 457 278 185 122 59 397 228 180 465
426 63 149 243 93 479 121 43 366 491]
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/[Link]: DeprecationWarning: This function is deprecated. Please
call randint(5, 500 + 1) instead
x = [Link].random_integers(5,500,100)
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/[Link]: DeprecationWarning: This function is deprecated. Please
call randint(5, 500 + 1) instead
y = [Link].random_integers(5,500,100)
In [ ]:
localhost:8888/notebooks/Python Bootcamp/[Link]#random.function_name() 16/16