import numpy as np
1 ones
ones(shape[, dtype, order])
依据给定形状和类型(shape[, dtype, order])返回一个新的元素全部为1的数组。
参数:
shape:int或者ints元组;定义返回数组的形状,形如:(2, 3)或2。
dtype:数据类型,可选。
返回数组的数据类型,例如:numpy.int8、默认numpy.float64。
order:{‘C’, ‘F’},可选,返回数组为多维时,元素在内存的排列方式是按C语言还是Fortran语言顺序(row- or columnwise)。
输出:ndarray给定形状,数据类型的数组。
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[ 1.],
[ 1.]])
>>> s = (2,2)
>>> np.ones(s)
array([[ 1., 1.],
[ 1., 1.]])
>>>a=np.array([[1,2,3],[4,5,6]])
>>>np.ones_like(a)
[[1 1 1]
[1 1 1]]
2 zeros
zeros(shape[, dtype, order])
参数:
shape:int或者ints元组;定义返回数组的形状,形如:(2, 3)或2。
dtype:数据类型,可选。
返回数组的数据类型,例如:numpy.int8、默认numpy.float64。
order:{‘C’, ‘F’},可选,返回数组为多维时,元素在内存的排列方式是按C语言还是Fortran语言顺序(row- or columnwise)。
输出:ndarray给定形状,数据类型的数组。
与1 ones用法大致相同。
3 eye
eye(N[, M, k, dtype])
返回一个对角线元素为1,其他元素为0的二维数组。
参数:
N : 整数返回数组的行数;
M : 整数,可选返回数组的列数。如果不赋值的话,默认等于N;
k : 整数, 可选对角线序列号: 0 对应主对角线;,整数对应upper diagonal,负数对应lower diagonal;
dtype : dtype, 可选,返回数组的数据类型
I : ndarray (N,M)该数组第k个对角线的元素为1,其他元素为0。
np.eye(2,dtype=int)生成的array=
[[1 0]
[0 1]]
np.eye(3,k=1)生成的array=
[[ 0. 1. 0.]
[ 0. 0. 1.]
[ 0. 0. 0.]]
4 size
size():计算数组和矩阵所有数据的个数
a = np.array([[1,2,3],[4,5,6]])
np.size(a),返回值为 6
np.size(a,1),返回值为 3
5 shape
shape ():得到矩阵每维的大小
np. shape (a),返回值为 (2,3)
另外要注意的是,shape和size既可以作为函数,也可以作为ndarray的属性
a.size,返回值为 6,
a.shape,返回值为 (2,3)
5.1 np.shape()形式
import numpy as np
a = [[1, 2, 3, 8],
[4, 5, 6, 9]]
b = [[1, 2, 3, 8],
[4, 5, 6, 9]]
c = [[1, 2, 3, 8],
[4, 5, 6, 9]]
print("a=", a)
print(np.shape(a))
print("b=", b)
print(np.shape(b))
print("c=", c)
print(np.shape(c))
print("增加一维,新维度的下标为0")
d = np.stack((a, b, c), axis=0)
print(d)
print(np.shape(d))
print("增加一维,新维度的下标为1")
d = np.stack((a, b, c), axis=1)
print(d)
print(np.shape(d))
print("增加一维,新维度的下标为2")
d = np.stack((a, b, c), axis=2)
print(d)
print(np.shape(d))
输出:
a= [[1, 2, 3, 8], [4, 5, 6, 9]]
(2, 4)
b= [[1, 2, 3, 8], [4, 5, 6, 9]]
(2, 4)
c= [[1, 2, 3, 8], [4, 5, 6, 9]]
(2, 4)
增加一维,新维度的下标为0
[[[1 2 3 8]
[4 5 6 9]]
[[1 2 3 8]
[4 5 6 9]]
[[1 2 3 8]
[4 5 6 9]]]
(3, 2, 4)
增加一维,新维度的下标为1
[[[1 2 3 8]
[1 2 3 8]
[1 2 3 8]]
[[4 5 6 9]
[4 5 6 9]
[4 5 6 9]]]
(2, 3, 4)
增加一维,新维度的下标为2
[[[1 1 1]
[2 2 2]
[3 3 3]
[8 8 8]]
[[4 4 4]
[5 5 5]
[6 6 6]
[9 9 9]]]
(2, 4, 3)
5.2 array.shape
c = np.array([[[1,1],[1,2]],[[1,3],[1,4]],[[2,2],[3,3]]])
print(c)
print(c.shape)
print(c.shape[0],c.shape[1],c.shape[2],sep=';')
print(type(c))
结果:
[[[1 1]
[1 2]]
[[1 3]
[1 4]]
[[2 2]
[3 3]]]
(3, 2, 2)
3;2;2
numpy.ndarray
6 empty
empty(shape[, dtype, order])
依据给定形状和类型(shape[, dtype, order])返回一个新的空数组。
参数:
shape : 整数或者整型元组定义返回数组的形状;
dtype : 数据类型,可选定义返回数组的类型。
order : {‘C’, ‘F’}, 可选规定返回数组元素在内存的存储顺序:C(C语言)-rowmajor;F(Fortran)column-major。
7 empty_like(a)
依据给定数组(a)的形状和类型返回一个新的空数组
8 reshape()
numpy.
reshape
(a, newshape, order='C')
reshape()是数组对象中的方法,用于在不改变数据内容的情况下,改变数组的形状。
参数:
a : 数组——需要处理的数据。
newshape : 新的格式——整数或整数数组,如(2,3)表示2行3列。新的形状应该与原来的形状兼容,即行数和列数相乘后等于a中元素的数量。如果是整数,则结果将是长度的一维数组,所以这个整数必须等于a中元素数量。若这里是一个整数数组,那么其中一个数据可以为-1。在这种情况下,这个个值python会自动从根据第二个数值和剩余维度推断出来。
order : 可选范围——{‘C’, ‘F’, ‘A’}。使用索引顺序读取a的元素,并按照索引顺序将元素放到变换后的的数组中。“C”指的是用类C写的读/索引顺序的元素,横着读,横着写,优先读/写一行。“F”是指用FORTRAN类索引顺序读/写元素,竖着读,竖着写,优先读/写一列。注意,“C”和“F”选项不考虑底层数组的内存布局,只引用索引的顺序。“A”竖着读,横着写。这里可能听起来有点模糊,下面会给出示例。如果不进行order参数的设置,默认为C。
import numpy as np
from numpy import random as nr
r=nr.randint(0,10,size=(4,3))
r1=r
r2=r
r3=r
r1=r1.reshape((3,4),order='F')
r2=r2.reshape((3,4),order='A')
r3=r3.reshape((3,4),order='C')
print(r)
print(r1)
print(r2)
print(r3)
结果:
>>>r
array([[1, 6, 2],
[6, 1, 9],
[6, 3, 2],
[9, 1, 5]])
>>>r1
array([[1, 9, 3, 9],
[6, 6, 1, 2],
[6, 1, 2, 5]])
>>>r2
array([[1, 6, 2, 6],
[1, 9, 6, 3],
[2, 9, 1, 5]])
>>>r3
array([[1, 6, 2, 6],
[1, 9, 6, 3],
[2, 9, 1, 5]])
r=r1
r1=r.reshape((-1,1),order='C')
r2=r.reshape((-1,1),order='F')
r3=r.reshape((-1,1),order='A')
print(r1)
print(r2)
print(r3)
结果:
>>>r1
array([[1],
[9],
[3],
[9],
[6],
[6],
[1],
[2],
[6],
[1],
[2],
[5]])
>>>r2
array([[1],
[6],
[6],
[9],
[6],
[1],
[3],
[1],
[2],
[9],
[2],
[5]])
>>>r3
array([[1],
[6],
[6],
[9],
[6],
[1],
[3],
[1],
[2],
[9],
[2],
[5]])
9 arrange()函数
arange([start,] stop[, step,], dtype=None)
函数说明:根据start与stop指定的范围以及step设定的步长,生成一个 ndarray。 dtype : dtype
与range()对比:
-
range()
返回的是range object
,而np.nrange()
返回的是numpy.ndarray()
-
range
尽可用于迭代,而np.nrange
作用远不止于此,它是一个序列,可被当做向量使用。 -
range()
不支持步长为小数,np.arange()
支持步长为小数 -
两者都可用于迭代
-
两者都有三个参数,以第一个参数为起点,第三个参数为步长,截止到第二个参数之前的不包括第二个参数的数据序列
某种意义上,和STL中由迭代器组成的区间是一样的,即左闭右开的区间。[first, last)
或者不加严谨地写作[first:step:last)
>>> np.arange(1, 5)
array([1, 2, 3, 4])
>>>np.arange(1, 5, .5)
array([ 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
>>for i in np.arange(1, 5):
... print(i)
1
2
3
4
10 stack(),hstack(),vstack()
10.1 stack()
主要的参数有两个,一个是arrays,也就是用来作为堆叠的数组,要求形状维度必须相等,第二个参数为axis也就是指定依照哪个维度进行堆叠。
通过axis=0或1,对value进行堆积。
(1)当axis=0时
import numpy as np
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
d = np.stack((a, b),axis=0)
print(d)
print(d[1, 1])
print(np.shape(d))
输出:
[[1 2 3 4]
[5 6 7 8]]
6
(2, 4)
(2)当axis=1时:
import numpy as np
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
d = np.stack((a, b),axis=1)
print(d)
print(d[1, 1])
print(np.shape(d))
输出:
[[1 5]
[2 6]
[3 7]
[4 8]]
6
(4, 2)
numpy中list的查找格式为list_one[row, column];如果写成list[row, :]代表取row那一行输出:d ==> [[1, 5],[2, 6],[3, 7],[4, 8]]
d[1, 1] ==> 6
python中list的查找格式为list[row][column]
示例(1):
>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
# 利用列表生成式生成一个10*3*4的列表
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)
# 原始的维度为3*4,共有10个,采取了axis=0,也就是为原数组新增一个维度,这个维度在第0位,
# 由于一共有10个3*4数组,那么堆叠后的第一位的值便是10,所以堆叠后的维度为10*3*4
>>> np.stack(arrays, axis=1).shape
(3, 10, 4)
# 如上面解释,在axis-1的位置插入维度,由于一共10个数组,所以堆叠后的形状为3*10*4
>>> np.stack(arrays, axis=2).shape
(3, 4, 10)
# 还是想上面的解释,在最后一位增加维度,堆叠后的形状是3*4*10
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b)) #默认axis=0
array([[1, 2, 3],
[2, 3, 4]])
#第一维度增加,也就是1*3变为了2*1*3
>>> np.stack((a, b), axis=-1)
array([[1, 2],
[2, 3],
[3, 4]])
# 根据前面介绍的,axis=-1也就是在最后一维后增加一维,原始为1*3,堆叠后的维度就是1*3*2,由
# 于这里的a和b都只是一维数组,所以axis=1和axis=-1是相同的效果
a = np.array([[1, 2, 3],[2,3,4]])
b = np.array([[2, 3, 4],[4,5,6]])
np.stack((a, b))
array([[[1, 2, 3],
[2, 3, 4]],
[[2, 3, 4],
[4, 5, 6]]])
np.stack((a, b), axis=1)
array([[[1, 2, 3],
[2, 3, 4]],
[[2, 3, 4],
[4, 5, 6]]])
np.stack((a, b), axis=2)
array([[[1, 2],
[2, 3],
[3, 4]],
[[2, 4],
[3, 5],
[4, 6]]])
# 最后一个比较难理解一点,但是想到是根据多个数组进行堆叠,并且每个数组是2*3,输出的形状是
# 2*3*2,便可以想到如上的结果,这个不是特别好描述,需要自己好好理解一下,参考样例应该会比较好了解。
示例(2):
a = np.arange(1, 10).reshape((3, 3))
b = np.arange(11, 20).reshape((3, 3))
c = np.arange(101, 110).reshape((3, 3))
np.stack((a,b,c),axis=0)
# axis=0可以认为只是将原数组上下堆叠,增加了0维的个数
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[ 11, 12, 13],
[ 14, 15, 16],
[ 17, 18, 19]],
[[101, 102, 103],
[104, 105, 106],
[107, 108, 109]]])
np.stack((a,b,c),axis=1)
#axis=1,可以看出第一个3*3的数组是由是a,b,c中每个数组的第一行堆叠而成
array([[[ 1, 2, 3],
[ 11, 12, 13],
[101, 102, 103]],
[[ 4, 5, 6],
[ 14, 15, 16],
[104, 105, 106]],
[[ 7, 8, 9],
[ 17, 18, 19],
[107, 108, 109]]])
np.stack((a,b,c),axis=2)
#axis=2,可以看到第一个3*3的数组是由a,b,c中的第一列向量组合而成
array([[[ 1, 11, 101],
[ 2, 12, 102],
[ 3, 13, 103]],
[[ 4, 14, 104],
[ 5, 15, 105],
[ 6, 16, 106]],
[[ 7, 17, 107],
[ 8, 18, 108],
[ 9, 19, 109]]])
10.2 hstack(value)
value必须具有相同的数据结构,数据类型不限,可以是python的列表或者元祖,或者是numpy列表,hstack会将多个value(value_one, value_two)的相同维度的数值组合在一起,并以同value同样的数据结构返回numpy数组。
它其实就是水平(按列顺序)把数组给堆叠起来,vstack()函数正好和它相反。
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.hstack((a,b)))
输出:[1 2 3 4 5 6 ]
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.shape(a))
print(np.hstack((a,b,c,d)))
输出:
(3L, 1L)
[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]]
10.3 vstack()函数
函数原型:vstack(tup) ,参数tup可以是元组,列表,或者numpy数组,返回结果为numpy的数组。看下面的代码体会它的含义。
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.vstack((a,b)))
输出:
[[1 2 3]
[4 5 6]]
import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.vstack((a,b,c,d)))
输出:
[[1]
[2]
[3]
[1]
[2]
[3]
[1]
[2]
[3]
[1]
[2]
[3]]
它是垂直(按照行顺序)的把数组给堆叠起来。
11 矩阵乘法 np.dot, np.multiply, *
11.1 np.dot
同线性代数中矩阵乘法的定义,np.dot(A, B):对于二维矩阵,计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。对于一维矩阵,计算两者的内积。见如下Python代码:
import numpy as np
# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
# 2-D array: 3 x 2
two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]])
two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)
print('two_multi_res: %s' %(two_multi_res))
# 1-D array
one_dim_vec_one = np.array([1, 2, 3])
one_dim_vec_two = np.array([4, 5, 6])
one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two)
print('one_result_res: %s' %(one_result_res))
结果:
two_multi_res: [[22 28]
[49 64]]
one_result_res: 32
11.2 np.multiply(), 或 *
在Python中,实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。见如下Python代码:
import numpy as np
# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
another_two_dim_matrix_one = np.array([[7, 8, 9], [4, 7, 1]])
# 对应元素相乘 element-wise product
element_wise = two_dim_matrix_one * another_two_dim_matrix_one
print('element wise product: %s' %(element_wise))
# 对应元素相乘 element-wise product
element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)
print('element wise product: %s' % (element_wise_2))
结果如下:
element wise product: [[ 7 16 27]
[16 35 6]]
element wise product: [[ 7 16 27]
[16 35 6]]
12 transpose
12.1 二维
对于二维 ndarray,transpose在不指定参数是默认是矩阵转置。
x = np.arange(4).reshape((2,2))
import numpy as np
x.transpose()
结果:
#x 为:
array([[0, 1],
[2, 3]])
array([[0, 2],
[1, 3]])
如果指定参数,有如下相应结果:
x.transpose((0,1))
# x 没有变化
array([[0, 1],
[2, 3]])
x.transpose((1,0))
# x 转置了
array([[0, 2],
[1, 3]])
对于x,因为:
x[0][0] == 0
x[0][1] == 1
x[1][0] == 2
x[1][1] == 3
不妨设第一个方括号“[]”为 0轴 ,第二个方括号为 1轴 ,则x可在 0-1坐标系 下表示如下:
因为 x.transpose((0,1)) 表示按照原坐标轴改变序列,也就是保持不变
而 x.transpose((1,0)) 表示交换 ‘0轴’ 和 ‘1轴’,所以就得到如下图所示结果:
注意,任何时候你都要保持清醒,告诉自己第一个方括号“[]”为 0轴 ,第二个方括号为 1轴
此时,transpose转换关系就清晰了。
12.2 三维
import numpy as np
# A是array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
A = np.arange(16)
# 将A变换为三维矩阵
A = A.reshape(2,2,4)
print(A)
结果:
A = array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
对上述的A表示成如下三维坐标的形式:
A.transpose((0,1,2)) #保持A不变
A.transpose((1,0,2)) #将 0轴 和 1轴 交换
结果:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])
13 单目运算函数
np.max: 返回最大值
np.argmax: 返回最大的值的序号
np.min: 返回最小值
np.argmin: 返回最小的值的序号
np.absolute: 计算绝对值,np.absolute(a) 或者 np.abs(a),对于非复数的数组,np.fabs 速度更快
np.exp: 计算 e 的指数
np.sqrt: 计算平方根
np.square: 计算平方
np.log, np.log10, np.log2, np.log1p: 分别为以 e, 10, 2 为底取 log, 和 log(1 + x)
np.sign: 取数值的正负号
np.ceil: 计算比每一个元素大或相等的最小的整数
np.floor: 计算比每一个元素小或相等的最大的整数
np.rint: 近似到最近的整数
np.clip(a, a_min, a_max, out=None): 返回一个 ndarray, 其元素的值如果小于min,就会被min值替代;如果大于max,就会被max值替代
np.modf: 返回一个 tuple, 包含两个数组, 一个是小数部分, 一个是整数部分
np.cos, np.cosh, np.sin, np.sinh, np.tan, np.tanh, np.arccos, np.arccosh, np.arcsin, np.arcsinh, np.arctan, np.arctanh: 三角函数和反三角函数
14 any()和all()
any(iterables)
和all(iterables)
对于检查两个对象相等时非常实用,但是要注意,any
和all
是python内置函数,同时numpy也有自己实现的any
和all
,功能与python内置的一样,只不过把numpy.ndarray
类型加进去了。因为python内置的对高于1维的ndarray
没法理解,所以numpy基于的计算最好用numpy自己实现的any
和all
。
14.1 any()
本质上讲,any()
实现了或(OR)运算,而all()
实现了与(AND)运算。
对于any(iterables),如果可迭代对象iterables中任意存在每一个元素为True
则返回True
。特例:若可迭代对象为空,比如空列表[]
,则返回False
。
14.2 all
对于all(iterables),如果可迭代对象iterables中所有元素都为True
则返回True
。特例:若可迭代对象为空,比如空列表[]
,则返回True
。
15 常用随机数random
15.1 np.random.uniform的用法
np.random.uniform(low=0.0, high=1.0, size=None)
- 作用:可以生成[low,high)中的随机数,可以是单个值,也可以是一维数组,也可以是多维数组
参数介绍:
- low :float型,或者是数组类型的,默认为0
- high:float型,或者是数组类型的,默认为1
- size:int型,或元组,默认为空
>>>np.random.uniform(1,5,(4,3))
array([[2.66697332, 3.72157769, 2.01692529],
[2.25870204, 3.11920493, 2.11933516],
[2.46373283, 3.8026553 , 3.78496208],
[2.07367062, 3.68166636, 1.97301102]])
15.2 np.random.random_sample、np.random.random
np.random.random_sample的用法 和np.random.random作用一样
random_sample(size=None)
- 作用:返回[0,1)之间的浮点型随机数,通过size控制返回的形状。没有输入时,则返回[0,1)内的一个随机值
>>>np.random.random_sample()
0.47108547995356098
>>>type(np.random.random_sample())
<type 'float'>
>>>np.random.random_sample((5,))
array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
#Three-by-two array of random numbers from [-5, 0):
5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])
- numpy.random.random_sample(size=None)
- numpy.random.ranf(size=None)
- numpy.random.sample(size=None)
上面三个函数同样具有类似的功能。
15.3 np.random.rand的用法
rand(d0, d1, …, dn)
- 作用:返回[0,1)内的浮点数,包含0,不包含1。
- 输入的d0,d1…dn代表维度信息,没有输入时,则返回[0,1)内的一个随机值
>>>np.random.rand()
0.9062297570740588
>>>np.random.rand(2,3,4)
array([[[0.2460765 , 0.89243816, 0.5160871 , 0.21932333],
[0.10939967, 0.50039809, 0.78810344, 0.60480316],
[0.86577114, 0.72845622, 0.661555 , 0.33048161]],
[[0.18981253, 0.6513151 , 0.03391931, 0.26622332],
[0.61785346, 0.08223993, 0.96677646, 0.80869126],
[0.74527198, 0.18172208, 0.21071073, 0.67069078]]])
15.4 np.random.randint的用法
randint(low, high=None, size=None, dtype=’l’)
- 作用:生成整型随机数,可以是单个随机数,也可以是多维的随机数构成的数组
参数介绍
- low:int 型,随机数的下限。(hign = None时,生成的数值要在[0, low)区间内)
- high:int 型 (可选),默认为空,随机数的上限,当此值为空时,函数生成[0,low)区间内的随机数。要求low < high。
- size:int、或ints、或元组,指明生成的随机数的类型。输出随机数的尺寸,比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。
- dtype:可选’int’ ,’int32’,默认为’l’
>>>np.random.randint(4,10,size=(2,2),dtype='int32')
array([[4, 4],
[8, 9]])
注:random.randint()和np.random.randint()的区别:
- random.randint()方法里面的取值区间是前闭后闭区间;而np.random.randint()方法的取值区间是前闭后开区间。
- print(random.randint(20, 20)) #结果永远是20;print(np.random.randint(20,20))会报错,要求low < high。
15.5 np.random.random_integers的用法
random_integers(low, high=None, size=None) 和randint的用法较为相似,区别在于[low,high] 的右边界能够取到,且该函数即将被抛弃,可以使用 np.random.randint(low,high+1)进行代替
>>>np.random.random_integers(3)
1
总结:随机数可以分为两大类,一类是浮点型的,常以np.random.uniform为代表,np.random.rand,np.random.radnom和np.random.random_simple可以看作是np.random.uniform的特例;另一类是整数型的,以np.random.randint为代表,也有np.random.random_integers, 但是后者将被前者取代.
15.6 np.random.normal() 正态分布
高斯分布的概率密度函数
numpy.random.normal(loc=0.0, scale=1.0, size=None)
参数的意义为:
- loc:float,概率分布的均值,对应着整个分布的中心center
- scale:float,概率分布的标准差,对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高
- size:int or tuple of ints, 输出的shape,默认为None,只输出一个值
我们更经常会用到np.random.randn(size)所谓标准正太分布(μ=0, σ=1),对应于np.random.normal(loc=0, scale=1, size)
15.7 numpy.random.randn()
numpy.random.randn(d0,d1,…,dn)
- randn函数返回一个或一组样本,具有标准正态分布。
- dn表格每个维度
- 返回值为指定维度的array
np.random.randn() # 当没有参数时,返回单个数据
# 0.4959488482546297
np.random.randn(2,4)
# array([[ 1.05183215, 1.4379411 , -1.91659247, -0.75401455],
[ 1.23900958, -0.6951091 , 0.91014532, 1.61389596]])
15.8 np.random.choice()
numpy.random.choice(a, size=None, replace=True, p=None)
- 从给定的一维数组中生成随机数
- 参数: a为一维数组类似数据或整数;size为数组维度;p为数组中的数据出现的概率
- a为整数时,对应的一维数组为np.arange(a)
- 当replace为False时,生成的随机数不能有重复的数值
- 参数p为a中每个元素的可能性,长度与参数a的长度需要一致;参数p为概率,p里的数据之和应为1
np.random.choice(5,3)
# array([1, 1, 4])
np.random.choice(5, (3,4))
# array([[0, 0, 0, 3],
[1, 1, 0, 1],
[0, 2, 4, 1]])
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])
# array([['sansumg', 'lenovo', 'iphone'],
['sansumg', 'xiaomi', 'lenovo'],
['sansumg', 'sansumg', 'lenovo']], dtype='<U7')
15.9 np.random.seed()
生成随机数的种子,使得每次生成随机数相同。
- np.random.seed()的作用:使得随机数据可预测。
- 当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数。
np.random.seed(0)
np.random.random((2,3))
# array([[0.5488135 , 0.71518937, 0.60276338],
[0.54488318, 0.4236548 , 0.64589411]])
np.random.random((2,3))
# array([[0.43758721, 0.891773 , 0.96366276],
[0.38344152, 0.79172504, 0.52889492]])
np.random.seed(0)
np.random.random((2,3))
# array([[0.5488135 , 0.71518937, 0.60276338],
[0.54488318, 0.4236548 , 0.64589411]])
16 np.array和np.asarray
array和asarray都可将结构数据转换为ndarray类型。
但是主要区别
就是当数据源是ndarray时,
array仍会copy出一个副本,占用新的内存,但asarray不会。
示例一:
import numpy as np
data1 = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
arr2 = np.array(data1)
arr3 = np.asarray(data1)
data1[1][1] = 2
print('data1:', data1, id(data1), sep='\n')
print('arr2:', arr2, id(arr2), sep='\n')
print('arr3:', arr3, id(arr3), sep='\n')
output:
data1:
[[1, 1, 1], [1, 2, 1], [1, 1, 1]]
1879366933512
arr2:
[[1 1 1]
[1 1 1]
[1 1 1]]
1879366975280
arr3:
[[1 1 1]
[1 1 1]
[1 1 1]]
1879366975360
可见array和asarray没有区别,都得元数据进行了复制。
示例二:
import numpy as np
arr1=np.ones((3,3))
arr2=np.array(arr1)
arr3=np.asarray(arr1)
arr1[1]=2
data1[1][1] = 2
print('data1:', data1, id(data1), sep='\n')
print('arr2:', arr2, id(arr2), sep='\n')
print('arr3:', arr3, id(arr3), sep='\n')
output:
data1:
[[1, 1, 1], [1, 2, 1], [1, 1, 1]]
1879366933512
arr2:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
1879367201456
arr3:
[[1. 1. 1.]
[2. 2. 2.]
[1. 1. 1.]]
1879366975200
fromfunction
np.linalg.inv(A)
a.resize a.reshape
a.ravel()
np.hsplit(a, 3)
np.vsplit(a, 3)
np.median(a,axis=0)
np.mean(a, axis=1)
np.average
np.var(a, axis=1)
np.std(a, axis=0)
使用数字 0 将一个全为 1 的 5x5 二维数组包围
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
创建一个 5x5 的二维数组,并设置值 1, 2, 3, 4 落在其对角线下方
Z = np.diag(1+np.arange(4),k=-1)
创建一个 10x10 的二维数组,并使得 1 和 0 沿对角线间隔放置
Z = np.zeros((10,10), dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
Z
找出两个一维数组中相同的元素
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print("Z1:", Z1)
print("Z2:", Z2)
np.intersect1d(Z1,Z2)
创建一个 0-10 的一维数组,并将 (1, 9] 之间的数全部反转成负数
Z = np.arange(11)
Z[(1 < Z) & (Z <= 9)] *= -1
Z
使用 NumPy 打印昨天、今天、明天的日期
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
today = np.datetime64('today', 'D')
tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
print("yesterday: ", yesterday)
print("today: ", today)
print("tomorrow: ", tomorrow)
使用五种不同的方法去提取一个随机数组的整数部分
Z = np.random.uniform(0,10,10)
print("原始值: ", Z)
print ("方法 1: ", Z - Z%1)
print ("方法 2: ", np.floor(Z))
print ("方法 3: ", np.ceil(Z)-1)
print ("方法 4: ", Z.astype(int))
print ("方法 5: ", np.trunc(Z))
创建等间隔一维数组
np.linspace(1, 10, num=6)
打印每个 NumPy 标量类型的最小值和最大值
for dtype in [np.int8, np.int32, np.int64]:
print("The minimum value of {}: ".format(dtype), np.iinfo(dtype).min)
print("The maximum value of {}: ".format(dtype),np.iinfo(dtype).max)
for dtype in [np.float32, np.float64]:
print("The minimum value of {}: ".format(dtype),np.finfo(dtype).min)
print("The maximum value of {}: ".format(dtype),np.finfo(dtype).max)
将 float32
转换为整型
Z = np.arange(10, dtype=np.float32)
print(Z)
Z = Z.astype(np.int32, copy=False)
Z
将随机二维数组按照第 3 列从上到下进行升序排列
Z = np.random.randint(0,10,(5,5))
print("排序前:\n",Z)
Z[Z[:,2].argsort()]
参考:
Python之Numpy库常用函数大全(含注释):https://www.cnblogs.com/TensorSense/p/6795995.html
NumPy基础--常用函数:https://blog.csdn.net/stefanie927/article/details/78066717
https://www.jianshu.com/p/71dc9d2a7772