0% found this document useful (0 votes)
32 views6 pages

Numpy

The document provides a tutorial on using the Numpy library in Python, showcasing various functionalities such as creating arrays, manipulating shapes, and performing mathematical operations. It includes examples of creating arrays with different dimensions, generating zeros and identity matrices, and reshaping arrays. Additionally, it demonstrates how to compute sums along specific axes and find indices of minimum and maximum values in arrays.

Uploaded by

cvinit1307
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)
32 views6 pages

Numpy

The document provides a tutorial on using the Numpy library in Python, showcasing various functionalities such as creating arrays, manipulating shapes, and performing mathematical operations. It includes examples of creating arrays with different dimensions, generating zeros and identity matrices, and reshaping arrays. Additionally, it demonstrates how to compute sums along specific axes and find indices of minimum and maximum values in arrays.

Uploaded by

cvinit1307
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/7/25, 10:43 AM Numpy

Numpy
In [1]: import numpy as np

In [2]: myarr = [Link]([[1,2,3,4]], np.int64)

In [3]: myarr

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

In [4]: [Link]

Out[4]: (1, 4)

In [5]: [Link]

Out[5]: dtype('int64')

In [6]: myarr[0,1] = 45

In [7]: myarr

Out[7]: array([[ 1, 45, 3, 4]])

In [8]: #List Making

In [9]: listarray=[Link]([[1,2,3,4],[4,5,6,7]])

In [10]: listarray

Out[10]: array([[1, 2, 3, 4],


[4, 5, 6, 7]])

In [11]: [Link]

Out[11]: (2, 4)

In [12]: [Link]

Out[12]: dtype('int64')

In [15]: arr=[Link]({1,2,3,3})

In [17]: arr

Out[17]: array({1, 2, 3}, dtype=object)

In [18]: [Link]

Out[18]: <function shape at 0x78a4385ffbf0>

[Link] 1/6
9/7/25, 10:43 AM Numpy

In [19]: zeros=[Link]((2,5))

In [20]: zeros

Out[20]: array([[0., 0., 0., 0., 0.],


[0., 0., 0., 0., 0.]])

In [21]: [Link]

Out[21]: (2, 5)

In [22]: [Link]

Out[22]: dtype('float64')

In [23]: arrange=[Link](15)

In [24]: arrange

Out[24]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

In [30]: lspce=[Link](1,1,12)

In [31]: lspce

Out[31]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [32]: lspce2= [Link](1,5,12)

In [33]: lspce2

Out[33]: array([1. , 1.36363636, 1.72727273, 2.09090909, 2.45454545,


2.81818182, 3.18181818, 3.54545455, 3.90909091, 4.27272727,
4.63636364, 5. ])

In [34]: #it gives 12 elements starting form 1 till 5, in which each element is eq

In [35]: lspace3 = [Link](10,50,5)

In [36]: lspace3

Out[36]: array([10., 20., 30., 40., 50.])

In [38]: emp=[Link]((4,6)) #it gives a matrix of 4 rows and 6 columns where each

In [39]: emp

Out[39]: array([[ 3.86274899e-316, 0.00000000e+000, 6.55363568e-310,


-7.48862926e-093, 6.55363499e-310, -8.64973341e+089],
[ 6.55363598e-310, -4.29174464e+245, 6.55363494e-310,
6.14271514e+287, 6.55363584e-310, -8.55859593e-244],
[ 6.55363486e-310, -1.10731191e+179, 6.55363598e-310,
-8.26448569e+236, 6.55363568e-310, -7.39202446e+024],
[ 6.55363569e-310, -1.19244942e+129, 6.55363494e-310,
4.08683961e-005, 6.55363604e-310, 5.64311570e+053]])

[Link] 2/6
9/7/25, 10:43 AM Numpy

In [40]: [Link]

Out[40]: dtype('float64')

In [41]: [Link]

Out[41]: (4, 6)

In [42]: identity_matrix = [Link](40)

In [43]: identity_matrix

Out[43]: array([[1., 0., 0., ..., 0., 0., 0.],


[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]], shape=(40, 40))

In [45]: #This made a 40X40 indentity matrix


identity_matrix.shape

Out[45]: (40, 40)

In [47]: newArray = [Link](99)

In [48]: newArray

Out[48]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1


6,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 3
3,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5
0,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 6
7,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 8
4,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])

In [50]: [Link](3,33)

Out[50]: array([[ 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, 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]])

In [51]: #always keep the multiplication of rows and columns equal to the number o

In [52]: newArray = [Link](9,11)

[Link] 3/6
9/7/25, 10:43 AM Numpy

In [53]: newArray

Out[53]: array([[ 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, 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]])

In [54]: [Link]

Out[54]: (9, 11)

In [58]: a = [Link]() #makes a linear array

In [59]: a

Out[59]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1


6,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 3
3,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5
0,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 6
7,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 8
4,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])

In [62]: [Link]

Out[62]: (99,)

Axis in array
In [63]: # In a 2D-Matrix, to span across row, we set axis = 0 and to spane accros
# axis = 0, goes vertically downwards and axis = 1, goes horizontally fro
# In 1D-Matrx, there is only one axis, axis = 0.

In [64]: x = [[1,2,3],[4,5,6],[7,8,0]]

In [65]: x

Out[65]: [[1, 2, 3], [4, 5, 6], [7, 8, 0]]

In [67]: ar = [Link](x)

In [69]: ar

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


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

[Link] 4/6
9/7/25, 10:43 AM Numpy

In [71]: [Link](axis = 0) #[1+4+7, 2+5+8, 3+6+0]

Out[71]: array([12, 15, 9])

In [72]: [Link](axis = 1) # [1+2+3, 4+5+6, 7+8+0]

Out[72]: array([ 6, 15, 15])

In [73]: ar.T

Out[73]: array([[1, 4, 7],


[2, 5, 8],
[3, 6, 0]])

In [74]: #ar.T = transpose of ar

In [75]: for item in [Link]:


print(item)
#Matrix ar flat keli, printed each element from that matrix

1
2
3
4
5
6
7
8
0

In [76]: [Link]

Out[76]: 2

In [77]: #Becoz there are 2 dimensions, that is axis = 0, and axis = 1

In [78]: [Link]

Out[78]: 9

In [79]: [Link] # total bytes consumed

Out[79]: 72

In [80]: one = [Link]([1,2,3,444,5555])

In [81]: [Link]() #gives index where the largest element is stored

Out[81]: np.int64(4)

In [82]: [Link]() #gives index where the smallest element is stored

Out[82]: np.int64(0)

In [83]: [Link]() #gives an array of indices, this indices when placed correc

[Link] 5/6
9/7/25, 10:43 AM Numpy

Out[83]: array([0, 1, 2, 3, 4])

In [84]: [Link]()

Out[84]: np.int64(8)

In [86]: #minimum value is stored on 8th index, it first flattens the matrix ar, t
#similar for argmax()
[Link]()

Out[86]: np.int64(7)

In [87]: [Link](axis = 0) #This gives an array of indices where each element in

Out[87]: array([2, 2, 1])

In [88]: ar2 = ar + ar

In [89]: ar2

Out[89]: array([[ 2, 4, 6],


[ 8, 10, 12],
[14, 16, 0]])

In [90]: ar * ar2

Out[90]: array([[ 2, 8, 18],


[ 32, 50, 72],
[ 98, 128, 0]])

In [92]: [Link](ar2)

Out[92]: array([[1.41421356, 2. , 2.44948974],


[2.82842712, 3.16227766, 3.46410162],
[3.74165739, 4. , 0. ]])

In [94]: [Link]()

Out[94]: np.int64(36)

In [95]: [Link](ar>5)

Out[95]: (array([1, 2, 2]), array([2, 0, 1]))

In [96]: [Link] * [Link]

Out[96]: 72

In [97]: #gives the size of matrix ar

In [ ]:

[Link] 6/6

You might also like