0% found this document useful (0 votes)
5 views9 pages

Numpy Array Operations Guide

The document is a Jupyter Notebook containing various examples of using the NumPy library for creating and manipulating arrays in Python. It covers topics such as creating 0D, 1D, 2D, and 3D arrays, array operations, reshaping, sorting, and performing arithmetic operations. The notebook includes code snippets demonstrating the use of functions like array(), zeros(), full(), eye(), arange(), linspace(), and various array manipulations.

Uploaded by

gannudegavath
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)
5 views9 pages

Numpy Array Operations Guide

The document is a Jupyter Notebook containing various examples of using the NumPy library for creating and manipulating arrays in Python. It covers topics such as creating 0D, 1D, 2D, and 3D arrays, array operations, reshaping, sorting, and performing arithmetic operations. The notebook includes code snippets demonstrating the use of functions like array(), zeros(), full(), eye(), arange(), linspace(), and various array manipulations.

Uploaded by

gannudegavath
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

9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [1]: import numpy as np #creating arrays(using array() function) 0D,1D,2D,3D arrays

In [2]: a=[Link](25) #creating arrays(using

In [3]: print(a)

25

In [4]: a=[Link]([34,89,12]) # 1D array(collection of 0D is 1D) list:CSV array:space

In [5]: print(a)

[34 89 12]

In [6]: a=[Link]([[34,28,10],[45,89,23]])

In [7]: print(a)

[[34 28 10]
[45 89 23]]

In [8]: a[0][2]

Out[8]: 10

In [9]: a[1][1]

Out[9]: 89

In [10]: a=[Link]([[[23,45,12],[34,18,29]],[[45,89,23],[35,89,10]]])

In [11]: print(a)

[[[23 45 12]
[34 18 29]]

[[45 89 23]
[35 89 10]]]

In [12]: a[0][0][1]

Out[12]: 45

In [13]: a[1][1][2]

Out[13]: 10

In [14]: a[0][0][1]

Out[14]: 45

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 1/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [15]: a[0][1][1]

Out[15]: 18

In [16]: b=[Link](5)

In [17]: print(b) #floats

[0. 0. 0. 0. 0.]

In [18]: b=[Link](5,2)

In [19]: print(b)

[2 2 2 2 2]

In [20]: b=[Link](4)

In [21]: print(b)

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

In [22]: b=[Link](5)

In [23]: print(b)

[1. 1. 1. 1. 1.]

In [24]: b=[Link]([2,3])

In [25]: print(b)

[[0. 0. 0.]
[0. 0. 0.]]

In [26]: b=[Link]([2,3],4)

In [27]: print(b)

[[4 4 4]
[4 4 4]]

In [28]: b=[Link]([2,3,3],5) #[height][rows][col]

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 2/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [29]: print(b)

[[[5 5 5]
[5 5 5]
[5 5 5]]

[[5 5 5]
[5 5 5]
[5 5 5]]]

In [30]: b=[Link]([2,4])

In [31]: print(b)

[[1. 1. 1. 1.]
[1. 1. 1. 1.]]

In [32]: b=[Link](4) #fills diagonals with 1 .only one parameter required

In [33]: print(b)

[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

In [34]: a=[Link](0,10,2) # initialize with a numerical ranges arrange(),linspace(),


In [35]: print(a)

[0 2 4 6 8]

In [36]: a=[Link](0,100,20,dtype=float) #start:stop:step

In [37]: print(a)

[ 0. 20. 40. 60. 80.]

In [38]: a=[Link](10,100,9) #start:stop:how many numbers b/w 10 to 100 , stop is

In [39]: print(a)

[ 10. 21.25 32.5 43.75 55. 66.25 77.5 88.75 100. ]

In [40]: a=[Link](10,100,9,base=2) #defalut base is 10

In [41]: print(a)

[1.02400000e+03 2.49394808e+06 6.07400100e+09 1.47932062e+13


3.60287970e+16 8.77479972e+19 2.13709911e+23 5.20489671e+26
1.26765060e+30]

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 3/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [42]: a=[Link](10,100,9)

In [43]: print(a)

[1.00000000e+010 1.77827941e+021 3.16227766e+032 5.62341325e+043


1.00000000e+055 1.77827941e+066 3.16227766e+077 5.62341325e+088
1.00000000e+100]

In [44]: a=[Link]([10,89,7,23,19])

In [45]: [Link](a)

Out[45]: 5

In [46]: [Link](a)

Out[46]: (5,)

In [47]: a=[Link]([[34,90,89],[34,78,2]])

In [48]: [Link](a)

Out[48]: 6

In [49]: [Link](a)

Out[49]: (2, 3)

In [50]: [Link]

Out[50]: dtype('int32')

array operations

In [51]: a=[Link]([[67,90,12],[45,78,65]])

In [52]: b=[Link](a)

In [53]: print(b)

[[67 90 12]
[45 78 65]]

In [54]: c=[Link]() # view() function works similar to copy but it generates a referenc

In [55]: print(c)

[[67 90 12]
[45 78 65]]

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 4/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [56]: a[0][1]=67 #it is not reflected in b as b is just a copy of a

In [57]: print(a)

[[67 67 12]
[45 78 65]]

In [58]: print(b)

[[67 90 12]
[45 78 65]]

In [59]: b[0][1]=177 #change is refelected in c as it takes voew of b

In [60]: print(c)

[[ 67 177 12]
[ 45 78 65]]

In [61]: a=[Link]([23,9,17,98,15])

In [62]: [Link](a)

Out[62]: array([ 9, 15, 17, 23, 98])

In [63]: a=[Link]([[45,89,24,10],[89,23,19,90]])

In [64]: a

Out[64]: array([[45, 89, 24, 10],


[89, 23, 19, 90]])

In [65]: [Link](a,axis=0) #columns are sorted

Out[65]: array([[45, 23, 19, 10],


[89, 89, 24, 90]])

In [66]: [Link](a) # deafult sorted row wise

Out[66]: array([[10, 24, 45, 89],


[19, 23, 89, 90]])

In [67]: [Link](a,axis=1) # sored row wise

Out[67]: array([[10, 24, 45, 89],


[19, 23, 89, 90]])

In [68]: #reshape() function reshapes the existing dimensions of the data

In [69]: ar1=[Link](10,250,10)

In [70]: ar1

Out[70]: array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130,
140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240])

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 5/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [71]: [Link](2,12)

