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

Numpy ArrayManipulation

Uploaded by

omkarmeher8689
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)
6 views7 pages

Numpy ArrayManipulation

Uploaded by

omkarmeher8689
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/ 7

Numpy_Array_Manipulation.ipynb - Colab https://colab.research.google.com/drive/1gIn2TDLWy...

Conditions and Boolean Arrays

import numpy as np
A = np.random.random((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 = np.random.random(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=np.arange(12).reshape(3,4)


B=a.reshape(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


a.shape = (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 https://colab.research.google.com/drive/1gIn2TDLWy...

[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 = a.ravel()
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=np.arange(12).reshape(3,4)

##can even act directly on the shape attribute of the array itself.
a.shape = (B.size)

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]])

B.transpose()

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=np.ones((3 3))

2 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab https://colab.research.google.com/drive/1gIn2TDLWy...

A=np.ones((3,3))
A

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


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

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

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


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

C=np.zeros((3,4))
C

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


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

np.vstack((A,B))

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


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

np.vstack((A,C)) ## A=3X3 C=4X3

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-1132681970.py in <cell line: 0>()
----> 1 np.vstack((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

np.hstack((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 https://colab.research.google.com/drive/1gIn2TDLWy...

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

np.hstack((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=np.zeros((4,3))
D

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


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

np.hstack((A,D)) ## same rows are not present in both matrices hence Error occurs

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-4185310927.py in <cell line: 0>()
----> 1 np.hstack((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=np.array([0,1,2])
a

array([0, 1, 2])

b=np.array([3,4,5])
b

array([3, 4, 5])

4 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab https://colab.research.google.com/drive/1gIn2TDLWy...

c=np.array([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=np.array([6,7,8,9])
d

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

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

---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-1740448016.py 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 arrays.append(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/ipython-input-3391918380.py:1: 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=np.arange(16).reshape((4,4))
A

5 of 7 03/09/25, 11:15
Numpy_Array_Manipulation.ipynb - Colab https://colab.research.google.com/drive/1gIn2TDLWy...

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

[B,C]=np.hsplit(A,2)
B

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

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

[D,E]=np.vsplit(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]=np.vsplit(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 https://colab.research.google.com/drive/1gIn2TDLWy...

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

You might also like