进来学习机器学习,把能用到的程序作业里用到的函数记录下来,方便以后使用
一、numpy.zeros(np.zeros)
用法:zeros(shape, dtype=float, order='C')
返回:返回来一个给定形状和类型的用0填充的数组;
参数:shape:形状
dtype:数据类型,可选参数,默认numpy.float64
dtype类型:t ,位域,如t4代表4位
b,布尔值,true or false
i,整数,如i8(64位)
u,无符号整数,u8(64位)
f,浮点数,f8(64位)
c,浮点负数,
o,对象,
s,a,字符串,s24
u,unicode,u24
order:可选参数,c代表与c语言类似,行优先;F代表列优先
例子:
np.zeros(5)
array([ 0., 0., 0., 0., 0.])
np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])
np.zeros((2, 1))
array([[ 0.],
[ 0.]])
s = (2,2)
np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
dtype=[('x', '<i4'), ('y', '<i4')])
二、numpy.arange(np.arange)函数
arange函数
用于创建等差数组
arange([start,] stop[, step,], dtype=None)
模块简介告诉我们arange有四个参数,其中start,step,dtype可以省略,分别是起始点、步长、和返回类型。
实例程序一
a = np.arange(5) #起始点0,结束点5,步长1,返回类型array,一维
print(a)
>>>[0 1 2 3 4]
示例程序二
import numpy as np
a = np.arange(6).reshape(2,3) #2行3列 tuple类型
print(a)
>>>[[0 1 2]
[3 4 5]]
示例程序三
import numpy as np
a = np.arange(5,20, step = 2)
print(a)
>>>[ 5 7 9 11 13 15 17 19]
三、np.meshgrid()
np.meshgrid()函数可以实现从坐标向量返回坐标矩阵
x = np.arange(-2,2)
y = np.arange(0,3)#生成一位数组,其实也就是向量
x
Out[31]: array([-2, -1, 0, 1])
y
Out[32]: array([0, 1, 2])
z,s = np.meshgrid(x,y)#将两个一维数组变为二维矩阵
z
Out[36]:
array([[-2, -1, 0, 1],
[-2, -1, 0, 1],
[-2, -1, 0, 1]])
s
Out[37]:
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2]])
从代码上看,我们得到了这样一组值:
-2, -1, 0, 1,---- 0, 0, 0, 0
-2, -1, 0, 1,---- 1, 1, 1, 1
也就是说,它讲 x 变成了矩阵 z 的行向量,y 变成了矩阵 s 的列向量。
反过来,也是一样的:
z,s = np.meshgrid(y,x)
z
Out[40]:
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])
s
Out[41]:
array([[-2, -2, -2],
[-1, -1, -1],
[ 0, 0, 0],
[ 1, 1, 1]])
从上面的例子来说,这个过程就构造了一个坐标矩阵,说白了就是一个网格,此处可以参照matlab中meshgrid函数的使用方法以及网格的生成过程。
四、np.where()
使用格式为where(condition, [x, y]) 当condition为true时,返回x,否则返回y
举个例子
In [3]: x = np.arange(9).reshape(3,3)#创建一个3×3的矩阵
In [4]: x
Out[4]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [5]: np.where(x>4) #只输入condition
Out[5]: (array([1, 2, 2, 2], dtype=int64), array([2, 0, 1, 2], dtype=int64))
In [6]: np.where(x>7)# 只输入condition
Out[6]: (array([2], dtype=int64), array([2], dtype=int64))
如果只有condition时,where返回的是一个位置索引,返回的是满足condition的条件
说明:返回的第一个数组为横坐标,第二个为纵坐标
同样也可以这样使用
In [8]: y = np.random.randn(3,3)
In [9]: y
Out[9]:
array([[ 1.59809956, -0.42735851, 1.46593089],
[-0.26497622, 0.53948157, -2.01569974],
[-0.11099139, -1.70616601, -1.34821361]])
In [10]: np.where(y > 0, 4, -4)
Out[10]:
array([[ 4, -4, 4],
[-4, 4, -4],
[-4, -4, -4]])
五、np.reshape()
数组新的shape属性应该要与原来的配套,如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。
举个例子
举个例子
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
也就是说,先前我们不知道z的shape属性是多少,但是想让z变成只有一列,行数不知道多少,通过`z.reshape(-1,1)`,Numpy自动计算出有12行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
六、np.hstack()
函数原型:hstack(tup) ,参数tup可以是元组,列表,或者numpy数组,返回结果为numpy的数组。看下面的代码体会它的含义
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.hstack((a,b,c,d)))
输出:
[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]]
还有vstack()函数,将数据垂直的堆叠起来
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]]