import numpy as npx = np.array([1,2,3,4,5,6,7,8])
print(x.shape) #(8,)
x.shape=[2, 4]
print(x)
# [[1 2 9 4]
#  [5 6 7 8]]
(8,)
[[1 2 3 4][5 6 7 8]]
# numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。
x = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]])
y = x.flat
print(y)
# <numpy.flatiter object at 0x0000020F9BA10C60>
for i in y:print(i, end=' ')
# 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35y[3] = 0
print(end='\n')
print(x)
# [[11 12 13  0 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
<numpy.flatiter object at 0x55fc58fa62d0>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
[[11 12 13  0 15][16 17 18 19 20][21 22 23 24 25][26 27 28 29 30][31 32 33 34 35]]
x = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]])
y = x.flatten()
print(y)
# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#  35]y[3] = 0
print(x)
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]x = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]])y = x.flatten(order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
#  35]y[3] = 0
print(x)
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3435]
[[11 12 13 14 15][16 17 18 19 20][21 22 23 24 25][26 27 28 29 30][31 32 33 34 35]]
[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 3035]
[[11 12 13 14 15][16 17 18 19 20][21 22 23 24 25][26 27 28 29 30][31 32 33 34 35]]
x = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]])
y = np.ravel(x)
print(y)
# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#  35]y[3] = 0
print(x)
# [[11 12 13  0 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3435]
[[11 12 13  0 15][16 17 18 19 20][21 22 23 24 25][26 27 28 29 30][31 32 33 34 35]]
x = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]])y = np.ravel(x, order='F')
print(y)
# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30
#  35]y[3] = 0
print(x)
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 3035]
[[11 12 13 14 15][16 17 18 19 20][21 22 23 24 25][26 27 28 29 30][31 32 33 34 35]]
# reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。
import numpy as npx = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype)  # int64
print(y)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]y = np.reshape(x, [3, -1])
print(y)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]y = np.reshape(x,[-1,3])
print(y)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]]y[0, 1] = 10
print(x)
# [ 0 10  2  3  4  5  6  7  8  9 10 11](改变x去reshape后y中的值,x对应元素也改变)
int64
[[ 0  1  2  3][ 4  5  6  7][ 8  9 10 11]]
[[ 0  1  2  3][ 4  5  6  7][ 8  9 10 11]]
[[ 0  1  2][ 3  4  5][ 6  7  8][ 9 10 11]]
[ 0 10  2  3  4  5  6  7  8  9 10 11]
# reshape()函数当参数newshape = -1时,表示将数组降为一维
import numpy as npx = np.random.randint(12, size=[2, 2, 3])
print(x)y = np.reshape(x, -1)
print(y)
[[[ 1  3  0][ 2  1  2]][[ 3  1  5][ 6  3 10]]]
[ 1  3  0  2  1  2  3  1  5  6  3 10]
import numpy as npx = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)y = x.T
print(y)z = np.transpose(x)
print(z)print(z == y)
[[3.9  6.09 1.42 0.79 5.18][9.83 2.98 8.59 1.51 9.06][4.6  5.62 1.9  6.59 2.71][9.4  8.75 1.8  3.58 8.36][7.27 7.5  1.85 1.69 7.62]]
[[3.9  9.83 4.6  9.4  7.27][6.09 2.98 5.62 8.75 7.5 ][1.42 8.59 1.9  1.8  1.85][0.79 1.51 6.59 3.58 1.69][5.18 9.06 2.71 8.36 7.62]]
[[3.9  9.83 4.6  9.4  7.27][6.09 2.98 5.62 8.75 7.5 ][1.42 8.59 1.9  1.8  1.85][0.79 1.51 6.59 3.58 1.69][5.18 9.06 2.71 8.36 7.62]]
[[ True  True  True  True  True][ True  True  True  True  True][ True  True  True  True  True][ True  True  True  True  True][ True  True  True  True  True]]
# 很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis参数来增加一个维度
import numpy as npx = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
print(x)  # [1 2 9 4 5 6 7 8]y = x[np.newaxis, :]
print(y.shape)  # (1, 8)
print(y)  # [[1 2 9 4 5 6 7 8]]y = x[:, np.newaxis]
print(y.shape)  # (8, 1)
print(y)
# [[1]
#  [2]
#  [9]
#  [4]
#  [5]
#  [6]
#  [7]
#  [8]]
(8,)
[1 2 9 4 5 6 7 8]
(1, 8)
[[1 2 9 4 5 6 7 8]]
(8, 1)
[[1][2][9][4][5][6][7][8]]
# numpy.squeeze()
import numpy as npx = np.arange(10)
print(x.shape)  # (10,)
x = x[np.newaxis, :]
print(x.shape)  # (1, 10)
y = np.squeeze(x)
print(y.shape)  # (10,)
(10,)
(1, 10)
(10,)
import numpy as npx = np.array([[[0], [1], [2]]])
print(x.shape)  # (1, 3, 1)
print(x)
# [[[0]
#   [1]
#   [2]]]y = np.squeeze(x)
print(y.shape)  # (3,)
print(y)  # [0 1 2]y = np.squeeze(x, axis=0)
print(y.shape)  # (3, 1)
print(y)
# [[0]
#  [1]
#  [2]]y = np.squeeze(x, axis=2)
print(y.shape)  # (1, 3)
print(y)  # [[0 1 2]]y = np.squeeze(x, axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one
(1, 3, 1)
[[[0][1][2]]]
(3,)
[0 1 2]
(3, 1)
[[0][1][2]]
(1, 3)
[[0 1 2]]---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)/tmp/ipykernel_560914/2187432804.py in <module>23 print(y)  # [[0 1 2]]24
---> 25 y = np.squeeze(x, axis=1)26 # ValueError: cannot select an axis to squeeze out which has size not equal to one<__array_function__ internals> in squeeze(*args, **kwargs)~/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/core/fromnumeric.py in squeeze(a, axis)1506         return squeeze()1507     else:
-> 1508         return squeeze(axis=axis)1509 1510 ValueError: cannot select an axis to squeeze out which has size not equal to one
import numpy as np
import matplotlib.pyplot as pltx = np.array([[1, 4, 9, 16, 25]])
print(x.shape)  # (1, 5)
plt.plot(x)
plt.show()
(1, 5)

