0% found this document useful (0 votes)
3 views7 pages

Numpy ArrayManipulation

The document provides a comprehensive guide on manipulating NumPy arrays, covering topics such as creating random arrays, applying boolean conditions, reshaping arrays, and stacking or splitting them. It demonstrates various functions like `ravel()`, `vstack()`, `hstack()`, and methods for horizontal and vertical splitting. Additionally, it includes examples and error handling for mismatched dimensions during operations.

Uploaded by

nehabhgvt
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)
3 views7 pages

Numpy ArrayManipulation

The document provides a comprehensive guide on manipulating NumPy arrays, covering topics such as creating random arrays, applying boolean conditions, reshaping arrays, and stacking or splitting them. It demonstrates various functions like `ravel()`, `vstack()`, `hstack()`, and methods for horizontal and vertical splitting. Additionally, it includes examples and error handling for mismatched dimensions during operations.

Uploaded by

nehabhgvt
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

Numpy_Array_Manipulation.ipynb - Colab [Link]

Conditions and Boolean Arrays

import numpy as np
A = [Link]((4, 4))
A

array([[0.05737493, 0.96554536, 0.19886106, 0.08974878],


[0.44949292, 0.04698525, 0.49425218, 0.54415645],
[0.85689757, 0.0170971 , 0.70667422, 0.12698457],
[0.66145663, 0.67251577, 0.81321009, 0.54152109]])

##giving boolean value for 2D array


A < 0.5

array([[ True, False, True, True],


[ True, True, True, False],
[False, True, False, True],
[False, False, False, False]])

## generate an array having A<0.5


A[A < 0.5]

array([0.05737493, 0.19886106, 0.08974878, 0.44949292, 0.04698525,


0.49425218, 0.0170971 , 0.12698457])

Shape Manipulation

## Create 1D array
a = [Link](12)
a

array([0.12158074, 0.17265653, 0.33923818, 0.46145824, 0.23439449,


0.38692062, 0.35850106, 0.42385278, 0.61738738, 0.86273847,
0.7632361 , 0.56837861])

##Create 2D array using reshape B=[Link](12).reshape(3,4)


B=[Link](3, 4)
B

array([[0.12158074, 0.17265653, 0.33923818, 0.46145824],


[0.23439449, 0.38692062, 0.35850106, 0.42385278],
[0.61738738, 0.86273847, 0.7632361 , 0.56837861]])

##Create 1-D array into 2-D array


[Link] = (3, 4)
a

array([[0.12158074, 0.17265653, 0.33923818, 0.46145824],


[0.23439449, 0.38692062, 0.35850106, 0.42385278],
[0.61738738, 0.86273847, 0.7632361 , 0.56837861]])

1 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

[0.61738738, 0.86273847, 0.7632361 , 0.56837861]])

ravel() - To convert a two-dimensional array into a one-dimensional array

##ravel() - To convert a two-dimensional array into a one-dimensional array


a = [Link]()
a

array([0.12158074, 0.17265653, 0.33923818, 0.46145824, 0.23439449,


0.38692062, 0.35850106, 0.42385278, 0.61738738, 0.86273847,
0.7632361 , 0.56837861])

B=[Link](12).reshape(3,4)

##can even act directly on the shape attribute of the array itself.
[Link] = ([Link])

array([0.12158074, 0.17265653, 0.33923818, 0.46145824, 0.23439449,


0.38692062, 0.35850106, 0.42385278, 0.61738738, 0.86273847,
0.7632361 , 0.56837861])

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

[Link]()

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

Array Manipulation: to create an array by joining or spli�ing already created arrays.

##Joining Arrays:

Concept of stacking:

���Vertical stacking: vstack(),combines the second array as new rows of the �rst array
���horizontal stacking:hstack(), combines the second arry as new columns of the �rst
array

A=[Link]((3 3))

2 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

A=[Link]((3,3))
A

array([[1., 1., 1.],


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

B=[Link]((3,3))
B

array([[0., 0., 0.],


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

C=[Link]((3,4))
C

array([[0., 0., 0., 0.],


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

[Link]((A,B))

array([[1., 1., 1.],


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

[Link]((A,C)) ## A=3X3 C=4X3

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/[Link] in <cell line: 0>()
----> 1 [Link]((A,C))

/usr/local/lib/python3.12/dist-packages/numpy/_core/shape_base.py in
vstack(tup, dtype, casting)
285 if not isinstance(arrs, tuple):
286 arrs = (arrs,)
--> 287 return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
288
289

ValueError: all the input array dimensions except for the concatenation
axis must match exactly, but along dimension 1, the array at index 0 has
size 3 and the array at index 1 has size 4

[Link]((A,B))

array([[1., 1., 1., 0., 0., 0.],


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

3 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

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

[Link]((A,C)) ##C=3x4 a=3x3

array([[1., 1., 1., 0., 0., 0., 0.],


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

D=[Link]((4,3))
D

array([[0., 0., 0.],


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

[Link]((A,D)) ## same rows are not present in both matrices hence Error occurs

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/[Link] in <cell line: 0>()
----> 1 [Link]((A,D))

/usr/local/lib/python3.12/dist-packages/numpy/_core/shape_base.py in
hstack(tup, dtype, casting)
356 return _nx.concatenate(arrs, 0, dtype=dtype,
casting=casting)
357 else:
--> 358 return _nx.concatenate(arrs, 1, dtype=dtype,
casting=casting)
359
360

ValueError: all the input array dimensions except for the concatenation
axis must match exactly, but along dimension 0, the array at index 0 has

Stacking: used with 1D array, form a 2 D array

column_stack()

row_stack()

a=[Link]([0,1,2])
a

array([0, 1, 2])

b=[Link]([3,4,5])
b

array([3, 4, 5])

4 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

c=[Link]([6,7,8])
c

array([6, 7, 8])

np.column_stack((a,b,c))

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

d=[Link]([6,7,8,9])
d

array([6, 7, 8, 9])

np.column_stack((a,b,d))

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/[Link] in <cell line: 0>()
----> 1 np.column_stack((a,b,d))

/usr/local/lib/python3.12/dist-packages/numpy/lib/_shape_base_impl.py in
column_stack(tup)
660 arr = array(arr, copy=None, subok=True, ndmin=2).T
661 [Link](arr)
--> 662 return _nx.concatenate(arrays, 1)
663
664

ValueError: all the input array dimensions except for the concatenation
axis must match exactly, but along dimension 0, the array at index 0 has
size 3 and the array at index 2 has size 4

np.row_stack((a,b,c))

/tmp/[Link]: DeprecationWarning: `row_stack` alias is depre


np.row_stack((a,b,c))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

Spli�ing array: divide an array into several parts

hsplit(): horizontally

vsplit(): vertically

A=[Link](16).reshape((4,4))
A

5 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])

[B,C]=[Link](A,2)
B

array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]])

array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])

[D,E]=[Link](A,2)
D

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

array([[ 8, 9, 10, 11],


[12, 13, 14, 15]])

[D,E,F,G]=[Link](A,4)
D

array([[0, 1, 2, 3]])

array([[4, 5, 6, 7]])

array([[ 8, 9, 10, 11]])

array([[12, 13, 14, 15]])

6 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab [Link]

7 of 7 03/09/25, 11:15

You might also like