Python笔记:详解使用内置函数创建ndarray

引入

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,其中包含从 0N - 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) 创建由满足特定统计学特性的随机数字组成的 ndarray

    X = 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(平均值)保持对称,正数和负数的数量很接近。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值