numpy科学计算器库入门2 创建array

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zy_505775013/article/details/88669191
import numpy as np 
a = np.array([1,3,2],dtype=np.int32)#创建一维数组类型为int32
print(a.dtype)

int32

b = np.array([1,3,2],dtype=np.float)
print(b.dtype)

float64

zero = np.zeros((2,3))#生成2行3列的全零矩阵
print(zero)

[[0. 0. 0.]
 [0. 0. 0.]]

one = np.ones((3,4))#生成3行4列的全1矩阵
print(one)

[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

empty = np.empty((3,2))#生成3行2列的接近于零但不是零的矩阵
print(empty)

[[0. 0.]
 [0. 0.]
 [0. 0.]]

e = np.arange(10)#生成0到9的一维矩阵
print(e)

[0 1 2 3 4 5 6 7 8 9]

f = np.arange(4,12)#生成4到11的一维矩阵
print(f)

[ 4  5  6  7  8  9 10 11]

g = np.arange(1,20,3)#生成1到19并且没两个数之间的间隔是3的一维数组
print(g)

[ 1  4  7 10 13 16 19]

h = np.arange(8).reshape(2,4)#将一维0-7转换成2行4列的二维数组
print(h)

[[0 1 2 3]
 [4 5 6 7]]

猜你喜欢

转载自blog.csdn.net/zy_505775013/article/details/88669191