numpy创建数组(矩阵)的几种方法

1.1 调用numpy的array()函数。

  • 格式: array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,like=None)
  • 返回值: ndarray,满足具体要求的数组。
  • 参数说明:
参数 类型 说明
object array_like An array, any object exposing the array interface, an object whose__array__ method returns an array, or any (nested) sequence.
dtype data-type, optional The desired data-type for the array.
copy bool, optional If true (default), then the object is copied.
order {'K', 'A', 'C', 'F'}, optional 指定数组元素的存储顺序。C:行优先 F:列优先 K:元素在内存中出现的顺序 A:原顺序

array函数接受序列型对象,如列表,元组作为参数,返回一个类型为ndarry的数组。这是numpy的基础数据类型。与列表不同,ndarry必须包含同一数据类型,否则向上转换或报错。

numpy支持的数据类型如下:

使用array创建数组时,如果没有指定数据类型,将默认为浮点数类型。

import numpy as np
x=np.array([1,2,3,4,5,6])
print(type(x))
print(x)
#输出
#<class 'numpy.ndarray'>
#[1 2 3 4 5 6]

x=np.array([[1,2,3],[4,5,6]])
print(x)
print(x.shape,x.dtype,x.size,x.ndim)#形状,数据类型,元素个数,维度
'''
输出
[[1 2 3]
 [4 5 6]]
(2, 3) int32 6 2
'''
x=np.array([a for a in range(10)])#迭代器
print(x)
print(x.shape,x.dtype,x.size,x.ndim)
'''
输出
[0 1 2 3 4 5 6 7 8 9]
(10,) int32 10 1
'''

x=np.array([range(a,a+5) for a in range(5)])#嵌套列表
print(x)
print(x.shape,x.dtype,x.size,x.ndim)
'''
输出
[[0 1 2 3 4]
 [1 2 3 4 5]
 [2 3 4 5 6]
 [3 4 5 6 7]
 [4 5 6 7 8]]
(5, 5) int32 25 2
'''

1.2 调用numpy的arange(),linspace(),logspace()方法

  1. arange()
  • 格式: numpy.arange(start, stop, step, dtype)

与python的range函数类似,start和stop相当于一个左闭右开的区间。step为步长默认为1,dtype为数据类型。函数返回一个ndarry数组。
step 可以是浮点数、负数

  1. linspace()
  • 格式: np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

该函数也可以用来创建等差数列数组。与arange函数不同,linspace的num参数为创建的数组元素个数,步长为(stop-start)/(num-1).
同时start和stop相当于一个左闭右闭的区间。
将 endpoint 设为 false,结果不包含终止值。

x=np.arange(10)
print(x)
print(x.shape,x.dtype,x.ndim,x.size)
'''
输出
[0 1 2 3 4 5 6 7 8 9]
(10,) int32 1 10
'''

x=np.arange(1,11,2)
print(x)
print(x.shape,x.dtype,x.ndim,x.size)
'''
输出
[1 3 5 7 9]
(5,) int32 1 5
'''

a=np.linspace(0,5,6)
print(a)
'''
[0. 1. 2. 3. 4. 5.]
'''

a=np.linspace(2,25,5,False)
print(a)
'''
[ 2.   6.6 11.2 15.8 20.4]
'''
  1. numpy.logspace()

用于创建一个于等比数列。它返回在对数刻度上均匀间隔的数字构成的数组。

这是什么意思呢,例如先取一个基底base=2,然后指定一个范围[0,8]和元素个数9,返回一个以等差数列为指数的等比数列。

  • 格式: np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
  • 返回值: ndarray
import numpy as np    
print(np.logspace(0,9,11,base=2,endpoint=True))
print([2**x for x in np.linspace(0,9,11)])

#它们生成的数据是一样的

'''
[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]
[1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0]

'''

print(np.logspace(1,10,10))
print([10**x for x in np.linspace(1,10,10)])

'''
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]
[10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0]

'''
y=np.logspace(1,10,20)
print(y)
'''
[1.00000000e+01 2.97635144e+01 8.85866790e+01 2.63665090e+02
 7.84759970e+02 2.33572147e+03 6.95192796e+03 2.06913808e+04
 6.15848211e+04 1.83298071e+05 5.45559478e+05 1.62377674e+06
 4.83293024e+06 1.43844989e+07 4.28133240e+07 1.27427499e+08
 3.79269019e+08 1.12883789e+09 3.35981829e+09 1.00000000e+10]
'''

1.3 调用ones,zeros,full,empty方法

a=np.ones((3,4),dtype=int)
print(a)
'''
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
'''

a=np.zeros((3,4))
print(a)
'''
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
'''

