numpy创建数组
目录
-
随机抓取未清零内存数组
import numpy as np
x = np.empty([3,12], dtype = int) #随机抓取内存,没有清零,
x
结果输出:
array([[ 94001510893200, 0, 140676899175152,
140676895625744, 140676922323568, 140676922367216,
140676915227696, 140676918246448, 140676895625904,
140676920069040, 140676896922608, 140676895625984],
[140676899165488, 140676896808112, 140676899172464,
140676899172592, 140676895615472, 140676895626064,
140676895626144, 140676895615536, 140676899173040,
140676895626224, 140676922529072, 140676922115184],
[140676920367408, 140676922465712, 140676922589616,
140676922402736, 140676921207728, 140676922464048,
140676895626464, 140676922325744, 140676895626544,
140676895626624, 140676922324400, 140676895615792]])
-
创建清零的实数
x=np.zeros(15) #清零的实数,15个
x
结果输出:
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.])
x=np.zeros( (15,),dtype=np.int) #清零的整,15个
x
结果输出:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
x=np.zeros((3,3),dtype=([("tall","f4"),("age","i4"),("money","f4")])) #自定义类型,创建数组,初始化
x
结果输出:
array([[( 0., 0, 0.), ( 0., 0, 0.), ( 0., 0, 0.)],
[( 0., 0, 0.), ( 0., 0, 0.), ( 0., 0, 0.)],
[( 0., 0, 0.), ( 0., 0, 0.), ( 0., 0, 0.)]],
dtype=[('tall', '<f4'), ('age', '<i4'), ('money', '<f4')])
x=np.ones(5)
x
结果输出:
array([ 1., 1., 1., 1., 1.])
x=np.ones([5,5],dtype=np.int)
x
结果输出:
x=np.ones([5,5],dtype=np.int)
x
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])