Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

numpy reshape

import numpy as np

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

# print 2d array shape
print(arr.shape)
# output (2, 4)


# 4 dimension array
arr = np.array([1, 2, 3, 4], ndmin=4)

print(arr)
print('shape of array :', arr.shape)


# reshape array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
arr = arr.reshape(4, 3)
print(arr)

# output
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]

# reshape uneven array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
arr = arr.reshape(4, 3)
print(arr)

# output ValueError: cannot reshape array of size 11 into shape (4,3)
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #numpy #reshape
ADD COMMENT
Topic
Name
5+1 =