0% found this document useful (0 votes)
25 views30 pages

Numpy

NumPy is a powerful Python library for handling n-dimensional arrays, offering significant performance advantages over traditional lists. It includes features for reshaping arrays and understanding shallow versus deep copies, which affect how data is shared between arrays. The document provides examples of reshaping arrays and demonstrates the differences between shallow and deep copies.

Uploaded by

mahbub sourov
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views30 pages

Numpy

NumPy is a powerful Python library for handling n-dimensional arrays, offering significant performance advantages over traditional lists. It includes features for reshaping arrays and understanding shallow versus deep copies, which affect how data is shared between arrays. The document provides examples of reshaping arrays and demonstrates the differences between shallow and deep copies.

Uploaded by

mahbub sourov
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Numpy

• NumPy (Numerical Python) is a python library used for working with Python arrays. • It offers a high-performance, richly
functional n-dimensional array type called ndarray. • Working domains: Linear algebra and Matrices.
NumPy vs. List:
▪ NumPy is 50x faster than lists.
▪ NumPy data structures take up less space than lists.
▪ NumPy have better runtime behavior.
▪ NumPy array need to be declared but lists don’t.
অক্ষ=0 নির্দিষ্ট করা হলে প্রতিটি কলাম এর মধ্যে থাকা সমস্ত
সারি মানের গড় গণনা করা হয় এবং
অক্ষ=1 সমস্ত প্রতিটি পৃথক সারির মধ্যে কলামের মানের গড়
গণনা করে।
Note: [ : ] is indexing [start:stop:step] ~from * to *
[ , ] is row column [row,column] ~* and *

Indexing of 1,2,3 dimentional array:


For 2d: [1][1] or [1,1] that means 2 num row and 2 num column (both will be
start counting from 0 index) =3
For 3d: [1,2,1] means 1 is 2nd steps/ layer value, 2 is 3rd row, 1 is 2nd column =9
[1:3] means from 1st index to upto 3rd index.

Here, [ :2 (range of row) , :2 (range of column) ] that means at first row will go
from 0 to 1 index and then column will go from 0 to 1 index.
1/ [[0,2,3]] that does not indicate previous row and column. It indicates just 0
and 2 and 3 number index row because of double [[ ]]. Here is not any
column.
2/ [ : , that means ( 0:length) overall full 2d rows. , [2]] means only 2nd index
column from overall 2d row.

Only 3,0 and 1 number index row.


1:3 is index from 1 to 2.
0 , 2 is 0 and 2nd index.
Here we have to give the data inside reshape, how much layer,
row and column we want.
Example 1: Reshape a 1D Array to a 2D Array
Code:
python
Copy code
import numpy as np

# 1D array
array_1d = [Link]([1, 2, 3, 4, 5, 6])
print("Original 1D Array:")
print(array_1d)
print("Shape:", array_1d.shape)
print()

# Reshape to 2D array
array_2d = array_1d.reshape(2, 3)
print("Reshaped to 2D Array:")
print(array_2d)
print("Shape:", array_2d.shape)
print()

Output:
lua
Copy code
Original 1D Array:
[1 2 3 4 5 6]
Shape: (6,)

Reshaped to 2D Array:
[[1 2 3]
[4 5 6]]
Shape: (2, 3)

Example 2: Reshape a 2D Array to a 3D Array


Code:
python
Copy code
# 2D array
array_2d = [Link]([[1, 2, 3], [4, 5, 6], [7, 8,
9], [10, 11, 12]])
print("Original 2D Array:")
print(array_2d)
print("Shape:", array_2d.shape)
print()

# Reshape to 3D array
array_3d = array_2d.reshape(2, 2, 3)
print("Reshaped to 3D Array:")
print(array_3d)
print("Shape:", array_3d.shape)
print()

Output:
lua
Copy code
Original 2D Array:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Shape: (4, 3)
Reshaped to 3D Array:
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]
Shape: (2, 2, 3)
reshape(4,3)
Here 4 is new row size and 3 is new column size.
Shallow Copy and Deep Copy:
Shallow Copy
A shallow copy shares the same data as the original array. Changes to one affect the
other. Created with view()or slicing.
Deep Copy
A deep copy has its own separate data. Changes to one do not affect the other. Created
with copy().
Example: Shallow Copy
Code:
import numpy as np

# Create an original array


original_array = [Link]([1, 2, 3, 4, 5])
print("Original Array:")
print(original_array)

# Create a shallow copy using view


shallow_copy = original_array.view()
print("Shallow Copy:")
print(shallow_copy)

# Modify the shallow copy


shallow_copy[0] = 99
print("After modifying Shallow Copy:")
print("Original Array:")
print(original_array)
print("Shallow Copy:")
print(shallow_copy)
print()

Output:
Original Array:
[1 2 3 4 5]

Shallow Copy:
[1 2 3 4 5]

After modifying Shallow Copy:


Original Array:
[99 2 3 4 5]
Shallow Copy:
[99 2 3 4 5]

Example: Deep Copy


Code:
# Create an original array
original_array = [Link]([1, 2, 3, 4, 5])
print("Original Array:")
print(original_array)

# Create a deep copy using copy


deep_copy = original_array.copy()
print("Deep Copy:")
print(deep_copy)

# Modify the deep copy


deep_copy[0] = 99
print("After modifying Deep Copy:")
print("Original Array:")
print(original_array)
print("Deep Copy:")
print(deep_copy)
print()

Output:
Original Array:
[1 2 3 4 5]

Deep Copy:
[1 2 3 4 5]

After modifying Deep Copy:


Original Array:
[1 2 3 4 5]
Deep Copy:
[99 2 3 4 5]

Explanation
 Shallow Copy (view or slicing): The shallow copy created by view() shares the
same data buffer as the original array. Therefore, changes made to one array
reflect in the other. In the example, modifying the first element of shallow_copy
also changes the first element of original_array.
 Deep Copy (copy): The deep copy created by copy() creates a new array with its
own data buffer. Therefore, changes made to one array do not affect the other. In
the example, modifying the first element of deep_copy does not change the
original_array.

These examples demonstrate the differences between shallow and deep copying in
NumPy, illustrating how data sharing works in each case.

You might also like