引入
import numpy as np
在此引入一次,下面直接使用 np
使用 np.zeros(shape) 创建
X = np.zeros((3,4))
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
X has dimensions: (3, 4)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: float64
np.zeros(shape)会创建一个全是 0 并且为给定形状的 ndarray- 同
np.array()一样,内置函数也可以创建指定类型的 ndarray, 参考前文
使用 np.ones(shape) 创建
X = np.ones((3,2), dtype = np.int64) # 此处指定类型
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[1 1]
[1 1]
[1 1]]
X has dimensions: (3, 2)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int64
- 此处我们创建了一个具有指定形状的 ndarray,其中的元素全是 1
使用 np.full(shape, constant value) 创建
X = np.full((2,3), 5)
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[5 5 5]
[5 5 5]]
X has dimensions: (2, 3)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: int64
- 第一个参数是你要创建的 ndarray 的形状,第二个参数是你要向数组中填充的常数值。
- 常数值这里如果你填写5.0,那么类型自动变为
float64类型 - 当然,还可再加第三个参数,比如:
dtype = np.int64, 来更改数据类型
使用 np.eye(N) 创建 单位矩阵
X = np.eye(5)
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
X has dimensions: (5, 5)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: float64
- 单位矩阵是主对角线上全是 1,其他位置全是 0 的方形矩阵。
- 线性代数中的基本数组是单位矩阵。
使用 np.diag() 创建 对角矩阵
X = np.diag([10,20,30,50])
print()
print('X = \n', X)
print()
输出为:
X =
[[10 0 0 0]
[ 0 20 0 0]
[ 0 0 30 0]
[ 0 0 0 50]]
使用 np.arange(start,stop,step) 创建在给定区间内值均匀分布的 ndarray
x = np.arange(1,14,3)
print()
print('x = ', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)
输出:
x = [ 1 4 7 10 13]
x has dimensions: (5,)
x is an object of type: class 'numpy.ndarray'
The elements in x are of type: int64
- 可以传入一个参数、两个参数或三个参数。
- 如果只传入一个参数,
np.arange(N)将创建一个秩为 1 的 ndarray,其中包含从0到N - 1的连续整数。 - 注意,如果我希望数组具有介于
0 到 9之间的整数,则需要将N设为10,而不是将N设为9。 - 均匀分布的数字将包括
start数字,但是不包括stop数字。 step表示两个相邻值之间的差。允许间隔为非整数,例如 0.3,但是由于浮点数精度有限,输出通常不一致。- 因此,如果需要非整数间隔,通常建议使用函数
np.linspace(start, stop, N)。
使用 np.linspace(start,stop,step) 创建在给定区间内值均匀分布的 ndarray
x = np.linspace(0,25,10)
print()
print('x = \n', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)
输出:
x =
[ 0. 2.77777778 5.55555556 8.33333333 11.11111111 13.88888889
16.66666667 19.44444444 22.22222222 25. ]
x has dimensions: (10,)
x is an object of type: <class 'numpy.ndarray'>
The elements in x are of type: float64
np.linspace(start, stop, N)函数返回N个在闭区间[start, stop]内均匀分布的数字, 即 start 和 stop 值都包括在内。- 也可以不包含区间的结束点(就像
np.arange()函数一样),方法是在np.linspace()函数中将关键字endpoint 设为 False。
x = np.linspace(0,25,10, endpoint = False)
使用 np.reshape(ndarray, new_shape) 创建秩为 2 的任何形状 ndarray
x = np.arange(20)
print()
print('Original x = ', x)
print()
x = np.reshape(x, (4,5))
print()
print('Reshaped x = \n', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)
输出:
Original x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Reshaped x =
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
x has dimensions: (4, 5)
x is an object of type: class 'numpy.ndarray'
The elements in x are of type: int64
- 我们将
np.arange()和np.linspace()这些函数与np.reshape()函数相结合,创建秩为 2 的任何形状 ndarray。 np.reshape(ndarray, new_shape)函数会将给定 ndarray 转换为指定的 new_shape。注意:new_shape 应该与给定 ndarray 中的元素数量保持一致。- 某些函数还可以当做方法使用, 比如我们用一行代码实现上述示例中的相同结果:
Y = np.arange(20).reshape(4, 5) - 注意,new_shape 应该与 ndarray 中的元素数量保持一致。
同样,我们也可以使用 reshape() 与 np.linspace() 创建秩为 2 的数组。
X = np.linspace(0,50,10, endpoint=False).reshape(5,2)
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[ 0. 5.]
[ 10. 15.]
[ 20. 25.]
[ 30. 35.]
[ 40. 45.]]
X has dimensions: (5, 2)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64
创建随机 ndarray
-
使用
np.random.random(shape)函数创建具有给定形状的 ndarray, 包含位于半开区间 [0.0, 1.0) 内的随机浮点数。X = np.random.random((3,3)) print() print('X = \n', X) print() print('X has dimensions:', X.shape) print('X is an object of type:', type(X)) print('The elements in x are of type:', X.dtype)输出:
X = [[ 0.12379926 0.52943854 0.3443525 ] [ 0.11169547 0.82123909 0.52864397] [ 0.58244133 0.21980803 0.69026858]] X has dimensions: (3, 3) X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64 -
使用
np.random.randint(start, stop, size = shape)创建由特定区间内的随机整数构成的 ndarray, 其中包含在半开区间 [start, stop) 内的随机整数。
X = np.random.randint(4,15,size=(3,2))
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
输出:
X =
[[ 7 11]
[ 9 11]
[ 6 7]]
X has dimensions: (3, 2)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: int64
-
使用
np.random.normal(mean, standard deviation, size=shape)创建由满足特定统计学特性的随机数字组成的 ndarrayX = np.random.normal(0, 0.1, size=(1000,1000)) print() print('X = \n', X) print() print('X has dimensions:', X.shape) print('X is an object of type:', type(X)) print('The elements in X are of type:', X.dtype) print('The elements in X have a mean of:', X.mean()) print('The maximum value in X is:', X.max()) print('The minimum value in X is:', X.min()) print('X has', (X < 0).sum(), 'negative numbers') print('X has', (X > 0).sum(), 'positive numbers')输出:
X = [[ 0.04218614 0.03247225 -0.02936003 ..., 0.01586796 -0.05599115 -0.03630946] [ 0.13879995 -0.01583122 -0.16599967 ..., 0.01859617 -0.08241612 0.09684025] [ 0.14422252 -0.11635985 -0.04550231 ..., -0.09748604 -0.09350044 0.02514799] ..., [-0.10472516 -0.04643974 0.08856722 ..., -0.02096011 -0.02946155 0.12930844] [-0.26596955 0.0829783 0.11032549 ..., -0.14492074 -0.00113646 -0.03566034] [-0.12044482 0.20355356 0.13637195 ..., 0.06047196 -0.04170031 -0.04957684]] X has dimensions: (1000, 1000) X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64 The elements in X have a mean of: -0.000121576684405 The maximum value in X is: 0.476673923106 The minimum value in X is: -0.499114224706 X 具有 500562 个负数 X 具有 499438 个正数说明:
- 创建了包含从正态高斯分布(具有给定均值和标准差)中抽样的随机数字。即:一个 1,000 x 1,000 ndarray,其中包含从正态分布(均值为 0,标准差为 0.1)中随机抽样的浮点数。
- 可以看出,ndarray 中的随机数字的平均值接近 0,X 中的最大值和最小值与 0(平均值)保持对称,正数和负数的数量很接近。
本文介绍使用Python的NumPy库创建不同类型的数组方法,包括全零数组、全一数组、常数填充数组、单位矩阵、对角矩阵等,并探讨如何通过arange、linspace函数及reshape方法创建指定形状的数组,最后还介绍了创建随机数组的方法。

358

被折叠的 条评论
为什么被折叠?



