[NumPy] reshape(), resize()

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.


<<:  CMoney工程师战斗营weekly4

>>:  CMoney第八届菁英软件工程师战斗营满一个月_Week 4

Day 4 - 类神经网路(二)

形式神经元变形 (原文网址:https://kknews.cc/tech/z34j3rg.html)...

如何恢复iPhone永久删除的照片?

对於大多数 iPhone 用户来说,iPhone 存储管理往往是一件令人头疼的事情。 由於存储空间太...

Day 6: 人工智慧在音乐领域的应用 (AI发展史与简介 - 一战封神AlphaGo)

昨天我们聊到了AI在1987-1993年进入了第二次寒冬,而这段期间里AI成了过街老鼠人人喊打,导致...

Day-16 用类比电视盒来处理落单的主机们

从开始写文至今已经介绍过 HDMI、色差、SCART 和 S-Video 等 4 种端子了、但在序文...

C# 入门之逻辑判断(上)

在讲逻辑判断之前,我们需要了解一下 C# 中的比较运算符,在前面的数据类型和运算符的介绍中,我们有介...