Out[71]: array([[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
[130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]])

In [72]: [Link](3,8)

Out[72]: array([[ 10, 20, 30, 40, 50, 60, 70, 80],
[ 90, 100, 110, 120, 130, 140, 150, 160],
[170, 180, 190, 200, 210, 220, 230, 240]])

In [73]: a=[Link]([12,18,10])

In [74]: b=[Link]([78,90,17])

In [75]: [Link](b,a)

Out[75]: array([78, 90, 17, 12, 18, 10])

In [76]: [Link](a,b)

Out[76]: array([12, 18, 10, 78, 90, 17])

In [77]: a=[Link]([23,19,10])

In [78]: [Link](a,1,90)

Out[78]: array([23, 90, 19, 10])

In [79]: [Link](a,1,[56,89]) # to insert multiple elemnets

Out[79]: array([23, 56, 89, 19, 10])

In [80]: a=[Link]([23,89,12])

In [81]: [Link](a,0)

Out[81]: array([89, 12])

In [82]: [Link](a,0)

Out[82]: array([89, 12])

In [83]: a=[Link]([[34,89,12],[34,89,12]])

In [84]: b=[Link]([[23,89,10],[12,9,4]])

In [85]: [Link]((a,b)) #by defalue columns are merged

Out[85]: array([[34, 89, 12],


[34, 89, 12],
[23, 89, 10],
[12, 9, 4]])

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 6/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [86]: [Link]((a,b),axis=1) # rows are merged

Out[86]: array([[34, 89, 12, 23, 89, 10],


[34, 89, 12, 12, 9, 4]])

In [87]: [Link]((a,b),axis=0) # colums are merged

Out[87]: array([[34, 89, 12],


[34, 89, 12],
[23, 89, 10],
[12, 9, 4]])

In [ ]: a=[Link]([[1,2,3],[4,5,6]])//arithemetic operations

In [ ]: b=[Link]([[3,4,5],[9,9,7]])

In [88]: c=[Link](a,b)

In [89]: print(c)

[[ 57 178 22]
[ 46 98 16]]

In [90]: c=[Link](b,a)

In [91]: print(c)

[[-11 0 -2]
[-22 -80 -8]]

In [92]: e=[Link](a,b)
print(e)

[[ 782 7921 120]


[ 408 801 48]]

In [95]: i=[Link]([[1,0,0],[0,1,0],[0,0,1]])
j=[Link]([[1,2,3],[4,5,6],[6,7,8]])
[Link](i,j)

Out[95]: array([[1, 2, 3],


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

In [96]: print([Link](i,j))

[[1. 0. 0. ]
[0. 0.2 0. ]
[0. 0. 0.125]]

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 7/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [97]: print([Link](i))//e power element

[[2.71828183 1. 1. ]
[1. 2.71828183 1. ]
[1. 1. 2.71828183]]

In [98]: s=[Link]([[25,9],[16,64]])
print([Link](s))

[[5. 3.]
[4. 8.]]

In [100]: #array comparision(array wise)


a=[Link]([[1,2,3],[4,5,6]])
b=[Link]([[1,2,3],[4,5,6]])
print(np.array_equal(a,b))

True

In [101]: a==b#element wise comparision

Out[101]: array([[ True, True, True],


[ True, True, True]])

In [102]: b[0][1]=23

In [103]: a==b#element wise comparision


Out[103]: array([[ True, False, True],


[ True, True, True]])

In [104]: a=[Link]([1,2,3])
[Link](a)

Out[104]: 1

In [111]: a=[Link]([[1,2,3],[4,5,6]])
[Link](a,axis=0)

Out[111]: array([1, 2, 3])

In [112]: [Link](a,axis=1)

Out[112]: array([1, 4])

In [113]: [Link](a,axis=0)

Out[113]: array([4, 5, 6])

In [114]: [Link](a,axis=1)

Out[114]: array([3, 6])

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 8/9
9/12/25, 12:03 PM Numpy(lab3) - Jupyter Notebook

In [115]: [Link](a,axis=0)

Out[115]: array([2.5, 3.5, 4.5])

In [116]: [Link](a,axis=1)

Out[116]: array([2., 5.])

In [117]: [Link](a,axis=0)

Out[117]: array([2.5, 3.5, 4.5])

In [118]: [Link](a,axis=1)

Out[118]: array([2., 5.])

In [119]: [Link](a,axis=0)

Out[119]: array([2.25, 2.25, 2.25])

In [120]: [Link](a,axis=1)

Out[120]: array([0.66666667, 0.66666667])

In [122]: [Link](a,axis=0)

Out[122]: array([1.5, 1.5, 1.5])

In [123]: [Link](a,axis=1)

Out[123]: array([0.81649658, 0.81649658])

In [ ]: ​

localhost:8888/notebooks/Desktop/23B81A05MU/Numpy(lab3).ipynb 9/9

You might also like