iT邦幫忙

0

[NumPy] reshape(), resize()

  • 分享至 

  • xImage
  •  

Approches to transform the shape of ndarray

  • numpy.reshape(array, (shape), order = 'C')
    transform [8, 1] to [2, 4]
import numpy as np

arrayA = np.arange(8)
# arrayA = array([0, 1, 2, 3, 4, 5, 6, 7])

np.reshape(arrayA, (2, 4))
# array([[0, 1, 2, 3],
        [4, 5, 6, 7]])

If the number of elements is not same between input and output, raise ValueError

  • numpy.reshape(array, (shape), order = 'F')
np.reshape(arrayA, (2, 4), order = 'F')

# array([[0, 2, 4, 6],
        [1, 3, 5, 7]])
  • ndarray.reshape()
arrayB = arrayA.reshape(2, 4)

ndarray.reshape() doesn't change the data from original matrix, it returns the modified one

  • numpy.resize()
arrayA = np.arange(8)
arrayB = arrayA.resize(arrayA, (2, 4))
# array([[0, 1, 2, 3],
         [4, 5, 6, 7]])
arrayC = np.resize(arrayA, (3, 4))
arrayC
# array([[0, 1, 2, 3],
         [4, 5, 6, 7],
         [0, 1, 2 ,3]])

If the new size is larger than the original one, it will repeat the elements to fill the spaces.


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言