机器学习中常用的Numpy函数

1、numpy.nonzeros()

返回非0元素的索引
如果是二维矩阵的话,返回两个数组。第一个数组包含矩阵非0元素按从左到右从上到下在行上的索引,第二个数组包含矩阵非0元素按从左到右从上到下在列上的索引

2、numpy.flatten()

返回矩阵展开在一维下的元祖

3、numpy.argsort(x)

返回元祖x按升序排列的索引值,默认为按照从小到大排列
numpy.argsort(-x)
回元祖x按降序排列的索引值

4、numpy.fill_diagonal

np.fill_diagonal(a, 5)
将矩阵a对角元素设置为5

5、numpy.multiply

矩阵对应位置元素相乘

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])
6、numpy.intersect1d

求两个数组的交集

>>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
array([1, 3])

要求交集的数组多于两个, 可使用functools.reduce:

>>> from functools import reduce
>>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([3])
7、numpy.where

找到矩阵中满足给定条件的元素的索引

>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )]               # 返回大于3的值.
array([ 4.,  5.,  6.,  7.,  8.])
>>> np.where( x == 3.0 )             # 返回等于3的值的索引.
(array([1], dtype=int64), array([0], dtype=int64))
8、numpy.empty_like

返回与给定数据具有同样大小和类型的数据,并且初始化为0,同理还有ones_like(),zeros_like()

>>> x = np.array([[1., 2., 3.],[4.,5.,6.]])
>>> np.empyt_like( x )
array([[ 0.,  0.,  0.],
     [ 0.,  0.,  0.]])
>>> x = np.array([[1., 2., 3.],[4.,5.,6.]])
>>> np.ones_like( x )
array([[ 1.,  1.,  1.],
     [ 1.,  1.,  1.]])
9、numpy.reshape

返回与给定数据具有同样值但不同结构的数据

>>> x = np.array([[1., 2., 3.],[4.,5.,6.]])
>>> x
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
>>> np.reshape(x,(3,2))
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
10、numpy.random.randint (low, high=None, size=None, dtype=’l’)

返回从lowhigh之间的随机整数

>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])

>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
       [3, 2, 2, 0]])

>>> y_train = np.random.randint(2, size=(10, 1))
>>> y_train
array([[0],
       [1],
       [0],
       [0],
       [0],
       [1],
       [0],
       [0],
       [1],
       [0]])
11、numpy.random.random

返回在半开区间[0.0, 1.0)之间的随机浮点数数

>>> np.random.random_sample()
0.43394098360117983

>>> x_train = np.random.random((1000, 2))
>>> x_train
array([[ 0.64316127,  0.33500098],
       [ 0.69310311,  0.22020709],
       [ 0.53413892,  0.60285074],
       ...,
       [ 0.73776484,  0.02222155],
       [ 0.4160632 ,  0.71319213],
       [ 0.83366365,  0.56880872]])
11、numpy.ravel

返回一个连续的扁平数组

>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print(np.ravel(x))
[1 2 3 4 5 6]

numpy.ravel( )与numpy.reshape(-1, order=order)结果相同

>>> print(x.reshape(-1))
[1 2 3 4 5 6]
>>> print(np.ravel(x, order='F'))
[1 4 2 5 3 6]
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值