Numpy 的常用功能
一、Numpy 的常用功能 Zeros
Zeros用于产生全0数组
语法结构:
numpy.zeros(shape, dtype = None, order = ‘C’) :
Return a new array of given shape and type, with zeros.
案例:
import numpy as np
# help(np.zeros) # 查看帮助
print(np.zeros(5)) # 创建1维0数组
print('*'*20)
print(np.zeros(5,dtype = int)) # 设置数据类型
print('*'*20)
print(np.zeros((2,3))) # 创建2维0数组
二、Numpy 的常用功能 Ones
Ones用于产生全1数组
语法结构:
numpy.ones(shape, dtype = None, order = ‘C’) :
Return a new array of given shape and type, with ones.
案例:
import numpy as np
# help(np.zeros) # 查看帮助
b = np.ones(2)
print(b) # 创建1维1数组
print('*'*20)
print(np.ones(2,dtype = int)) # 设置数据类型
print('*'*20)
print(np.ones((2,3))) # 创建2维1数组 (2,3)/[2,3] 圆括号和方括号均可
print(b.ndim,b.itemsize,b.dtype)
三、Numpy 的常用功能 empty
empty用于产生空数组(零或不完全随机数)
语法结构:
numpy.empty(shape, dtype = float, order = ‘C’) :
Return a new array of given shape and type, with random values.
案例:
import numpy as np
# help(np.zeros) # 查看帮助
b = np.empty(2)
print(b) # 创建1维空数组
print('*'*20)
print(np.empty(2,dtype = int)) # 设置数据类型
print('*'*20)
print(np.empty((2,3))) # 创建2维空数组 (2,3)/[2,3] 圆括号和方括号均可
print(b.ndim,b.itemsize,b.dtype)