y = np.squeeze(x)
print(y.shape)
print(y)plt.plot(y)
plt.show()
(5,)
[ 1  4  9 16 25]

# 数组组合
#连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)
import numpy as npx = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)
# [1 2 3 7 8 9]z = np.concatenate([x, y], axis=0)
print(z)
# [1 2 3 7 8 9]
[1 2 3 7 8 9]
[1 2 3 7 8 9]
# 原来x,y都是二维的,拼接后的结果也是二维的。
import numpy as npx = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)
# [[ 1  2  3]
#  [ 7  8  9]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1  2  3]
#  [ 7  8  9]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1  2  3  7  8  9]]
[[1 2 3][7 8 9]]
[[1 2 3][7 8 9]]
[[1 2 3 7 8 9]]
# x,y在原来的维度上进行拼接。
import numpy as npx = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.concatenate([x, y])
print(z)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]
z = np.concatenate([x, y], axis=0)
print(z)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]
z = np.concatenate([x, y], axis=1)
print(z)
# [[ 1  2  3  7  8  9]
#  [ 4  5  6 10 11 12]]
[[ 1  2  3][ 4  5  6][ 7  8  9][10 11 12]]
[[ 1  2  3][ 4  5  6][ 7  8  9][10 11 12]]
[[ 1  2  3  7  8  9][ 4  5  6 10 11 12]]
# 沿着新的轴加入一系列数组(stack为增加维度的拼接)。np.stack()
import numpy as npx = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]z = np.stack([x, y], axis=1)
print(z.shape)  # (3, 2)
print(z)
# [[1 7]
#  [2 8]
#  [3 9]]
(2, 3)
[[1 2 3][7 8 9]]
(3, 2)
[[1 7][2 8][3 9]]
import numpy as npx = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.stack([x, y])
print(z.shape)  # (2, 1, 3)
print(z)
# [[[1 2 3]]
#
#  [[7 8 9]]]z = np.stack([x, y], axis=1)
print(z.shape)  # (1, 2, 3)
print(z)
# [[[1 2 3]
#   [7 8 9]]]z = np.stack([x, y], axis=2)
print(z.shape)  # (1, 3, 2)
print(z)
# [[[1 7]
#   [2 8]
#   [3 9]]]
(2, 1, 3)
[[[1 2 3]][[7 8 9]]]
(1, 2, 3)
[[[1 2 3][7 8 9]]]
(1, 3, 2)
[[[1 7][2 8][3 9]]]
import numpy as npx = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.stack([x, y])
print(z.shape)  # (2, 2, 3)
print(z)
# [[[ 1  2  3]
#   [ 4  5  6]]
#
#  [[ 7  8  9]
#   [10 11 12]]]z = np.stack([x, y], axis=1)
print(z.shape)  # (2, 2, 3)
print(z)
# [[[ 1  2  3]
#   [ 7  8  9]]
#
#  [[ 4  5  6]
#   [10 11 12]]]z = np.stack([x, y], axis=2)
print(z.shape)  # (2, 3, 2)
print(z)
# [[[ 1  7]
#   [ 2  8]
#   [ 3  9]]
#
#  [[ 4 10]
#   [ 5 11]
#   [ 6 12]]]
(2, 2, 3)
[[[ 1  2  3][ 4  5  6]][[ 7  8  9][10 11 12]]]
(2, 2, 3)
[[[ 1  2  3][ 7  8  9]][[ 4  5  6][10 11 12]]]
(2, 3, 2)
[[[ 1  7][ 2  8][ 3  9]][[ 4 10][ 5 11][ 6 12]]]
import numpy as npx = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.vstack((x, y))
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]z = np.stack([x, y])
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]z = np.hstack((x, y))
print(z.shape)  # (6,)
print(z)
# [1  2  3  7  8  9]z = np.concatenate((x, y))
print(z.shape)  # (6,)
print(z)  # [1 2 3 7 8 9]
(2, 3)
[[1 2 3][7 8 9]]
(2, 3)
[[1 2 3][7 8 9]]
(6,)
[1 2 3 7 8 9]
(6,)
[1 2 3 7 8 9]
import numpy as npx = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.vstack((x, y))
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]z = np.concatenate((x, y), axis=0)
print(z.shape)  # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]z = np.hstack((x, y))
print(z.shape)  # (1, 6)
print(z)
# [[ 1  2  3  7  8  9]]z = np.concatenate((x, y), axis=1)
print(z.shape)  # (1, 6)
print(z)
# [[1 2 3 7 8 9]]
(2, 3)
[[1 2 3][7 8 9]]
(2, 3)
[[1 2 3][7 8 9]]
(1, 6)
[[1 2 3 7 8 9]]
(1, 6)
[[1 2 3 7 8 9]]
import numpy as npx = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.vstack((x, y))
print(z.shape)  # (4, 3)
print(z)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]z = np.concatenate((x, y), axis=0)
print(z.shape)  # (4, 3)
print(z)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]z = np.hstack((x, y))
print(z.shape)  # (2, 6)
print(z)
# [[ 1  2  3  7  8  9]
#  [ 4  5  6 10 11 12]]z = np.concatenate((x, y), axis=1)
print(z.shape)  # (2, 6)
print(z)
# [[ 1  2  3  7  8  9]
#  [ 4  5  6 10 11 12]]
(4, 3)
[[ 1  2  3][ 4  5  6][ 7  8  9][10 11 12]]
(4, 3)
[[ 1  2  3][ 4  5  6][ 7  8  9][10 11 12]]
(2, 6)
[[ 1  2  3  7  8  9][ 4  5  6 10 11 12]]
(2, 6)
[[ 1  2  3  7  8  9][ 4  5  6 10 11 12]]
import numpy as npa = np.hstack([np.array([1, 2, 3, 4]), 5])
print(a)  # [1 2 3 4 5]a = np.concatenate([np.array([1, 2, 3, 4]), 5])
print(a)
# all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
[1 2 3 4 5]---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)/tmp/ipykernel_560914/3703934453.py in <module>4 print(a)  # [1 2 3 4 5]5
----> 6 a = np.concatenate([np.array([1, 2, 3, 4]), 5])7 print(a)8 # all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)<__array_function__ internals> in concatenate(*args, **kwargs)ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
# 拆分数组
import numpy as npx = np.array([[11, 12, 13, 14],[16, 17, 18, 19],[21, 22, 23, 24]])
y = np.split(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
#        [16],
#        [21]]), array([[12, 13],
#        [17, 18],
#        [22, 23]]), array([[14],
#        [19],
#        [24]])]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],[21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int64)]
[array([[11],[16],[21]]), array([[12, 13],[17, 18],[22, 23]]), array([[14],[19],[24]])]
# 垂直切分是把数组按照高度切分
import numpy as npx = np.array([[11, 12, 13, 14],[16, 17, 18, 19],[21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]y = np.split(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]y = np.vsplit(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
#        [21, 22, 23, 24]])]y = np.split(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
#        [21, 22, 23, 24]])]y = np.vsplit(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=0)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],[21, 22, 23, 24]])]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],[21, 22, 23, 24]])]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],[21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int64)]
[array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],[21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int64)]
import numpy as npx = np.array([[11, 12, 13, 14],[16, 17, 18, 19],[21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)
# [array([[11, 12],
#        [16, 17],
#        [21, 22]]), array([[13, 14],
#        [18, 19],
#        [23, 24]])]y = np.split(x, 2, axis=1)
print(y)
# [array([[11, 12],
#        [16, 17],
#        [21, 22]]), array([[13, 14],
#        [18, 19],
#        [23, 24]])]y = np.hsplit(x, [3])
print(y)
# [array([[11, 12, 13],
#        [16, 17, 18],
#        [21, 22, 23]]), array([[14],
#        [19],
#        [24]])]y = np.split(x, [3], axis=1)
print(y)
# [array([[11, 12, 13],
#        [16, 17, 18],
#        [21, 22, 23]]), array([[14],
#        [19],
#        [24]])]y = np.hsplit(x, [1, 3])
print(y)
# [array([[11],
#        [16],
#        [21]]), array([[12, 13],
#        [17, 18],
#        [22, 23]]), array([[14],
#        [19],
#        [24]])]y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
#        [16],
#        [21]]), array([[12, 13],
#        [17, 18],
#        [22, 23]]), array([[14],
#        [19],
#        [24]])]
[array([[11, 12],[16, 17],[21, 22]]), array([[13, 14],[18, 19],[23, 24]])]
[array([[11, 12],[16, 17],[21, 22]]), array([[13, 14],[18, 19],[23, 24]])]
[array([[11, 12, 13],[16, 17, 18],[21, 22, 23]]), array([[14],[19],[24]])]
[array([[11, 12, 13],[16, 17, 18],[21, 22, 23]]), array([[14],[19],[24]])]
[array([[11],[16],[21]]), array([[12, 13],[17, 18],[22, 23]]), array([[14],[19],[24]])]
[array([[11],[16],[21]]), array([[12, 13],[17, 18],[22, 23]]), array([[14],[19],[24]])]
# 将原矩阵横向、纵向地复制。
import numpy as npx = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
#  [3 4]]y = np.tile(x, (1, 3))
print(y)
# [[1 2 1 2 1 2]
#  [3 4 3 4 3 4]]y = np.tile(x, (3, 1))
print(y)
# [[1 2]
#  [3 4]
#  [1 2]
#  [3 4]
#  [1 2]
#  [3 4]]y = np.tile(x, (3, 3))
print(y)
# [[1 2 1 2 1 2]
#  [3 4 3 4 3 4]
#  [1 2 1 2 1 2]
#  [3 4 3 4 3 4]
#  [1 2 1 2 1 2]
#  [3 4 3 4 3 4]]
[[1 2][3 4]]
[[1 2 1 2 1 2][3 4 3 4 3 4]]
[[1 2][3 4][1 2][3 4][1 2][3 4]]
[[1 2 1 2 1 2][3 4 3 4 3 4][1 2 1 2 1 2][3 4 3 4 3 4][1 2 1 2 1 2][3 4 3 4 3 4]]
# 重复数组的元素
import numpy as npx = np.repeat(3, 4)
print(x)  # [3 3 3 3]x = np.array([[1, 2], [3, 4]])
y = np.repeat(x, 2)
print(y)
# [1 1 2 2 3 3 4 4]y = np.repeat(x, 2, axis=0)
print(y)
# [[1 2]
#  [1 2]
#  [3 4]
#  [3 4]]y = np.repeat(x, 2, axis=1)
print(y)
# [[1 1 2 2]
#  [3 3 4 4]]y = np.repeat(x, [2, 3], axis=0)
print(y)
# [[1 2]
#  [1 2]
#  [3 4]
#  [3 4]
#  [3 4]]y = np.repeat(x, [2, 3], axis=1)
print(y)
# [[1 1 2 2 2]
#  [3 3 4 4 4]]
[3 3 3 3]
[1 1 2 2 3 3 4 4]
[[1 2][1 2][3 4][3 4]]
[[1 1 2 2][3 3 4 4]]
[[1 2][1 2][3 4][3 4][3 4]]
[[1 1 2 2 2][3 3 4 4 4]]
# 查找数组的唯一元素
a=np.array([1,1,2,3,3,4,4])
b=np.unique(a,return_counts=True)
print(b[0][list(b[1]).index(1)])
#2
2
# 将arr转换为2行的2维数组
import numpy as nparr = np.arange(10)# 方法1
x = np.reshape(arr, newshape=[2, 5])
print(x)
# [[0 1 2 3 4]
#  [5 6 7 8 9]]# 方法2
x = np.reshape(arr, newshape=[2, -1])
print(x)
# [[0 1 2 3 4]
#  [5 6 7 8 9]]
[[0 1 2 3 4][5 6 7 8 9]]
[[0 1 2 3 4][5 6 7 8 9]]
# 垂直堆叠数组a和数组b、
import numpy as npa = np.arange(10).reshape([2, -1])
b = np.repeat(1, 10).reshape([2, -1])print(a)
# [[0 1 2 3 4]
#  [5 6 7 8 9]]
print(b)
# [[1 1 1 1 1]
#  [1 1 1 1 1]]#方法一
print(np.concatenate([a, b], axis=0))# 方法二
print(np.vstack([a, b]))
[[0 1 2 3 4][5 6 7 8 9]]
[[1 1 1 1 1][1 1 1 1 1]]
[[0 1 2 3 4][5 6 7 8 9][1 1 1 1 1][1 1 1 1 1]]
[[0 1 2 3 4][5 6 7 8 9][1 1 1 1 1][1 1 1 1 1]]
# 将数组a和数组b水平堆叠
import numpy as npa = np.arange(10).reshape([2, -1])
b = np.repeat(1, 10).reshape([2, -1])print(a)
# [[0 1 2 3 4]
#  [5 6 7 8 9]]
print(b)
# [[1 1 1 1 1]
#  [1 1 1 1 1]]
#方法1
print(np.concatenate([a, b], axis=1))# 方法2
print(np.hstack([a, b]))
[[0 1 2 3 4][5 6 7 8 9]]
[[1 1 1 1 1][1 1 1 1 1]]
[[0 1 2 3 4 1 1 1 1 1][5 6 7 8 9 1 1 1 1 1]]
[[0 1 2 3 4 1 1 1 1 1][5 6 7 8 9 1 1 1 1 1]]
# 将arr的2维数组按列输出
import numpy as np
arr =  np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])
y = arr.flatten(order='F')
print(y)
[16 11 21 31 26 17 12 22 32 27 18 13 23 33 28 19 14 24 34 29 20 15 25 3530]
# 给定两个随机数组A和B,验证它们是否相等。
import numpy as np
A = np.array([1,2,3])
B = np.array([1,2,3])# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)# Checking both the shape and the element values, no tolerance (values have to be exactly equal)
equal = np.array_equal(A,B)
print(equal)
True
True
# 在给定的numpy数组中找到重复的条目(第二次出现以后),并将它们标记为True。第一次出现应为False。
import numpy as npnp.random.seed(100)
a = np.random.randint(0, 5, 10)
print(a)
# [0 0 3 0 2 4 2 2 2 2]
b = np.full(10, True)
print(b)
vals, counts = np.unique(a, return_index=True)
print(vals)
print(counts)
b[counts] = False
print(b)
# [False  True False  True False False  True  True  True  True]
[0 0 3 0 2 4 2 2 2 2]
[ True  True  True  True  True  True  True  True  True  True]
[0 2 3 4]
[0 4 2 5]
[False  True False  True False False  True  True  True  True]
# 建立一个随机数在1-10之间的3行2列的数组,并将其转换成2行3列的数组。
import numpy as np
x = np.random.randint(10, size=[3, 2])
print(x)
y = np.reshape(x, [2, 3])
print(y)
[[3 2][5 8][1 0]]
[[3 2 5][8 1 0]]


numpy_3数组的操作-变形相关推荐

  1. java数组如何pop_Js数组的操作push,pop,shift,unshift等方法详细介绍

    js中针对数组操作的方法还是比较多的,今天突然想到来总结一下,也算是温故而知新吧.不过不会针对每个方法进行讲解,我只是选择其中的一些来讲. 首先来讲一下push和pop方法,这两个方法只会对数组从尾部 ...

  2. C# 指定格式的字符串截成一维数组(二维数组)的操作类

    指定格式的字符串截成一维数组(二维数组)的操作类 做项目时经常会遇到将"1,3,a,b,d"截成一维数组或将"1,a;2,b;3,c;4,d"截成二维数组.虽然 ...

  3. JAVA-初步认识-第五章-数组-常见操作-最值

    一. 最值 数组的操作是对数组中的元素进行操作,有时需要对元素的位置重新进行安排,有时仅需要提供题目需要的那个数据. 在视频没有讲解怎么做之前,我先提供一个思路.得出最值肯定是一个一个地比较出来的,这 ...

  4. 【C 语言】字符串操作 ( 使用 数组下标 操作字符串 | 使用 char * 指针 操作字符串 )

    文章目录 一.使用 数组下标 或 指针 操作字符串 1.使用 数组下标 操作字符串 2.使用 char * 指针 操作字符串 二.代码示例 一.使用 数组下标 或 指针 操作字符串 1.使用 数组下标 ...

  5. Java数组常见操作

    Java数组常见操作 文章目录 Java数组常见操作 7.0 数组的遍历 1.使用foreach循环访问数组中每个元素. 2.使用简单的for循环(多层循环嵌套)来遍历数组. 7.1 数组长度 7.2 ...

  6. java web定义数组_Java基础之数组--数组常用操作

    3.2一维数组 3.2.1声明数组 数组类型[] 数组名称:int[] username; 或者 数组类型 数组名称[];int username[]; 3.2.2初始化一维数组 一维数组初始化有两种 ...

  7. 转 C#对多个集合和数组的操作(合并,去重,判断)

    在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数. 首先举例2个集合A,B. List<int> ...

  8. 快学Scala习题解答—第三章 数组相关操作

    原文链接:http://blog.csdn.net/ivan_pig/article/details/8257365 ----------------------------------------- ...

  9. 动态数组相关操作 0104 ArrayList

    动态数组相关操作 0104 引入命名空间 using System.Collections; 实例化一个动态数组 类名称:ArrayList 基本的实例化 ArrayList 数组名= new Arr ...

最新文章

  1. windows2008系统启动weblogic报错
  2. xcode 的一些问题
  3. 使用WebDriver + Java + Junit做自动化测试教程
  4. 云计算三层架构_金山云发布星曜裸金属服务器 打造面向云计算2.0的云基础架构...
  5. ROS多根adsl叠加负载均衡PCC的做法
  6. 【Android-tips】 Unable to execute dex: Multiple dex files define 解决方法
  7. 【Spring Boot 分享】开源项目【8个】
  8. c#把日期改成数字字符串_C#编写壹个函数将输入的中文日期转换为阿拉伯数字日期...
  9. 从零开始学习python编程-和尧名大叔一起从0开始学Python编程-循环
  10. MySQL显示连接的数据库名
  11. VS2015 编译Protobuf
  12. seata 使用oracle_使用Seata彻底解决Spring Cloud中的分布式事务问题!
  13. 人工智能搜索算法案例分析
  14. LTE中阻塞干扰,杂散干扰,邻信道干扰,交调干扰,加性噪声干扰分析
  15. 重大噩耗:苹果账号无法付款!(11-20更新:账单地址和卡地址一样,信用卡名字和开发者名字一致,都无法付款)
  16. blog推荐 - 左岸读书
  17. Java第十五篇:详解一元二次方程
  18. 测绘程序设计——基础篇(1)C#编写方位角计算程序篇1——用户界面的构造
  19. 大学计算机基础培训的课程
  20. linux php环境搭建教程,linux php环境搭建教程

热门文章

  1. 什么是极大似然估计?
  2. 密码学速查笔记(一)-- 分组对称加密
  3. 【免费赠送源码】Springboot篮球网站19133计算机毕业设计-课程设计-期末作业-毕设程序代做
  4. Linux安装卸载Mysql与基本操作
  5. 盘古开源:Web3.0浪潮来袭,离不开分布式存储赋能
  6. 阿里开源新一代 AI 算法模型,由达摩院90后科学家研发
  7. MATLAB(SimMechanics)机器人可视化运动仿真-关节位置控制篇
  8. 10.Report\1.SinglePinNetReport
  9. CSR867x — sink工程的编译与下载(run)
  10. bzoj1375 双调路径