a=np.full((3,4),4.2)
print(a)
'''
[[4.2 4.2 4.2 4.2]
 [4.2 4.2 4.2 4.2]
 [4.2 4.2 4.2 4.2]]
'''

a=np.full((2,2),9)
print(np.full_like(a,3))
'''
[[3 3]
 [3 3]]
ones_like,zeros_like类似。
'''

1.4 调用tile函数

tile意为展开,铺设。它接受两个参数,格式为tile(A,reps)。

  • 格式: tile(A, reps)
  • 返回值: ndarray,展开后的数组。
  • 参数说明:
    • A的数据类型:array_like,例如数组,列表,元组,字典,以及int,float,string等。

    • B的数据类型:array_like,例如数组,列表,元组,字典,int等。

A通常为需要进行操作的输入数组,B为A在每个轴上的重复次数。

函数的返回值为数组。

简单理解为将A视为一个元素,B为返回数组的形状(shape)。

实例:

a=np.tile(2,9)
print(a)
#[2 2 2 2 2 2 2 2 2]

a=np.tile((2,2),(2,2))#两个参数均为元组
print(a)
'''
[[2 2 2 2]
 [2 2 2 2]]
'''

a=[2,3,4]
b=np.tile(a,4)
print(b)
'''
[2 3 4 2 3 4 2 3 4 2 3 4]
'''

a=np.arange(4).reshape(2,2)
b=np.tile(a,[2,2])
print(b)
'''
[[0 1 0 1]
 [2 3 2 3]
 [0 1 0 1]
 [2 3 2 3]]
'''

1.5 调用diag()创建对角矩阵

x=np.diag([1,2,3,4])
print(x)
"""
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]
"""

1.6 由自定义函数创建

  1. np.fromfunction()
def fun(x,y):
    return 2*x+y
l=np.fromfunction(fun,(3,6))
#fun()接收的参数就是矩阵元素的坐标
print(l)
'''
[[0. 1. 2. 3. 4. 5.]
 [2. 3. 4. 5. 6. 7.]
 [4. 5. 6. 7. 8. 9.]]
'''
  1. np.fromiter()

从可迭代对象建立ndarray对象,返回一维数组。

l=np.fromiter((x**2 for x in range(0,6)),int)
print(l)

#[ 0  1  4  9 16 25]

1.7 随机数组创建

1. np.random.rand() np.random.random()

生成指定形状的0-1之间的随机数组。区别在于前者参数为多个整数,后者是一个序列。

a=np.random.random((3,2))
b=np.random.rand(2,3)
print(a,2*"\n",b)

# [[0.92121226 0.02247888]
#  [0.39044403 0.08281157]
#  [0.67822274 0.48432339]] 

#  [[0.58183831 0.47706858 0.08828412]
#  [0.28485837 0.53791803 0.1256546 ]]

2. np.random.randn()

标准正态分布

b=np.random.randn(2,3)
print(b)

# [[-0.58095833 -2.90148355  2.33022057]
#  [ 0.458806   -0.64570515 -1.1560223 ]]

3. np.random.normal

指定均值和方法的正态分布

  • 格式:normal(loc=0.0, scale=1.0, size=None)
    三个参数分别是:均值、方差、元素个数
a=np.random.normal(1,0.5,4)
# [1.57883425 0.95897684 0.8493202  1.14610196]

4. np.random.randit()

生成指定范围内的随机整数

  • 格式:randint(low, high=None, size=None, dtype=None)
  • 参数说明:最小值、最大值、返回数组形状、数据元素类型
a=np.random.randint(1,5,10)
# [1 2 3 3 3 4 2 3 1 4]

5. np.random.seed()

设置种子,相同种子调用random函数结果一致

6. np.random.shuffle()

打乱数组元素顺序
该函数没有返回值,直接对传入数组进行修改

x=[1,2,3,4,5,6]
np.random.shuffle(x)
print(x)
#[6, 4, 2, 5, 1, 3]

7. np.random.uniform()

选择指定区间的均匀分布随机数,区间左开右闭

  • 格式: uniform(low=0.0, high=1.0, size=None)
x=np.random.uniform(1,100,10)
print(x)
# [23.67407963 38.34110513 54.68831863  9.28520362 95.15048023 89.72708317
# 80.54592304 69.74768056 75.27314397 67.69583441]

8. np.random.choice()

按照指定概率从指定集合中生成随机数。

  • 格式: choice(a, size=None, replace=True, p=None)
  • 参数说明: 指定数集、数组形状、元素可否重复、数值选中概率(总和必须为1)
a=np.random.choice([1,2,3,4],size=10000,p=[0.1,0.5,0.2,0.2])
posted @ 2021-07-24 10:42  zeroy610  阅读(3658)  评论(1编辑  收藏  举报