In [1]:
import numpy as np
In [2]:
a = np.arange(24)
In [3]:
print(a)
In [4]:
print(a.shape)
In [5]:
print(a.ndim)
In [6]:
a_4_6 = a.reshape([4, 6])
In [7]:
print(a_4_6)
In [8]:
print(a_4_6.shape)
In [9]:
print(a_4_6.ndim)
In [10]:
a_2_3_4 = a.reshape([2, 3, 4])
In [11]:
print(a_2_3_4)
In [12]:
print(a_2_3_4.shape)
In [13]:
print(a_2_3_4.ndim)
In [14]:
# a_5_6 = a.reshape([5, 6])
# ValueError: cannot reshape array of size 24 into shape (5,6)
In [15]:
print(a.reshape(4, 6))
In [16]:
print(a.reshape(2, 3, 4))
In [17]:
print(np.reshape(a, [4, 6]))
In [18]:
print(np.reshape(a, [2, 3, 4]))
In [19]:
# print(np.reshape(a, [5, 6]))
# ValueError: cannot reshape array of size 24 into shape (5,6)
In [20]:
print(a.reshape(4, 6))
In [21]:
# print(np.reshape(a, 4, 6))
# ValueError: cannot reshape array of size 24 into shape (4,)
In [22]:
print(a.reshape([4, 6], order='C'))
In [23]:
print(a.reshape([4, 6], order='F'))
In [24]:
print(a.reshape([2, 3, 4], order='C'))
In [25]:
print(a.reshape([2, 3, 4], order='F'))
In [26]:
print(np.reshape(a, [4, 6], order='F'))
In [27]:
# print(a.reshape([4, 6], 'F'))
# TypeError: 'list' object cannot be interpreted as an integer
In [28]:
print(np.reshape(a, [4, 6], 'F'))
In [29]:
print(a.reshape([4, -1]))
In [30]:
print(a.reshape([2, -1, 4]))
In [31]:
# print(a.reshape([2, -1, -1]))
# ValueError: can only specify one unknown dimension
In [32]:
# print(a.reshape([2, -1, 5]))
# ValueError: cannot reshape array of size 24 into shape (2,newaxis,5)
In [33]:
a = np.arange(8)
print(a)
In [34]:
a_2_4 = a.reshape([2, 4])
print(a_2_4)
In [35]:
print(np.shares_memory(a, a_2_4))
In [36]:
a[0] = 100
print(a)
In [37]:
print(a_2_4)
In [38]:
a_2_4[0, 0] = 0
print(a_2_4)
In [39]:
print(a)
In [40]:
a_2_4_copy = a.reshape([2, 4]).copy()
print(a_2_4_copy)
In [41]:
print(np.shares_memory(a, a_2_4_copy))
In [42]:
a[0] = 100
print(a)
In [43]:
print(a_2_4_copy)
In [44]:
a_2_4_copy[0, 0] = 200
print(a_2_4_copy)
In [45]:
print(a)
In [46]:
a = np.arange(6).reshape(2, 3)
print(a)
In [47]:
a_step = a[:, ::2]
print(a_step)
In [48]:
print(a_step.reshape(-1))
In [49]:
print(np.shares_memory(a_step, a_step.reshape(-1)))
In [50]:
np.info(a)
In [51]:
np.info(a_step)
In [52]:
np.info(a_step.reshape(-1))
In [53]:
a = np.arange(8).reshape(2, 4)
print(a)
In [54]:
a_step = a[:, ::2]
print(a_step)
In [55]:
print(a_step.reshape(-1))
In [56]:
print(np.shares_memory(a_step, a_step.reshape(-1)))
In [57]:
np.info(a)
In [58]:
np.info(a_step)
In [59]:
np.info(a_step.reshape(-1))