Nmpy - 1 - Data Science
Nmpy - 1 - Data Science
...................................................................................................................................................................................................
Srikanta Gouda
Not avalable but we can create from external libraries one is by usion "Array module" and
another is using "Numpy module or library ."
for x in a:
print(x)
import numpy as np
a= np.array([10,20,30,40])
print(a)
print(type(a))
[10 20 30 40]
<class 'numpy.ndarray'>
Element one by one
10
20
30
40
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 1/29
5/28/24, 12:38 AM Nmpy_1
[10 20 30]
1
Out[16]:
[[10 20 30 40]
[10 20 30 40]
[10 20 30 40]]
import numpy as np
a= np.array(('Siku','Biku','Chiku'))
print(type(a))
print(a)
print(a.ndim)
print(a.shape)
<class 'numpy.ndarray'>
['Siku' 'Biku' 'Chiku']
1
(3,)
import numpy as np
a= np.array([10,'Siku',10.5,True,10+20j],dtype=object)
a
# type(a)
In [5]: # arrange()
import numpy as np
a = np.arange(0,10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Out[5]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Out[6]:
In [ ]: import numpy as np
a = np.arange(1,11,2)
a
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 2/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(10)
print(""a.ndim)
#print(f'a={a}')
#print(f'The dimension of the array is: ,{a.ndim}' )
In [16]: # linspace():
import numpy as np
a= np.linspace(0,1)
print(a)
print(a.shape)
print(a.ndim)
In [14]: # zeros()
import numpy as np
a= np.zeros((3,3,4)) #by default type is "float type"
print(a)
[[[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.]]]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 3/29
5/28/24, 12:38 AM Nmpy_1
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
import numpy as np
a= np.zeros((2,3,4))
print(a)
print(a.size)
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
24
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Out[17]:
array([[[1, 1, 1, 1],
Out[3]:
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 4/29
5/28/24, 12:38 AM Nmpy_1
Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
fill_value : scalar or array_like
Fill value.
dtype : data-type, optional
The desired data-type for the array The default, None, means
``np.array(fill_value).dtype``.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory.
like : array_like, optional
Reference object to allow the creation of arrays which are not
NumPy arrays. If an array-like passed in as ``like`` supports
the ``__array_function__`` protocol, the result will be defined
by it. In this case, it ensures the creation of an array object
compatible with that passed in via this argument.
.. versionadded:: 1.20.0
Returns
-------
out : ndarray
Array of `fill_value` with the given shape, dtype, and order.
See Also
--------
full_like : Return a new array with shape of input filled with value.
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
zeros : Return a new array setting values to zero.
Examples
--------
>>> np.full((2, 2), np.inf)
array([[inf, inf],
[inf, inf]])
>>> np.full((2, 2), 10)
array([[10, 10],
[10, 10]])
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 5/29
5/28/24, 12:38 AM Nmpy_1
Function **
In [14]: def f1(a,*,b):
print(f'The value of a: {a}')
print(f'The value of b: {b}')
#f1(10,20)
#f1(a=10,b=20)
f1(10,b=20)
#f1(10,b=20)
The value of a: 10
The value of b: 20
13
Out[5]:
import numpy as np
np.random.randint(1,9,size=10)
array([5, 2, 7, 5, 6, 2, 5, 2, 3, 7])
Out[8]:
array([0, 6, 7, 6, 5, 7, 3, 4, 2])
Out[9]:
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 6/29
5/28/24, 12:38 AM Nmpy_1
array([[[50, 70, 70, 93],
Out[14]:
[88, 70, 73, 82],
[53, 61, 52, 35]],
dtype('int32')
Out[10]:
import numpy as np
import sys
a= np.random.randint(1,100, size=(2,3))
print(f'size of the array: {sys.getsizeof(a)}')
b= np.random.randint(1,100, size=(2,3),dtype= 'int8')
print(f"The size of the int-8 size: {sys.getsizeof(b)}")
c= np.random.randint(1,100, size=(2,3),dtype= 'int16')
print(f"The size of the int-8 size: {sys.getsizeof(c)}")
d= np.random.randint(1,100, size=(2,3),dtype= 'int64')
print(f"The size of the int-8 size: {sys.getsizeof(d)}")
In [25]: # rand()
# generate rando value using rand function
import numpy as np
a= np.random.rand()
a
0.27904189827849124
Out[25]:
import numpy as np
a= np.random.rand(10)
a
import numpy as np
a= np.random.rand(3,3)
a
import numpy as np
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 7/29
5/28/24, 12:38 AM Nmpy_1
a= np.random.rand(2,3,3)
a
In [38]: # unform()
# create random array with custmize value
import numpy as np
np.random.uniform()
# a= np.random.uniform()
#a
0.6641641512998665
Out[38]:
import numpy as np
a= np.random.uniform(10,20,size=(3))
a
import numpy as np
a= np.random.uniform(1,100,size=(3,3))
a
import numpy as np
a= np.random.uniform(1,100,size=(3,3,4))
a
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 8/29
5/28/24, 12:38 AM Nmpy_1
import matplotlib.pyplot as plt
count, bins, ignored = plt.hist(s, 15, density=True)
plt.plot(bins,np.ones_like(bins), linewidth=2, color='r')
plt.show()
In [26]: # Suffle()
# 1-D Array
import numpy as np
a= np.arange(9)
a
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 9/29
5/28/24, 12:38 AM Nmpy_1
np.random.shuffle(a)
print(a)
[3 0 4 5 2 6 1 7 8]
import numpy as np
a= np.random.randint(1,100,size=(6,5))
print("Before Shuffle")
print(a)
np.random.shuffle(a)
print("After Shuffle")
print(a)
Before Shuffle
[[ 2 39 25 28 99]
[79 84 14 60 36]
[35 98 46 45 16]
[15 59 8 40 84]
[76 47 21 20 14]
[24 15 91 46 53]]
After Shuffle
[[ 2 39 25 28 99]
[24 15 91 46 53]
[79 84 14 60 36]
[35 98 46 45 16]
[76 47 21 20 14]
[15 59 8 40 84]]
import numpy as np
a= np.arange(48).reshape(4,3,4)
np.random.shuffle(a)
print(a)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[36 37 38 39]
[40 41 42 43]
[44 45 46 47]]
[[24 25 26 27]
[28 29 30 31]
[32 33 34 35]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
In [ ]: array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 10/29
5/28/24, 12:38 AM Nmpy_1
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 11/29
5/28/24, 12:38 AM Nmpy_1
[[10 20 30]
[30 20 40]
[50 60 70]]
The Dimention of Given Aray is: 2
The Size of Given Aray is: 9
The Shape of Given Aray is: (3, 3)
The Type of Given Aray is: int32
The ItemSize of Given Aray is: 4
import numpy as np
a= np.array([[10,20,30],[40,50,60]])
print(a)
print()
print(a[0][2]) #accesing 30
print(a[1][1]) #accessing 50
[[10 20 30]
[40 50 60]]
30
50
import numpy as np
a=[[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]]]
n= np.array(a)
print(n)
print(f"The shape of the array is: {n.shape}")
print(n[0][0][2])
print(n[1][2][1])
print(n[-1][-3][-3])
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
The shape of the array is: (2, 3, 3)
3
17
10
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 12/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(1,37).reshape(2,2,3,3)
a
array([[[[ 1, 2, 3],
Out[54]:
[ 4, 5, 6],
[ 7, 8, 9]],
In [62]: # 2
import numpy as np
a= np.arange(1,121).reshape(2,3,4,5)
print(a)
print("\nThe Request value [1][1][1][2] is: ",a[1][1][1][2])
print("The Position of 88: ",np.where(a==88))
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 13/29
5/28/24, 12:38 AM Nmpy_1
[[[[ 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 30]
[ 31 32 33 34 35]
[ 36 37 38 39 40]]
[[ 41 42 43 44 45]
[ 46 47 48 49 50]
[ 51 52 53 54 55]
[ 56 57 58 59 60]]]
[[[ 61 62 63 64 65]
[ 66 67 68 69 70]
[ 71 72 73 74 75]
[ 76 77 78 79 80]]
[[ 81 82 83 84 85]
[ 86 87 88 89 90]
[ 91 92 93 94 95]
[ 96 97 98 99 100]]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[]
[]
print(result) # Output: []
[]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 14/29
5/28/24, 12:38 AM Nmpy_1
[[10 20]
[30 40]
[50 60]]
40
Sysntax:
a[row,column]
Row Column
a[begin:end:step , begin:end:step]
In [37]: # applying Slicing
a= np.array([[10,20],[30,40],[50,60],[70,80],[90,100]])
print(a)
print('\n The value is: \n',a[0::2, : ])
[[ 10 20]
[ 30 40]
[ 50 60]
[ 70 80]
[ 90 100]]
import numpy as np
a= np.array([[10,20],[30,40],[50,60],[70,80],[90,100]])
print(a)
print("\nThe slice of the given 2-Array is \n", a[0:2,1:2])
print('\n The value is: \n',a[:2,1:])
[[ 10 20]
[ 30 40]
[ 50 60]
[ 70 80]
[ 90 100]]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 15/29
5/28/24, 12:38 AM Nmpy_1
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
import numpy as np
a= np.arange(1,25).reshape(2,3,4)
print(a)
array([[[ 1, 2, 3, 4],
Out[4]:
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]],
import numpy as np
a= np.arange(1,25).reshape(2,3,4)
print(a)
print('\nEx-1 The required a[:,:,0:1] Slice is: \n',a[:,:,0:1])
print('\nEx-2 The Required a[:,:1,:] Slice is: \n', a[:,:1,:])
print('\nEx-3 The Required a[:,0::2,:] Slice is: \n', a[:,0::2,:])
print('\nEx-4 The Required a[:,0:2,1:3] Slice is: \n', a[:,0:2,1:3])
print('\nEx-4 The Required a[:,0::2,::3] Slice is: \n', a[:,0::2,::3])
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 16/29
5/28/24, 12:38 AM Nmpy_1
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
[[13]
[17]
[21]]]
[[13 14 15 16]]]
[[13 14 15 16]
[21 22 23 24]]]
[[14 15]
[18 19]]]
[[13 16]
[21 24]]]
Advanced Indexing
1st way
1-D Array
import numpy as np
a= np.arange(1,100,10)
print('The array is: \n',a)
indices= np.array([2,4,5,8])
print('\nThe elements which we are passed the argument is: \n', a[indices])
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 17/29
5/28/24, 12:38 AM Nmpy_1
2nd Way
In [19]: import numpy as np
a= np.arange(10,101,10)
print(a)
l=[2,4,5,8]
m=[3,4,9,1] # indeces with no oder
print('To access the arbitary elements present in the array: \n',a[l])
print('To access the arbitary elements present in the array: \n',a[m])
[ 10 20 30 40 50 60 70 80 90 100]
To access the arbitary elements present in the array:
[30 50 60 90]
To access the arbitary elements present in the array:
[ 40 50 100 20]
2-D Array
import numpy as np
a= np.arange(1,17).reshape(4,4)
print(a)
print('\nThe output..')
print('Ex-1 The Required Arbitary Elements: ',a[[0,1,2,3],[0,1,2,3]])
print('Ex-2 The Required Arbitary Elementes: ',a[[0,1,2,3],[1,3,0,2]])
print('Ex-3 The Required Arbitary Elementes: ',a[[0,1,2,3,3,3,3],[0,0,0,0,1,2,3]])
print('Ex-4 The Required Arbitary Elementes: ',a[[0,0,0,0,1,2,3],[0,1,2,3,3,3,3]])
print('Ex-5 The Required Arbitary Elementes: ',a[[0,0,1,1,2,2,3,3],[0,1,2,3,0,1,1,2
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
The output..
Ex-1 The Required Arbitary Elements: [ 1 6 11 16]
Ex-2 The Required Arbitary Elementes: [ 2 8 9 15]
Ex-3 The Required Arbitary Elementes: [ 1 5 9 13 14 15 16]
Ex-4 The Required Arbitary Elementes: [ 1 2 3 4 8 12 16]
Ex-5 The Required Arbitary Elementes: [ 1 2 7 8 9 10 14 15]
3-D Array
import numpy as np
a= np.arange(1,33).reshape(2,4,4)
print(a)
print('Ex-1 The Selected Arbitary Elementes: ',a[[0,1],[1,0],[2,1]])
print('Ex-2 The Selected Arbitary Elementes: ',a[[0,1],[0,2],[2,3]])
print('Ex-3 The Selected Arbitary Elmentes: ',a[[0,1],[1,1],[1,3]])
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 18/29
5/28/24, 12:38 AM Nmpy_1
[[[ 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 30 31 32]]]
Ex-1 The Selected Arbitary Elementes: [ 7 18]
Ex-2 The Selected Arbitary Elementes: [ 3 28]
Ex-3 The Selected Arbitary Elmentes: [ 6 24]
[10 20 30 40]
[10 40]
[10 20 30 40 50 60]
[False False True True True True]
[30 40 50 60]
[10 20 30 40 50 60 75 13]
[30 40 50 60 75]
[10 20 30 40 50 60]
[75 13]
import numpy as np
a= np.arange(1,37).reshape(6,6)
print(a)
print(a[a%2==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 30]
[31 32 33 34 35 36]]
[ 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 19/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(10,101,10)
print(a)
# Applying Slicing
b=a[0:4]
print(b)
b[2]=100
print(a)
print(a)
[ 10 20 30 40 50 60 70 80 90 100]
[10 20 30 40]
[ 10 20 100 40 50 60 70 80 90 100]
[ 10 20 100 40 50 60 70 80 90 100]
import numpy as np
a= np.arange(10,101,10)
print('a=',a)
b= a[[0,2,3]]
print('b=',b)
# if we want to change in a this will not reflacted to b
a[1]=300
print('After adding the value to a')
print('a=',a)
print('Same time chec the b')
print(b) # The output of b is not reflacted the changed value
a= [ 10 20 30 40 50 60 70 80 90 100]
b= [10 30 40]
After adding the value to a
a= [ 10 300 30 40 50 60 70 80 90 100]
Same time chec the b
[10 30 40]
import numpy as np
a= np.arange(10,101,10)
print(a)
for x in a:
print(x)
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 20/29
5/28/24, 12:38 AM Nmpy_1
[ 10 20 30 40 50 60 70 80 90 100]
10
20
30
40
50
60
70
80
90
100
import numpy as np
a= np.array([[10,20,30],[40,50,60],[70,80,90]])
#a= np.arange(10,91,10).reshape(3,3)
print(a)
print('Iterate Each Elements')
for x in a:
for y in x:
print(y)
[[10 20 30]
[40 50 60]
[70 80 90]]
Iterate Each Elements
10
20
30
40
50
60
70
80
90
import numpy as np
a= np.array([[[10,20],[30,40]],[[50,60],[70,80]]])
#a= np.arange(10,81,10).reshape(2,2,2)
print(a)
for x in a:
for y in x:
for z in y:
print(z)
[[[10 20]
[30 40]]
[[50 60]
[70 80]]]
10
20
30
40
50
60
70
80
Applying nditer()
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 21/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(10,101,10)
print(a)
for x in np.nditer(a):
print(x)
[ 10 20 30 40 50 60 70 80 90 100]
10
20
30
40
50
60
70
80
90
100
import numpy as np
a= np.arange(10,91,10).reshape(3,3)
print(a)
for x in np.nditer(a):
print(x)
[[10 20 30]
[40 50 60]
[70 80 90]]
10
20
30
40
50
60
70
80
90
import numpy as np
a= np.arange(10,181,10).reshape(2,3,3)
print(a)
for x in np.nditer(a):
print(x)
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 22/29
5/28/24, 12:38 AM Nmpy_1
[[[ 10 20 30]
[ 40 50 60]
[ 70 80 90]]
import numpy as np
a=np.arange(1,17).reshape(2,2,2,2)
print(a)
for x in np.nditer(a):
print(x)
[[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]]
[[[ 9 10]
[11 12]]
[[13 14]
[15 16]]]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[[10 20 30]
[40 50 60]
[70 80 90]]
10
20
40
50
70
80
10.0
20.0
30.0
40.0
50.0
60.0
70.0
80.0
90.0
[[10 20 30]
[40 50 60]
[70 80 90]]
import numpy as np
a= np.arange(1,10).reshape(3,3)
print(a)
for x in np.nditer(a):
print(x.dtype)
[[1 2 3]
[4 5 6]
[7 8 9]]
int32
int32
int32
int32
int32
int32
int32
int32
int32
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 24/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(1,11)
print(a)
[ 1 2 3 4 5 6 7 8 9 10]
1 element present at the position(0,)
2 element present at the position(1,)
3 element present at the position(2,)
4 element present at the position(3,)
5 element present at the position(4,)
6 element present at the position(5,)
7 element present at the position(6,)
8 element present at the position(7,)
9 element present at the position(8,)
10 element present at the position(9,)
import numpy as np
a= np.arange(1,11)
print(a)
[ 1 2 3 4 5 6 7 8 9 10]
2 element present at the position(0,)
4 element present at the position(1,)
6 element present at the position(2,)
8 element present at the position(3,)
10 element present at the position(4,)
12 element present at the position(5,)
14 element present at the position(6,)
16 element present at the position(7,)
18 element present at the position(8,)
20 element present at the position(9,)
[ 1 2 3 4 5 6 7 8 9 10]
import numpy as np
a= np.arange(10,91,10).reshape(3,3)
for pos,element in np.ndenumerate(a):
print(f"{element} Element present at the posisition {pos}")
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 25/29
5/28/24, 12:38 AM Nmpy_1
import numpy as np
a= np.arange(10,181,10).reshape(2,3,3)
for pos,element in np.ndenumerate(a):
print(f"{element} element present at position {pos}")
Arithmetic Operator
Array with Scalar value
In [6]: # 1-D Array
import numpy as np
a= np.arange(1,11)
print(a)
print('\n')
print(f"a+2 value is: {a+2}")
print(f"a-2 value is: {a-2}")
print(f"a*2 value is: {a*2}")
print(f"a/2 value is: {a/2}")
print(f"a//2 value is: {a//2}")
print(f"a%2 value is: {a%2}")
[ 1 2 3 4 5 6 7 8 9 10]
import numpy as np
a= np.arange(10,91,10).reshape(3,3)
print(a)
print('\n')
print(f"a+2 Value is:\n {a+2}")
print(f"a-2 value is:\n {a-2}")
print(f"a*2 value is:\n {a*2}")
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 26/29
5/28/24, 12:38 AM Nmpy_1
print(f"a/2 value is:\n {a/2}")
print(f"a//2 value is:\n {a//2}")
print(f"a%2 value is:\n {a%2}")
[[10 20 30]
[40 50 60]
[70 80 90]]
1. Same dimention
2. Same size
3. Same shape
import numpy as np
a= np.array([10,30,40,50])
b= np.array([60,70,80,90])
print('A= ',a)
print('B= ',b)
print('\n')
print(f"Dimention of A: {a.ndim}, Size of A: {a.shape}, Shape of A: {a.shape}")
print(f"Dimention of A: {b.ndim}, Size of A: {b.shape}, Shape of A: {b.shape}")
print(f"A+B value is: {a+b}")
print(f"A-B value is: {a-b}")
print(f"A*B value is: {a*b}")
print(f"A/B value is: {a/b}")
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 27/29
5/28/24, 12:38 AM Nmpy_1
A= [10 30 40 50]
B= [60 70 80 90]
a= np.arange(1,10,1).reshape(3,3)
b= np.arange(10,91,10).reshape(3,3)
print('A= \n',a)
print('B= \n',b)
print(f"A+b value is: \n{a+b}")
print(f"A-b value is: \n{a-b}")
print(f"A*B value is: \n{a*b}")
A=
[[1 2 3]
[4 5 6]
[7 8 9]]
B=
[[10 20 30]
[40 50 60]
[70 80 90]]
A+b value is:
[[11 22 33]
[44 55 66]
[77 88 99]]
A-b value is:
[[ -9 -18 -27]
[-36 -45 -54]
[-63 -72 -81]]
A*B value is:
[[ 10 40 90]
[160 250 360]
[490 640 810]]
import numpy as np
a= np.array([10,30,40,50])
b= np.array([60,70,80,90])
print('A= ',a)
print('B= ',b)
print(F"Demention of A: {a.ndim}, Size of A is: {a.shape}, Shape of A: {a.shape}")
print(F"Demention of B: {b.ndim}, Size of B is: {b.shape}, Shape of B: {b.shape}")
print(F'A+B value is: {np.add(a,b)}')
print(F'A-B value is: {np.subtract(a,b)}')
print(F'A*B value is: {np.multiply(a,b)}')
print(F'A/B value is: {np.divide(a,b)}')
print(F'A%B value is: {np.mod(a,b)}')
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 28/29
5/28/24, 12:38 AM Nmpy_1
A= [10 30 40 50]
B= [60 70 80 90]
Demention of A: 1, Size of A is: (4,), Shape of A: (4,)
Demention of B: 1, Size of B is: (4,), Shape of B: (4,)
A+B value is: [ 70 100 120 140]
A-B value is: [-50 -40 -40 -40]
A*B value is: [ 600 2100 3200 4500]
A/B value is: [0.16666667 0.42857143 0.5 0.55555556]
A%B value is: [10 30 40 50]
localhost:8888/nbconvert/html/Nmpy_1.ipynb?download=false 29/29