• NumPy使用手记[z]


    前面一个NumPy系列基本上是抄书,没有多少具体的内容。最近做实验经常使用NumPy,确实感觉到向量计算的强大。这个系列开始,我记录在使用NumPy使用中的一些具体的技巧和注意事项。

    1) 巧用 where函数

      where函数是numpy的内置,也是一个非常有用的函数,提供了快速并且灵活的计算功能。

    def f_norm_1(data, estimate):
       residule = 0
       for row_index in range(data.shape[0]):
         for column_index in range(data.shape[1]):
           if data[row_index][column_index] != 0:
             residule += (data[row_index][column_index] - estimate[row_index][column_index]) ** 2
       return residule

    def f_norm_2(data, estimate) 

        return sum(where(data != 0, (data-estimate) **2, 0))

    这两段代码完成同样的功能,计算两个矩阵的差,然后将残差进行平方,注意,因为我需要的是考虑矩阵稀疏性,所以不能用内置的norm,函数1是我用普通的python写的,不太复杂,对于规模10*10的矩阵,计算200次耗时0.15s,函数2使用了where函数和sum函数,这两个函数都是为向量计算优化过的,不仅简介,而且耗时仅0.03s, 快了有五倍,不仅如此,有同学将NumPy和matlab做过比较,NumPy稍快一些,这已经是很让人兴奋的结果。

     

    本篇我们看看NumPy中最为基本的Array操作

    >>> from numpy import *

    创建一个矩阵

    >>> a=array([[1,2,3],[4,5,6]])
    >>> a.shape
    (2, 3)

    >>> b=arange(15);print b
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    >>> b.reshape(3,5)
    array([[ 0,  1,  2,  3,  4],
    [ 5,  6,  7,  8,  9],
    [10, 11, 12, 13, 14]])

    可以看到,A是2行3列的矩阵。通过arange方法,可以得到一个1维的数组。然后我们可以通过reshape方法改变它的维度。

    >>> c=zeros((4,5));print c
    [[ 0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.]
    [ 0.  0.  0.  0.  0.]]

    >>> d=ones((5,7));print d
    [[ 1.  1.  1.  1.  1.  1.  1.]
    [ 1.  1.  1.  1.  1.  1.  1.]
    [ 1.  1.  1.  1.  1.  1.  1.]
    [ 1.  1.  1.  1.  1.  1.  1.]
    [ 1.  1.  1.  1.  1.  1.  1.]]

    >>> e=add(c,arange(20).reshape(4,5))
    >>> f=dot(e,d);print f
    [[ 10.  10.  10.  10.  10.  10.  10.]
    [ 35.  35.  35.  35.  35.  35.  35.]
    [ 60.  60.  60.  60.  60.  60.  60.]
    [ 85.  85.  85.  85.  85.  85.  85.]]

    使用zeros可以生成一个零矩阵。同理,用ones可以生成值全部为1的矩阵。我选择了一个4*5的矩阵e,和一个5*7的矩阵d做点乘。最后得到f矩阵。再举一个更加明显的例子:

    >>> a=arange(5);print a
    [0 1 2 3 4]
    >>> b=arange(5).reshape(5,1);print b
    [[0]
    [1]
    [2]
    [3]
    [4]]
    >>> print dot(a,b)
    [30]

    点积的效果更加明显了。

    ndarray的几个常用属性:

    · shape: 代表一个array的形态,是一个向量还是一个矩阵,抑或是一个更复杂的向量组。

    · ndim: 代表这个array的维度

    · size: 在array中拥有的元素数量

    · itemsize: 这个array中每一个元素所需要占的字节数

    · nbytes: 这个array的总字节数(=itemsize*size)

    · real: 代表一个array中所有元素的实数部分

    · imag: 同理,代表一个array中所有元素的虚数部分

    · flat: 将这个array整理成一维的,可以索引的一系列的元素组合。它实际上是通过iterator实现的,我们可以通过for x in array.flat来取得到所有的元素

    · T: 矩阵转置,同transpose()方法

    一些比较有用的方法:

    · tolist(): 将array转化成一个Python中的list对象

    · item(*args): 取得某一位置的元素

    · dump(file): 将这个对象序列化至文件。同cPickle中的dump作用

    · dumps(): 将序列化的结果通过字符串加以输出

    一些关于Array的形态操作:

    · reshape(): 改变array的形态

    · resize(): 也是改变array的形态。不同的是,resize是直接修改这个对象的,而reshape则会生成一个新的对象

    · transpose(): 这个就是矩阵的转置操作啦

    · swapaxes(): 将n个维度中任意两个维度(坐标轴)进行调换

    · flatten(): 复制一个一维的array出来

    还有一些关于Array的运算操作:

    · max():取得所有元素中的最大值

    · min():取得最小值。还有一点值得说,就是max、min这些函数都可以针对某一坐标轴(具体维度)进行运算,例如array.max(axis=0),就在0坐标上求最大值

    · sum():求和

    · cumsum():求累计和

    · prod():求所有元素之积

    · cumprod():求累计积

    · all():如果所有元素都为真,那么返回真;否则返回假

    · any():只要有一个元素为真则返回真

    · mean():求平均数

     

    Array高级操作

    1. Vectorize函数

    def t(x):
    return x + 3
    a1 = scipy.zeros((5,4))
    a1
    NumPy array, format: long
    [[0 0 0 0]
    [0 0 0 0]
    [0 0 0 0]
    [0 0 0 0]
    [0 0 0 0]]
    s = scipy.vectorize(t)
    a2 = s(a1)
    a2
    NumPy array, format: long
    [[3 3 3 3]
    [3 3 3 3]
    [3 3 3 3]
    [3 3 3 3]
    [3 3 3 3]]

    2. NumPy和SciPy相互转化

    import numpy
    import scipy
    a1 = zeros((4,6))
    type(a1)
    <type 'scipy.ndarray'>
    a2 = numpy.asarray(a1)
    type(a2)
    <type 'numpy.ndarray'>
    a3 = numpy.zeros((3,5))
    type(a3)
    <type 'numpy.ndarray'>
    a4 = scipy.asarray(a3)
    type(a4)
    <type 'scipy.ndarray'>

     

     

    NumPy 数学函数

    Trigonometric functions

    sin (x[, out]) Trigonometric sine, element-wise.
    cos (x[, out]) Cosine elementwise.
    tan (x[, out]) Compute tangent element-wise.
    arcsin (x[, out]) Inverse sine elementwise.
    arccos (x[, out]) Trigonometric inverse cosine, element-wise.
    arctan (x[, out]) Trigonometric inverse tangent, element-wise.
    hypot (x1, x2[, out]) Given two sides of a right triangle, return its hypotenuse.
    arctan2 (x1, x2[, out]) Elementwise arc tangent of x1/x2 choosing the quadrant correctly.
    degrees (x[, out]) Convert angles from radians to degrees. This is the same function as rad2deg but the latter is preferred because of the more descriptive name.
    radians (x[, out]) Convert angles from degrees to radians. This function is the same as deg2rad, which is more descriptive..
    unwrap (p[, discont, axis]) Unwrap by changing deltas between values to 2*pi complement.

    Hyperbolic functions

    sinh (x[, out]) Hyperbolic sine, element-wise.
    cosh (x[, out]) Hyperbolic cosine, element-wise.
    tanh (x[, out]) Hyperbolic tangent element-wise.
    arcsinh (x[, out]) Inverse hyperbolic sine elementwise.
    arccosh (x[, out]) Inverse hyperbolic cosine, elementwise.
    arctanh (x[, out]) Inverse hyperbolic tangent elementwise.

    Rounding

    around (a[, decimals, out]) Evenly round to the given number of decimals.
    round_ (a[, decimals, out]) Round an array to the given number of decimals.
    rint (x[, out]) Round elements of the array to the nearest integer.
    fix (x[, y]) Round to nearest integer towards zero.
    floor (x[, out]) Return the floor of the input, element-wise.
    ceil (x[, out]) Return the ceiling of the input, element-wise.

    Sums, products, differences

    prod (a[, axis, dtype, out]) Return the product of array elements over a given axis.
    sum (a[, axis, dtype, out]) Return the sum of array elements over a given axis.
    nansum (a[, axis]) Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
    cumprod (a[, axis, dtype, out]) Return the cumulative product of elements along a given axis.
    cumsum (a[, axis, dtype, out]) Return the cumulative sum of the elements along a given axis.
    diff (a[, n, axis]) Calculate the nth order discrete difference along given axis.
    ediff1d (ary[, to_end, to_begin]) The differences between consecutive elements of an array.
    gradient (f, *varargs) Return the gradient of an N-dimensional array.
    cross (a, b[, axisa, axisb, axisc, ...]) Return the cross product of two (arrays of) vectors.
    trapz (y[, x, dx, axis]) Integrate along the given axis using the composite trapezoidal rule.

    Exponents and logarithms

    exp (x[, out]) Calculate the exponential of the elements in the input array.
    expm1 (x[, out]) Return the exponential of the elements in the array minus one.
    log (x[, out]) Natural logarithm, element-wise.
    log10 (x[, out]) Compute the logarithm in base 10 element-wise.
    log2 (x[, y]) Return the base 2 logarithm.
    log1p (x[, out]) log(1 + x) in base e, elementwise.

    Other special functions

    i0 (x) Modified Bessel function of the first kind, order 0.
    sinc (x) Return the sinc function.

    Floating point routines

    signbit (x[, out]) Returns element-wise True where signbit is set (less than zero).
    frexp (x[, out1, out2]) Split the number, x, into a normalized fraction (y1) and exponent (y2)
    ldexp (x1, x2[, out]) Compute y = x1 * 2**x2.

    Arithmetic operations

    add (x1, x2[, out]) Add arguments element-wise.
    reciprocal (x[, out]) Return element-wise reciprocal.
    negative (x[, out]) Returns an array with the negative of each element of the original array.
    multiply (x1, x2[, out]) Multiply arguments elementwise.
    divide (x1, x2[, out]) Divide arguments element-wise.
    power (x1, x2[, out]) Returns element-wise base array raised to power from second array.
    subtract (x1, x2[, out]) Subtract arguments element-wise.
    true_divide (x1, x2[, out]) Returns an element-wise, true division of the inputs.
    floor_divide (x1, x2[, out]) Return the largest integer smaller or equal to the division of the inputs.
    fmod (x1, x2[, out]) Return the remainder of division.
    mod (x1, x2[, out]) Returns element-wise remainder of division.
    modf (x[, out1, out2]) Return the fractional and integral part of a number.
    remainder (x1, x2[, out]) Returns element-wise remainder of division.

    Handling complex numbers

    angle (z[, deg]) Return the angle of the complex argument.
    real (val) Return the real part of the elements of the array.
    imag (val) Return the imaginary part of array.
    conj (x[, out]) Return the complex conjugate, element-wise.

    Miscellaneous

    convolve (a, v[, mode]) Returns the discrete, linear convolution of two one-dimensional sequences.
    clip (a, a_min, a_max[, out]) Clip (limit) the values in an array.
    sqrt (x[, out]) Return the positive square-root of an array, element-wise.
    square (x[, out]) Return the element-wise square of the input.
    absolute (x[, out]) Calculate the absolute value element-wise.
    fabs (x[, out]) Compute the absolute values elementwise.
    sign (x[, out]) Returns an element-wise indication of the sign of a number.
    maximum (x1, x2[, out]) Element-wise maximum of array elements.
    minimum (x1, x2[, out]) Element-wise minimum of array elements.
    nan_to_num (x) Replace nan with zero and inf with large numbers.
    real_if_close (a[, tol]) If complex input returns a real array if complex parts are close to zero.
    interp (x, xp, fp[, left, right]) One-dimensional linear interpolation.

     

    原帖:http://blog.csdn.net/leiowu1982/archive/2009/03/17/3998301.aspx

  • 相关阅读:
    Linux 下升级python和安装pip
    All of Me
    MangataのACM模板
    HDU1517 A Multiplication Game (博弈论+思维)
    Home_W的握手问题(思维+打表)
    Home_W的几何题 (计算几何)
    stone (组合数学 + Lucas定理)
    随笔分类
    HDU 5586 Sum (预处理 + 动态规划)
    POJ2104 K-th Number (平方分割 + 二分)
  • 原文地址:https://www.cnblogs.com/begtostudy/p/1790935.html
Copyright © 2020-2023  润新知