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

Python

The document provides a series of Python code snippets demonstrating the use of the NumPy library for various operations, including array creation, mathematical operations, and linear algebra functions. It covers array initialization with zeros, ones, and random values, as well as array manipulation techniques like slicing and concatenation. Additionally, it includes examples of statistical calculations and matrix operations such as dot and cross products.

Uploaded by

mnvtarsariya29
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 views5 pages

Python

The document provides a series of Python code snippets demonstrating the use of the NumPy library for various operations, including array creation, mathematical operations, and linear algebra functions. It covers array initialization with zeros, ones, and random values, as well as array manipulation techniques like slicing and concatenation. Additionally, it includes examples of statistical calculations and matrix operations such as dot and cross products.

Uploaded by

mnvtarsariya29
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/ 5

python

February 6, 2025

[2]: import numpy as np


arr = np.array([1, 2, 3, 4])
arr

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

[7]: arr = np.arange(10)


arr

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

[13]: zeros = np.zeros((2, 3))


zeros

[13]: array([[0., 0., 0.],


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

[11]: ones = np.ones((2, 3))


ones

[11]: array([[1., 1., 1.],


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

[12]: empty = np.empty((2, 3))


empty

[12]: array([[1., 1., 1.],


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

[14]: full = np.full((2, 3), 7)


full

[14]: array([[7, 7, 7],


[7, 7, 7]])

[15]: identity = np.eye(3)


identity

1
[15]: array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

[16]: arr = np.array([[1, 2, 3], [4, 5, 6]])


print(arr.shape)
print(arr.size)
print(arr.ndim)
print(arr.dtype)
print(arr.itemsize)

(2, 3)
6
2
int32
4

[17]: arr = np.array([10, 20, 30, 40, 50])


print(arr[0])
print(arr[-1])
print(arr[1:4])
print(arr[:3])
print(arr[::2])

10
50
[20 30 40]
[10 20 30]
[10 30 50]

[18]: arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


print(arr[0, 1])
print(arr[:, 1])
print(arr[1:, 1:])

2
[2 5 8]
[[5 6]
[8 9]]

[19]: arr = np.arange(6).reshape((2, 3))


arr

[19]: array([[0, 1, 2],


[3, 4, 5]])

2
[20]: arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
concat = np.concatenate((arr1, arr2))
concat

[20]: array([1, 2, 3, 4, 5, 6])

[21]: arr = np.array([1, 2, 3])


print(arr + 10)
print(arr - 10)
print(arr * 10)
print(arr / 10)
print(arr ** 2)

[11 12 13]
[-9 -8 -7]
[10 20 30]
[0.1 0.2 0.3]
[1 4 9]

[22]: arr1 = np.array([1, 2, 3])


arr2 = np.array([4, 5, 6])
print(arr1 + arr2)
print(arr1 - arr2)
print(arr1 * arr2)
print(arr1 / arr2)

[5 7 9]
[-3 -3 -3]
[ 4 10 18]
[0.25 0.4 0.5 ]

[23]: arr = np.array([1, 2, 3, 4])


print(np.sqrt(arr))
print(np.exp(arr))
print(np.log(arr))
print(np.sin(arr))

[1. 1.41421356 1.73205081 2. ]


[ 2.71828183 7.3890561 20.08553692 54.59815003]
[0. 0.69314718 1.09861229 1.38629436]
[ 0.84147098 0.90929743 0.14112001 -0.7568025 ]

[24]: arr = np.array([[1, 2, 3], [4, 5, 6]])


print(np.mean(arr))
print(np.median(arr))
print(np.std(arr))
print(np.var(arr))

3
print(np.min(arr))
print(np.max(arr))
print(np.sum(arr))

3.5
3.5
1.707825127659933
2.9166666666666665
1
6
21

[26]: rand = np.random.rand(3, 3)


print(rand)
randn = np.random.randn(3, 3)
print(randn)
randint = np.random.randint(0, 10, (3, 3))
print(randint)

[[0.89863386 0.66033717 0.90603822]


[0.61945405 0.91886152 0.72712725]
[0.97641642 0.20516689 0.92979643]]
[[ 1.49021887 1.43104864 -0.49661225]
[ 0.24681302 0.52884867 0.65720922]
[-1.25622051 0.8608327 -0.3720419 ]]
[[8 7 1]
[4 3 3]
[4 7 1]]

[29]: arr = np.array([[1, 2], [3, 4]])


print(np.linalg.inv(arr)) # inverse
print(np.linalg.det(arr)) # determinant
print(np.linalg.eig(arr)) # eigenvalues and eigenvectors

[[-2. 1. ]
[ 1.5 -0.5]]
-2.0000000000000004
EigResult(eigenvalues=array([-0.37228132, 5.37228132]),
eigenvectors=array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))

[30]: arr = np.array([[1, 2, 3], [4, 5, 6]])


transpose = arr.T
print(transpose)

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

4
[31]: arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
dot_product = np.dot(arr1, arr2)
print(dot_product)

32

[32]: arr1 = np.array([1, 2, 3])


arr2 = np.array([4, 5, 6])
cross_product = np.cross(arr1, arr2)
print(cross_product)

[-3 6 -3]

[33]: arr = np.array([3, 1, 2, 5, 4])


sorted_arr = np.sort(arr)
print(sorted_arr)

[1 2 3 4 5]

[ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy