KEMBAR78
FDS Exp3 | PDF | Matrix (Mathematics) | Linear Algebra
0% found this document useful (0 votes)
4 views5 pages

FDS Exp3

NumPy is a powerful library for numerical computing in Python, centered around the N-dimensional array object (ndarray), which is more efficient than Python lists. It offers benefits such as efficient memory usage and fast computation through vectorized operations, allowing for easy creation and manipulation of arrays. Key features include mathematical operations, linear algebra capabilities, and broadcasting, making it essential for data analysis and scientific computing.

Uploaded by

harsh.pandey22
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)
4 views5 pages

FDS Exp3

NumPy is a powerful library for numerical computing in Python, centered around the N-dimensional array object (ndarray), which is more efficient than Python lists. It offers benefits such as efficient memory usage and fast computation through vectorized operations, allowing for easy creation and manipulation of arrays. Key features include mathematical operations, linear algebra capabilities, and broadcasting, making it essential for data analysis and scientific computing.

Uploaded by

harsh.pandey22
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

What is NumPy?

NumPy is built around the concept of the N-dimensional array object, or ndarray. It is a
homogeneous collection of elements (numbers) stored in contiguous memory locations. This
array structure is much more efficient than Python lists, both in terms of memory and
computational speed. The ndarray allows for efficient operations like matrix multiplication,
element-wise addition, and statistical calculations.

Key Benefits of NumPy:-

• Efficient Memory Usage: Arrays in NumPy are stored more compactly than lists,
which helps reduce memory overhead.

• Fast Computation: NumPy uses vectorized operations (operations that apply at once
to the entire array) that allow for fast computation without the need for explicit
loops
1. Importing NumPy:-

import numpy as np

2.Creating NumPy Arrays:-

NumPy arrays are similar to Python lists but more efficient and allow you to perform vectorized
operations.

• 1D Array (Vector):
# Create a 1D array (vector)
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)

[1 2 3 4 5]

• 2D Array (Matrix):
# Create a 2D array (matrix)
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2)

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

• 3D Array(Matrix)
# Create a 3D array
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3)
[[[1 2]
[3 4]]

[[5 6]
[7 8]]]

3.Array Properties:-

You can inspect important properties of a NumPy array like its shape, size, and type.

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

print(arr.shape)

print(arr.size)

# Data type of elements


print(arr.dtype)

(2, 3)
6
int64

4.Generating Arrays:-

NumPy provides several functions to create arrays filled with certain values:

• Zeros:- Create an array of zeros.


zeros = np.zeros((3, 3))
print(zeros)

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

• Ones:- Create an array of ones.


ones = np.ones((2, 3))
print(ones)

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

• Range of Numbers (arange):- Create an array with a range of numbers.


range_array = np.arange(0, 10, 3)
print(range_array)

[0 3 6 9]

• Random Values:- Create an array with random values.


random_array = np.random.random((3, 3, 3))
print(random_array)

[[[0.92640756 0.43375216 0.64962087]


[0.90229118 0.86267935 0.51415112]
[0.47479624 0.33766512 0.97328937]]

[[0.75120097 0.163952 0.17608495]


[0.10505401 0.95680892 0.1923433 ]
[0.19384736 0.5786261 0.81881285]]

[[0.19808137 0.77760343 0.84276766]


[0.64745235 0.33736927 0.8897564 ]
[0.01341966 0.40681822 0.37730376]]]

• Identity Matrix:- Create an identity matrix.


identity_matrix = np.eye(5)
print(identity_matrix)

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

5.Mathematical Operations on Arrays:-

One of the biggest advantages of NumPy is the ability to perform element-wise operations on
arrays without the need for loops (vectorization).

# Creating arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Element-wise addition
print(arr1 + arr2) # Outputs: [5 7 9]

# Element-wise multiplication
print(arr1 * arr2) # Outputs: [4 10 18]

# Scalar multiplication
print(arr1 * 2) # Outputs: [2 4 6]

# Exponentiation
print(arr1 ** 2) # Outputs: [1 4 9]

[5 7 9]
[ 4 10 18]
[2 4 6]
[1 4 9]
6.Saving and Loading Data:-

NumPy allows you to save arrays to files and load them back.

• Save to File:
arr = np.array([1, 2, 3])
np.save('my_array.npy', arr)

• Load from File:


loaded_array = np.load('my_array.npy')
print(loaded_array) # Outputs: [1 2 3]

[1 2 3]

7.Linear Algebra:-

NumPy includes a module for linear algebra operations, such as matrix multiplication,
determinant, inverse, etc.

# Matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication


result = np.dot(A, B)
print(result) # Outputs: [[19 22] [43 50]]

# Finding the inverse of a matrix


inverse_A = np.linalg.inv(A)
print(inverse_A)

# Determinant of a matrix
det_A = np.linalg.det(A)
print(det_A)

[[19 22]
[43 50]]
[[-2. 1. ]
[ 1.5 -0.5]]
-2.0000000000000004

8.Broadcasting:-

Broadcasting is a powerful mechanism that allows NumPy to perform operations on arrays of


different shapes.

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

# Adding a scalar to all elements


print(arr + 10) # Outputs: [[11 12 13] [14 15 16]]

# Adding a 1D array to a 2D array


arr2 = np.array([1, 2, 3])
print(arr + arr2) # Outputs: [[ 2 4 6] [ 5 7 9]]

[[11 12 13]
[14 15 16]]
[[2 4 6]
[5 7 9]]

You might also like