0% found this document useful (0 votes)
2 views5 pages

Numpy and Array

The document details various operations using NumPy arrays, including creation, slicing, reshaping, and mathematical functions. It demonstrates the use of functions like np.mean, np.dot, and np.linalg.inv, along with handling errors such as ValueError in squeezing arrays. Additionally, it covers generating random arrays and calculating determinants and inverses of matrices.

Uploaded by

princerajstem
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)
2 views5 pages

Numpy and Array

The document details various operations using NumPy arrays, including creation, slicing, reshaping, and mathematical functions. It demonstrates the use of functions like np.mean, np.dot, and np.linalg.inv, along with handling errors such as ValueError in squeezing arrays. Additionally, it covers generating random arrays and calculating determinants and inverses of matrices.

Uploaded by

princerajstem
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

EXPERIMENT NO.

1
TOPIC: Arrays and Numpy Operations

In [2]: import numpy as np


a=[Link]([1,2,3,4,5,6,7,8,9,10])
a

Out[2]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

In [3]: a[3]

Out[3]: np.int64(4)

In [4]: a[2:]

Out[4]: array([ 3, 4, 5, 6, 7, 8, 9, 10])

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


arr

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


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

In [6]: arr[1:2,0:2]

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

In [7]: arr[0:2,1:]

Out[7]: array([[2, 3],


[5, 6]])

In [8]: arr[2:3,0:2]

Out[8]: array([[7, 8]])

In [9]: arr[2:3,2:3]

Out[9]: array([[9]])

In [10]: [Link](arr)

Out[10]: (3, 3)

In [11]: [Link]

Out[11]: 9

In [12]: a=[Link](0,10,2)
a

Out[12]: array([0, 2, 4, 6, 8])


In [13]: [Link]

Out[13]: dtype('int64')

In [14]: [Link](0,10,5)

Out[14]: array([ 0. , 2.5, 5. , 7.5, 10. ])

In [15]: [Link](arr,axis=1)

ValueError Traceback (most recent call last)


/tmp/ipykernel_3264/[Link] in <cell line: 0>()
----> 1 [Link](arr,axis=1)

/usr/local/lib/python3.12/dist-packages/numpy/_core/[Link] in squeez
e(a, axis)
1630 return squeeze()
1631 else:
-> 1632 return squeeze(axis=axis)
1633
1634

ValueError: cannot select an axis to squeeze out which has size not equal to on
e

In [16]: br=[Link]()

In [17]: ai=[Link](3,3)
ai

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


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

In [18]: ai=[Link]([[1,2,3],[4,5,6],[7,8,9]])
q=[Link](ai,(12,3))
q

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


[4, 5, 6],
[7, 8, 9],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

In [19]: [Link](3,5)
Out[19]: array([[0.69090421, 0.813085 , 0.7513965 , 0.16561777, 0.93282322],
[0.47744403, 0.11792459, 0.47568609, 0.71743712, 0.80386051],
[0.03810498, 0.54742223, 0.57933578, 0.91171801, 0.60072783]])

In [20]: s=[Link](0,100,(3,4))
s

Out[20]: array([[10, 19, 98, 81],


[14, 34, 26, 50],
[41, 53, 66, 45]])

In [21]: [Link](1,100)

Out[21]: 20

In [22]: [Link](br)

Out[22]: np.float64(5.0)

In [23]: [Link](s)

Out[23]: np.float64(44.75)

In [24]: [Link](ai)

Out[24]: np.float64(5.0)

In [25]: [Link](ai)

Out[25]: np.float64(2.581988897471611)

In [26]: ai

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


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

In [27]: [Link](s,axis=1)

Out[27]: array([2, 3, 2])

In [28]: [Link](s,axis=0)

Out[28]: array([2, 2, 0, 0])

In [29]: s1=[Link]([[1,2,3],[4,5,6],[7,8,9]])
s2=[Link]([[2,2,3],[2,5,1],[7,1,2]])

In [30]: [Link](s1,s2)
Out[30]: array([[27, 15, 11],
[60, 39, 29],
[93, 63, 47]])

In [31]: [Link](s1,s2)

Out[31]: array([[ 3, 4, 6],


[ 6, 10, 7],
[14, 9, 11]])

In [32]: [Link](s1,s2)

Out[32]: array([[-1, 0, 0],


[ 2, 0, 5],
[ 0, 7, 7]])

In [33]: [Link](s1,s2)

Out[33]: array([[0.5, 1. , 1. ],
[2. , 1. , 6. ],
[1. , 8. , 4.5]])

In [38]: import numpy as np

s1 = [Link]([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

det_s1 = [Link](s1)
print(f"Determinant of s1: {det_s1}")

Determinant of s1: 0.0

In [39]: s3 = [Link]([[1, 2, 3],


[0, 1, 4],
[5, 6, 0]])
a_inv = [Link](s3)
print("Inverse of s3 (a_inv):\n", a_inv)
result = [Link](s3, a_inv)
print("\nResult of s3 dot a_inv:\n", result)

Inverse of s3 (a_inv):
[[-24. 18. 5.]
[ 20. -15. -4.]
[ -5. 4. 1.]]

Result of s3 dot a_inv:


[[ 1.00000000e+00 -2.66453526e-15 0.00000000e+00]
[ 0.00000000e+00 1.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 -7.10542736e-15 1.00000000e+00]]

In [44]: import numpy as np

s1 = [Link]([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

det_s1 = [Link](s1)
print(f"Determinant of s1: {det_s1}")

Determinant of s1: 0.0

You might also like