zoukankan      html  css  js  c++  java
  • 机器学习---笔记----numpy和math包中的常用函数

    本文只是简单罗列一下再机器学习过程中遇到的常用的数学函数。

    1. math.fabs(x): 返回x的绝对值。同numpy。

    >>> import numpy
    >>> import math
    >>> numpy.fabs(-5)
    5.0
    >>> math.fabs(-5)
    5.0
    View Code

    2.  x.astype(type): 返回type类型的x, type 一般可以为numpy.int, numpy.float等,没有math.int等。

    >>> import numpy as np
    >>> d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.float)
    >>> f=d.astype(np.int)
    >>> print f
    [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]]
    >>> d
    array([[  1.,   2.,   3.,   4.],
           [  5.,   6.,   7.,   8.],
           [  9.,  10.,  11.,  12.]])
    View Code

    3. numpy.frompyfunc(func, para_size, valu_size): 将一个计算单个元素的函数func 转换成计算能计算多个元素的函数,返回类型为object。

    >>> f=np.frompyfunc(np.fabs, 1,1)
    >>> f([-1,-2,-3,-4])
    array([1.0, 2.0, 3.0, 4.0], dtype=object)
    View Code

    4. numpy.zeros_like(x): 返回一个用0填充的跟输入数组形 x 状和类型一样的数组。类似的还有 np.ones_like(), np.empty_like(), math包没有该函数。

    >>> d
    array([[  1.,   2.,   3.,   4.],
           [  5.,   6.,   7.,   8.],
           [  9.,  10.,  11.,  12.]])
    >>> np.zeros_like(d)
    array([[ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.]])
    >>> np.empty_like(d)
    array([[ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.]])
    >>> np.ones_like(d)
    array([[ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.]])
    View Code

    5. numpy.exp(x): 返回e的x次方, math函数同此。

    >>> np.exp(2)
    7.3890560989306504
    >>> math.exp(2)
    7.38905609893065
    View Code

    6. numpy.sqrt(x): 返回x的平方根, math函数同此。

    >>> math.exp(2)
    7.38905609893065
    >>> math.sqrt(2)
    1.4142135623730951
    >>> numpy.sqrt(2)
    1.4142135623730951
    >>> numpy.sqrt(4)
    2.0
    View Code

    7. numpy.e, numpy.pi: 引用e, pi, math函数同此。

    >>> np.e
    2.718281828459045
    >>> np.pi
    3.141592653589793
    >>> math.e
    2.718281828459045
    >>> math.pi
    3.141592653589793
    View Code
  • 相关阅读:
    六. 异常处理5.多重catch语句的使用
    六. 异常处理4.try和catch的使用
    六. 异常处理3.未被捕获的异常
    六. 异常处理2.异常类型
    对mysql数据库中字段为空的处理
    mysql 中实现多条数据同时更新
    java 用PDFBox 删除 PDF文件中的某一页
    java7 java MethodHandle解析
    【十四】jvm 性能调优实例
    【十三】jvm 性能调优工具之 jstack
  • 原文地址:https://www.cnblogs.com/no-tears-girl/p/6962832.html
Copyright © 2011-2022 走看看