0% found this document useful (0 votes)
0 views3 pages

Python Exp8

Uploaded by

BADASS GAMING YT
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)
0 views3 pages

Python Exp8

Uploaded by

BADASS GAMING YT
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/ 3

Experiment : 08

Title : Exploring basics of NumPy Methods

Theory:

NumPy is a powerful library for numerical computations in Python. It provides a set of


methods and functions for working with arrays, matrices, and numerical data. Some of
the basic methods of NumPy are:

1. Creating Arrays: NumPy provides several ways to create arrays. The most
common way is to use the array() function. For example, to create a
one-dimensional array of integers, you can use:
import numpy as np
arr = np.array([1, 2, 3, 4,
5])
print(arr)

Output: [1 2 3 4 5]

You can also create arrays of zeros, ones, or a range of numbers using functions
like zeros(), ones(), and arange().

2. Accessing Elements: You can access individual elements of a NumPy array using
their index. For example, to access the first element of the array created above, you
can use:

print(arr[0])

Output:
1

You can also access a range of elements using slicing. For example, to access the
first three elements of the array, you can use:
print(arr[:3])

Output:
[1 2 3]

3. Basic Operations: NumPy provides several basic operations for working with
arrays. For example, you can perform element-wise addition, subtraction,
multiplication, and division using arithmetic operators. For example:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
print(a - b)
print(a * b)
print(a / b)

Output:

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

4. Broadcasting: NumPy supports broadcasting, which allows you to perform


arithmetic operations on arrays of different shapes and sizes. For example:

a = np.array([1, 2, 3])
b = 2
print(a + b)

Output:

[3 4 5]

In this example, the scalar value b is broadcasted to match the shape of the array a.

5. Universal Functions: NumPy provides a set of universal functions (ufuncs) that


operate element-wise on arrays. These functions include mathematical functions
like sin(), cos(), and exp(), and statistical functions like mean(), median(), and
std(). For example:

a = np.array([1, 2, 3])
print(np.sin(a))
print(np.mean(a))

Output:

[0.84147098 0.90929743 0.14112001]


2.0

6. Shape Manipulation: NumPy provides several functions for manipulating the shape
of arrays. For example, you can reshape an array using the reshape() function:

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

Output:

[[1 2 3]
[4 5 6]]
You can also transpose an array using the transpose() function:

a = np.array([[1, 2, 3], [4, 5, 6]])


b = np.transpose(a)
print(b)
Output:
[[1 4]
[2 5]
[3 6]]

Conclusion: We have learned the basics of NumPy Methods.

You might also like