KEMBAR78
NumpyOperations - Ipynb - Colab | PDF | Linear Algebra | Algebra
0% found this document useful (0 votes)
9 views11 pages

NumpyOperations - Ipynb - Colab

Uploaded by

omkarmeher8689
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)
9 views11 pages

NumpyOperations - Ipynb - Colab

Uploaded by

omkarmeher8689
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/ 11

NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

import numpy as np

Now apply various operations on arrays

## Arithmetic operators
a=np.arange(4)
a

array([0, 1, 2, 3])

a+4

array([4, 5, 6, 7])

a*2

array([0, 2, 4, 6])

a+4*2

array([ 8, 9, 10, 11])

For operations with 2 arrays, operators are applied only between corresponding elements
(element-wise).

b=np.arange(4,8)
b

array([4, 5, 6, 7])

print(a)
print(b)
print(a+b)

[0 1 2 3]
[4 5 6 7]
[ 4 6 8 10]

c=np.arange(5,10)
c

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

print(a+c) ## warning: Size of the arrays should be same.

1 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-4222838292.py in <cell line: 0>()
----> 1 print(a+c)

ValueError: operands could not be broadcast together with shapes (4,) (5,)

print(a)
print(b)
print(a-b)

[0 1 2 3]
[4 5 6 7]
[-4 -4 -4 -4]

print(a)
print(b)
print(b-a)

[0 1 2 3]
[4 5 6 7]
[4 4 4 4]

print(a)
print(b)
print(a*b)

[0 1 2 3]
[4 5 6 7]
[ 0 5 12 21]

c=np.arange(0,3)
c

array([0, 1, 2])

print(a)
print(c)
print(a*c)

[0 1 2 3]
[0 1 2]
---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-2251260352.py in <cell line: 0>()
1 print(a)
2 print(c)
----> 3 print(a*c)

ValueError: operands could not be broadcast together with shapes (4,) (3,)

2 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

a * np.sin(b)

array([-0. , -0.95892427, -0.558831 , 1.9709598 ])

a * np.sqrt(b)

array([0. , 2.23606798, 4.89897949, 7.93725393])

A = np.arange(0, 9).reshape(3, 3)
A

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

B = np.ones((3, 3))
B

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


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

A*B

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


[3., 4., 5.],
[6., 7., 8.]])

## Matrix Product
## in numpy, matrix product is used with dot function
np.dot(A,B)

array([[ 3., 3., 3.],


[12., 12., 12.],
[21., 21., 21.]])

A.dot(B)## alternative way for matrix product

array([[ 3., 3., 3.],


[12., 12., 12.],
[21., 21., 21.]])

B.dot(A)

array([[ 9., 12., 15.],


[ 9., 12., 15.],
[ 9., 12., 15.]])

Increment and decrement operators ++ or -- operators are not used in python

for increment: +=

3 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

for increment: +=

for decrement: -=

j = np.arange(4)
j

array([0, 1, 2, 3])

j+=1

array([1, 2, 3, 4])

j+=1
j

array([2, 3, 4, 5])

j-=2

array([0, 1, 2, 3])

j*=4
j

array([ 0, 4, 8, 12])

Universal Function : acts individually on each single element of the input array to generate
a corresponding result in a new output array. In the end, you obtain an array of the same
size as the input. FOR EX:

a = np.arange(1, 5)

array([1, 2, 3, 4])

np.sqrt(a)

array([1. , 1.41421356, 1.73205081, 2. ])

np.log(a)

4 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

array([0. , 0.69314718, 1.09861229, 1.38629436])

np.sin(a)

array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])

np.cos(a)

array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362])

np.tan(a)

array([ 1.55740772, -2.18503986, -0.14254654, 1.15782128])

Aggregate functions sum, mean, max, min, std

a = np.array([3.3, 4.5, 1.2, 5.7, 0.3])

array([3.3, 4.5, 1.2, 5.7, 0.3])

a.min()

np.float64(0.3)

a.max()

np.float64(5.7)

a.mean()

np.float64(3.0)

a.std()

np.float64(2.0079840636817816)

a.sum()

np.float64(15.0)

Indexing, Slicing, Iterating Used to select elements through indexes and slices, in order to
obtain the values contained in them or to make assignments in order to change their
values. Finally, you can make iterations within them.

5 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

