本系列来自B站up主莫烦的视频 https://www.bilibili.com/video/BV1Ex411L7oT?p=7
import numpy as np
"""
新建array的五种方法:
1. np.array() 中直接输入一维或多维List
2. np.zeros()中输入想要的shape (3,4), 数据类型 dtype
3. np.empty() 输入 shape , dtype
4. np.arange().reshape()
5. np. linspace().reshape()
6. .reshape() 将元素重新组成矩阵
1.
a = np.array([[2,3,4],[1,2,3]],dtype=np.int )
[[2 3 4]
[1 2 3]]
2.
b = np.zeros((3,4),int)
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
3.
c = np.empty((3,4))
[[6.23042070e-307 4.67296746e-307 1.69121096e-306 1.60218491e-306]
[1.89146896e-307 1.37961302e-306 1.05699242e-307 8.01097889e-307]
[1.11261162e-306 1.69119330e-306 7.56599128e-307 1.42410974e-306]]
4.
d = np.arange(0,12,1).reshape(3,4)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
5.
e = np.linspace(1,12,12,dtype=int).reshape(3,4)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
"""
# dtype 参数来确定元素类型
a = np.array([[2,3,4],[1,2,3]],dtype=np.int )
# array 元素间没有逗号
print(a)
# 参数为 shape,dtype,order 如果直接打 (3,4) 则表示 shape为3,dtype为4
b = np.zeros((3,4),int)
print(b)
c = np.empty((3,4))
print(c)
# arange生成一个数列 用reshape 将单行数列变换成矩阵
d = np.arange(0,12,1)
print(d)
d = d.reshape((3,4))
print(d)
# arange第三个参数是步长, linspace第三个参数是分段数,两个数互补
# arrange 含头不含尾 linspace 含头含尾
e = np.linspace(1,12,12,dtype=int).reshape(3,4)
print(e)