29/03/2023, 20:40 NumPy - Jupyter Notebook
NumPy - Numerical Python
https://numpy.org/ (https://numpy.org/)
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 [17]:
import numpy as np
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 1/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
Creating an array
In [21]:
arr = np.array([2, 3, 4])
print(type(arr))
print(arr)
<class 'numpy.ndarray'>
[2 3 4]
ndarray(array) attributes
ndarray.ndim - the number of axes (dimensions) of the array.
ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
ndarray.dtype - 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.
ndarray.itemsize- the size in bytes of each element of the array.
ndarray.size - the total number of elements of the array. This is equal to the product of the elements of shape.
In [22]:
arr.ndim
Out[22]:
In [23]:
arr.shape
Out[23]:
(3,)
In [20]:
arr.dtype
Out[20]:
dtype('int32')
In [21]:
arr.itemsize
Out[21]:
In [22]:
arr.size
Out[22]:
Common mistakes while creating numpy arrays
In [28]:
a = np.array(1, 2, 3, 4) # Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/2326401496.py in <module>
----> 1 a = np.array(1, 2, 3, 4) # Error
TypeError: array() takes from 1 to 2 positional arguments but 4 were given
In [29]:
a = numpy.array([1,2,3,4]) # Error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/259882583.py in <module>
----> 1 a = numpy.array([1,2,3,4]) # Error
NameError: name 'numpy' is not defined
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 2/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [32]:
a = np.array[1,2,3,4] # Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/2601176658.py in <module>
----> 1 a = np.array[1,2,3,4] # Error
TypeError: 'builtin_function_or_method' object is not subscriptable
In [36]:
a = np.array()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/1326429237.py in <module>
----> 1 a = np.array()
TypeError: array() missing required argument 'object' (pos 1)
In [37]:
a = np.array([1,2,3],[4,5,6])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12688/2132018308.py in <module>
----> 1 a = np.array([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 = np.array([1,2,3])
arr
Out[24]:
array([1, 2, 3])
In [25]:
arr1 = np.array((1,2,3)) # Implicitly tuple of elements converted as list type
arr1
Out[25]:
array([1, 2, 3])
In [26]:
arr2 = np.array([i for i in range(10)])
arr2
Out[26]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [27]:
arr2 = np.arange(10)
arr2
Out[27]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [58]:
arr3 = np.arange(10,20)
arr3
Out[58]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [59]:
arr4 = np.arange(10,20,2)
arr4
Out[59]:
array([10, 12, 14, 16, 18])
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 3/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [60]:
arr5 = np.arange(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 = np.linspace(0,2,10)
print(arr6)
print(arr6.size)
[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 = np.array([1,2,3,4,5],dtype=int)
print(arr1)
[1 2 3 4 5]
In [33]:
arr2 = np.array([1,2,3,4,5],dtype=float)
arr2
Out[33]:
array([1., 2., 3., 4., 5.])
In [34]:
arr2 = np.array([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 = np.zeros(10)
arr3
Out[91]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [38]:
arr3 = np.ones(10)
arr3
Out[38]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [40]:
arr4 = np.empty(4) # creating random numbers depends on size of memory
print(arr4)
[1. 0. 0. 1.]
Creating 1D arrays
In [41]:
arr_1d =np.array([1,2,3,4,5,6])
print(arr_1d)
[1 2 3 4 5 6]
In [42]:
arr_1d = np.asarray([1,2,3,4,5,6])
print(arr_1d)
[1 2 3 4 5 6]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 4/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [43]:
print(arr_1d.ndim)
print(arr_1d.shape)
print(arr_1d.size)
print(arr_1d.itemsize)
1
(6,)
6
4
np.array() Vs np.asarray()
In [237]:
arr1 =np.array([1,2,3,4,5,6])
print(arr1)
a = np.array(arr1)
arr1[0]=10
print(a)
[1 2 3 4 5 6]
[1 2 3 4 5 6]
In [238]:
arr1 =np.asarray([1,2,3,4,5,6])
print(arr1)
a = np.asarray(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
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 np.nditer(arr_1d):
print(i,end=" ")
1 2 3 4 5 6
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 5/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
Creating 2D arrays
In [48]:
arr_2d =np.array([[1,2,3],[3,4,5]])
print(arr_2d)
[[1 2 3]
[3 4 5]]
In [55]:
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 np.nditer(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 [63]:
print(arr_2d[:,0]) # accessing columns
print(arr_2d[:,:])
[1 3]
[[1 2 3]
[3 4 5]]
Creating 3D arrays
In [252]:
arr_3d =np.array([[[1,2,3],[3,4,5]]])
print(arr_3d)
[[[1 2 3]
[3 4 5]]]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 6/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
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 [69]:
arr = np.array([1,2,3,4,5,6])
print(arr)
[1 2 3 4 5 6]
In [70]:
arr1 = arr.reshape(1,6)
print(arr1)
[[1 2 3 4 5 6]]
In [72]:
arr2 = arr.reshape(1,2,3)
print(arr2)
[[[1 2 3]
[4 5 6]]]
In [77]:
arr3 = np.arange(20).reshape(5,4)
In [78]:
print(arr3)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
zeroes()
In [286]:
arr = np.zeros(20, dtype=int)
print(arr)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
In [79]:
arr = np.zeros(20).reshape(5,4)
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 [295]:
arr = np.ones(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]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 7/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [296]:
arr = np.ones(20).reshape(5,4)
print(arr)
print(arr[0].dtype)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
float64
full()
In [82]:
arr = np.full(20, 100,dtype=int)
print(arr)
[100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100]
In [84]:
arr = np.full((20,10), 0,dtype=int)
print(arr)
print(arr.shape)
[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
(20, 10)
random.function_name()
In [87]:
arr = np.random.randint(10,20,10)
print(arr)
[13 13 14 10 11 15 18 13 10 17]
In [91]:
arr = np.random.rand(5)
print(arr)
[0.58748405 0.56893266 0.26094098 0.63075255 0.97047465]
In [328]:
arr = np.random.randn(5,5)
print(arr)
[[ 2.30319918 2.61235677 0.27914347 -0.28002049 1.15846259]
[-2.14798237 -1.43719389 -0.29861044 -0.0986336 0.66753633]
[-0.2293047 -0.65340018 0.04108155 0.69578437 0.02061387]
[ 0.05094432 -1.67694569 -0.68071051 0.87500703 -0.37681401]
[-0.77426742 -1.47868711 2.53816137 -0.74537811 0.27507264]]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 8/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [94]:
arr = np.random.random_integers(5,10,size=(5,5))
print(arr)
[[ 9 6 10 6 6]
[ 7 6 8 5 6]
[ 8 7 9 9 9]
[ 8 5 7 8 10]
[ 8 9 10 5 6]]
C:\Users\chinu\AppData\Local\Temp/ipykernel_12272/3993291982.py:1: DeprecationWarning: This function is deprecated. Please
call randint(5, 10 + 1) instead
arr = np.random.random_integers(5,10,size=(5,5))
In [96]:
arr = np.random.random((5))
print(arr)
[0.38633693 0.27505836 0.69015322 0.37678293 0.69804673]
In [98]:
arr = np.random.choice(10,5)
print(arr)
arr = np.random.choice(['s','r','jdfjakf','sjfdaj'],20)
print(arr)
[1 9 7 3 6]
['r' 'r' 'jdfjakf' 'r' 'r' 'jdfjakf' 's' 's' 'jdfjakf' 'r' 'jdfjakf'
'jdfjakf' 'r' 'r' 'r' 'r' 'r' 'r' 's' 's']
In [100]:
arr = np.arange(20)
print(arr)
np.random.shuffle(arr)
print(arr)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 1 17 11 5 8 2 15 3 14 7 10 0 16 9 6 4 19 13 18 12]
vstack()
In [101]:
arr1 = np.arange(20).reshape(5,4)
print(arr1)
arr2 = np.arange(20).reshape(5,4)
print(arr2)
np.vstack((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]]
Out[101]:
array([[ 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/NumPy.ipynb# 9/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
hstack()
In [102]:
arr1 = np.arange(20).reshape(5,4)
print(arr1)
arr2 = np.arange(20).reshape(5,4)
print(arr2)
np.hstack((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]]
Out[102]:
array([[ 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 [104]:
arr = np.arange(20)
print(arr)
np.append(arr,30)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Out[104]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 30])
In [105]:
arr = np.arange(20)
print(arr)
a = np.append(arr,[10,203,33])
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 10 203 33]
In [106]:
arr = np.arange(20)
print(arr)
a = np.insert(arr,3,[10,203,33])
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 10 203 33 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19]
delete()
In [110]:
arr = np.arange(20)
print(arr)
a = np.delete(arr,5)
print(a)
a = np.delete(arr,[2,3,4])
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 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 0 1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 10/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
where()
In [114]:
arr = np.arange(20)
print(arr)
arr1 = np.where(arr>10)
print(arr1)
print(arr1[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]
Basic operations
In [421]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
print(arr1+10)
print(arr1-10)
print(arr1*10)
print(arr1/12)
print(arr1//2)
print(arr1**2)
[[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 [422]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(10,20).reshape(2,5)
print(arr2)
print(np.add(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 [424]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(10,20).reshape(2,5)
print(arr2)
print(np.subtract(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]]
In [425]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(10,20).reshape(2,5)
print(arr2)
print(np.multiply(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]]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 11/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [426]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(10,20).reshape(2,5)
print(arr2)
print(np.divide(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 [4]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(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 [8]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(10,20).reshape(5,2)
print(arr2)
print(arr1@arr2)
print(arr1.dot(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 [115]:
arr2 = np.arange(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 [116]:
arr2 = np.array([[1,0],[0,1]])
print(arr2)
print(np.linalg.inv(arr2))
[[1 0]
[0 1]]
[[1. 0.]
[0. 1.]]
In [117]:
arr1 = np.arange(10,20).reshape(2,5)
print(arr1)
arr2 = np.arange(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
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 12/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [118]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.exp(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 [430]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.sqrt(arr1))
[[3.16227766 3.31662479 3.46410162 3.60555128 3.74165739]
[3.87298335 4. 4.12310563 4.24264069 4.35889894]]
In [432]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.min(arr1))
10
In [433]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.max(arr1))
19
In [434]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.mean(arr1))
14.5
In [435]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.median(arr1))
14.5
In [437]:
arr1 = np.arange(10,20).reshape(2,5)
print(np.sum(arr1))
145
In [445]:
arr1 = np.array([10,2,532,32,3,23])
print(np.sort(arr1))
[ 2 3 10 23 32 532]
In [447]:
arr1 = np.array([[10,2,532],[32,3,23]])
print(np.sort(arr1,axis=1))
[[ 2 10 532]
[ 3 23 32]]
In [448]:
arr1 = np.array([[10,2,532],[32,3,23]])
print(np.sort(arr1,axis=0))
[[ 10 2 23]
[ 32 3 532]]
In [449]:
arr1 = np.array([[10,2,532],[32,3,23]])
print(np.sort(arr1))
[[ 2 10 532]
[ 3 23 32]]
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 13/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
Purpose of Learning NumPy
In [123]:
import pandas as pd
In [124]:
sno = np.random.randint(low = 1, high =1000, size = [10000,])
names = np.random.choice(['Sreenivas','Ram','Kranthi','Mani','Sam'],10000)
gender = np.random.choice(['Male','Female'],10000)
In [125]:
df = pd.DataFrame({"Serial Number":sno," Name ":names,"Gender":gender})
df
Out[125]:
Serial Number Name Gender
0 946 Kranthi Female
1 773 Ram Female
2 438 Kranthi Female
3 485 Mani Female
4 292 Mani Male
... ... ... ...
9995 346 Mani Male
9996 368 Kranthi Male
9997 506 Mani Female
9998 132 Kranthi Female
9999 628 Ram Female
10000 rows × 3 columns
In [461]:
df.shape
Out[461]:
(10000, 3)
In [126]:
import matplotlib.pyplot as plt
In [127]:
x = np.arange(10)
y = np.arange(10,20)
z = np.arange(20,30)
print(x)
print(y)
print(z)
[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 25 26 27 28 29]
In [128]:
plt.scatter(y,x,c='g',marker='*',s=100)
plt.scatter(z,x,c='r',s=100)
plt.show()
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 14/15
29/03/2023, 20:40 NumPy - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Python Bootcamp/NumPy.ipynb# 15/15