## Indexing
## always uses square brackets ([ ]) to index the elements of the array
## such as extracting a value, selecting items, or even assigning a new value.
a = np.arange(10, 16)
a
print(a)
print(a[4])
print(a.size)

## NumPy arrays also accept negative indexes.


print(a[-1])
print(a[-4])

[10 11 12 13 14 15]
14
6
15
12

## To select multiple items at once, you can pass an array of indexes in square bracke
a[[1, 3, 4]]

array([11, 13, 14])

a[1]

np.int64(11)

a[1,2]

---------------------------------------------------------------------------
IndexError Traceback (most recent call
last)
/tmp/ipython-input-2691606579.py in <cell line: 0>()
----> 1 a[1,2]

IndexError: too many indices for array: array is 1-dimensional, but 2 were
indexed

a[[1,2]]

array([11, 12])

Indexing for 2D matrices

A = np.arange(10, 19).reshape((3, 3))


A

array([[10, 11, 12],


[13, 14, 15],
[16, 17, 18]])

6 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

[16, 17, 18]])

A[1, 2]

np.int64(15)

Slicing

• allows you to extract portions of an array to generate new arrays. Use a sequence of
numbers separated by : within[]

import numpy as np

a=np.arange(10,16)
a

array([10, 11, 12, 13, 14, 15])

##slicing operation
a[1:5]

array([11, 12, 13, 14])

c=a[2:4]
c

array([12, 13])

a[1:5:2] ##3rd defines the gap in the sequence of the elements

array([11, 13])

a[0:4]

array([10, 11, 12, 13])

a[0:4:2]

array([10, 12])

a[0:4:3]

array([10, 13])

##if omit 1st no., it implicitly interprets as 0


a[:5:2]

array([10, 12, 14])

7 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

#if omit 2nd no, it implicitly interprets max index


a[1::2]

array([11, 13, 15])

##if omit 3rd no, it will be interpreted as 1


a[1:4:]

array([11, 12, 13])

a[::2]##starts from 0th index to max index

array([10, 12, 14])

a[:4:2]

array([10, 12])

a[:5:]

array([10, 11, 12, 13, 14])

2D slicing: it separately de�ned for rows and columns

B=np.arange(10,19).reshape(3,3)
B

array([[10, 11, 12],


[13, 14, 15],
[16, 17, 18]])

##extract only 1st row


B[0,:]## select all columns

array([10, 11, 12])

B[:,0] ## select only first column

array([10, 13, 16])

##if you want to extract smaller matrix from continuous rows and columns
B[:2,:2]

array([[10, 11],
[13, 14]])

##extract non-continuous rows

8 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

B[[0,2],:2]

array([[10, 11],
[16, 17]])

array([[10, 11, 12],


[13, 14, 15],
[16, 17, 18]])

Iterate the array

for(i=0;i<5;i++) prin�(i)

a=np.arange(10,18)
a

array([10, 11, 12, 13, 14, 15, 16, 17])

## iterate the elements of an array


for i in a:
print(i)

10
11
12
13
14
15

Iterating 2D Array

A=np.arange(10,19).reshape(3,3)
A

array([[10, 11, 12],


[13, 14, 15],
[16, 17, 18]])

for row in A:
print(row)

[10 11 12]
[13 14 15]
[16 17 18]

##iterate element by element


for item in A.flat:
print(item)

9 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

print(item)

10
11
12
13
14
15
16
17
18

instead of applying iteration on rows/cols/elements, use apply_along_axis(aggregate


function, axis on which iterations applied, array)

axis=0, iteration evaluates column by column

axis=1, iteration evaluates row by row

for(i=0;i<3;i++) { for(j=0;j<3;j++) {prin�(

array([[10, 11, 12],


[13, 14, 15],
[16, 17, 18]])

np.apply_along_axis(np.mean,axis=0,arr=A)

array([13., 14., 15.])

np.apply_along_axis(np.mean,axis=1,arr=A)

array([11., 14., 17.])

np.apply_along_axis(np.max,axis=1,arr=A)

array([12, 15, 18])

np.apply_along_axis(np.max,axis=0,arr=A)

array([16, 17, 18])

np.apply_along_axis(np.sum,axis=1,arr=A)

array([33, 42, 51])

10 of 11 03/09/25, 11:12
NumpyOperations.ipynb - Colab https://colab.research.google.com/drive/119LcTkWF...

11 of 11 03/09/25, 11:12

You might also like