PYTHON-数组知识

1.shape

#1.shape
#一维数组
a = [1,2,3,4,5,6,7,8,9,10,11,12] 
b = np.array(a)

print(b.shape[0])#最外层有12个元素
#print(b.shape[1])#次外层,#IndexError: tuple index out of range

#为什么不直接 a.shape[0],因为 'list' object has no attribute 'shape'

#二维数组
a = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
b = np.array(a)
print(b)
print(b.shape[0],b.shape[1])#最外层3个,里边4个
#output:
12 [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] 3 4

2.reshape

#2.reshape
a = [1,2,3,4,5,6,7,8,9,10,11,12]
b = np.array(a).reshape(2,6) #2行6列
print(b)
print(a)
b = np.array(a).reshape(2,3,2) #2行3列的两个矩阵
print(b)
print(np.array(a))

#reshape新生成数组和原数组公用一个内存,不管改变哪个都会互相影响。
#输出:
[[ 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  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]
[ 1  2  3  4  5  6  7  8  9 10 11 12]
#3.reshape(-1,1) 解释为:-1行 == 没有行;1 == 1列,那么这个就是1个列向量
a = [1,2,3,4,5,6,7,8,9,10,11,12]
b = np.array(a).reshape(-1,1) #12 * 1
print(b)

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

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

a = [1,2,3,4,5,6,7,8,9,10,11,12]
b = np.array(a).reshape(2,-1) #2 *6
print(b)
#结果:
[[ 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  4  5  6  7  8  9 10 11 12]]
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]
>>> 

 

3.学这个的时候好像参考了那个网址,有些不记得了,如果有侵犯到您的著作权,立删!

 

posted @ 2020-04-18 20:32  落月_YU  阅读(223)  评论(0编辑  收藏  举报