• python numpy 学习


    例子

    >>> import numpy as np
    >>> a = np.arange(15).reshape(35)
    >>> a

    array([[ 0,  1,  2,  3,  4],

           [ 5,  6,  7,  8,  9],

           [10, 11, 12, 13, 14]])

    >>> a.shape

    (3, 5)

    >>> a.ndim

    2

    >>> a.dtype.name

    'int64'

    >>> a.itemsize

    8

    >>> a.size

    15

    >>> type(a)

    <type 'numpy.ndarray'>

    >>> b = np.array([6, 7, 8])

    >>> b

    array([6, 7, 8])

    >>> type(b)

    <type 'numpy.ndarray'>

    创建数组

    这里有多种创建数组的方法。

    例如,你可以通过一个Python内置的列表或者元组函数创建一个数组。产生的数组的类型由序列中的元素的类型推导出来的。

    >>> import numpy as np

    >>> a = np.array([2,3,4])

    >>> a

    array([2, 3, 4])

    >>> a.dtype

    dtype('int64')

    >>> b = np.array([1.2, 3.5, 5.1])

    >>> b.dtype

    dtype('float64')

     

    通常,数组的元素是起初是未知的,但是排列大小是已知的。因此,numpy提供了一些函数在创建数组的时候初始化占位符的内容。这样使扩展数组以及运算成本最小化。

    函数zeros创建一个全是0的数组,ones创建一个全是1的数组,empty创建一个初始内容根据内存状态而随即的随即内容。默认情况下,这些创建的数组的dtype都是float64

    >>> np.zeros( (3,4) )

    array([[ 0.,  0.,  0.,  0.],

           [ 0.,  0.,  0.,  0.],

           [ 0.,  0.,  0.,  0.]])

    >>> np.ones( (2,3,4), dtype=np.int16 )                # dtype 也能自定义

    array([[[ 1, 1, 1, 1],

            [ 1, 1, 1, 1],

            [ 1, 1, 1, 1]],

           [[ 1, 1, 1, 1],

            [ 1, 1, 1, 1],

            [ 1, 1, 1, 1]]], dtype=int16)

    >>> np.empty( (2,3) )                                 # 未初始化,内容不一致

    array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],

           [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])

    创建一个数字序列,numpy提供一个类似range的函数,返回数组而不是列表

    >>> np.arange( 10, 30, 5 )

    array([10, 15, 20, 25])

    >>> np.arange( 0, 2, 0.3 )                 # 接受浮点型实参

    array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])

    当arange传入浮点型实参的时候,它通常不能预测元素的数量,主要因为浮点类型的精度。因此,更好的办法是通过linspace函数接收一个实参作为我们想要得到的元素数量。

    >>> from numpy import pi

    >>> np.linspace( 0, 2, 9 )                 # 从0到2一个9个元素

    array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])

    >>> x = np.linspace( 0, 2*pi, 100 )        # 有时非常有用

    >>> f = np.sin(x)

    更多内容参考:

    array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, numpy.random.rand, numpy.random.randn, fromfunction, fromfile

    打印数组

    当你打印数组的时候,numpy的显示类似于嵌套的列表,但是诚信以下布局。

    一维的轴从左到右打印

    二维的轴自上至下打印

    二维以后的轴自上至下打印,每个切片通过空格与写一个隔开

    >>> a = np.arange(6)                         # 一维数组

    >>> print(a)

    [0 1 2 3 4 5]

    >>>

    >>> b = np.arange(12).reshape(4,3)           # 二维数组

    >>> print(b)

    [[ 0  1  2]

     [ 3  4  5]

     [ 6  7  8]

     [ 9 10 11]]

    >>>

    >>> c = np.arange(24).reshape(2,3,4)         # 三维数组

    >>> print(c)

    [[[ 0  1  2  3]

      [ 4  5  6  7]

      [ 8  9 10 11]]

     [[12 13 14 15]

      [16 17 18 19]

      [20 21 22 23]]]

    点击这里,查看更多与reshape的细节

    如果一个数组过于庞大,numpy会自动的跳过中间的一些内容,只打印四端。

    >>> print(np.arange(10000))

    [   0    1    2 ..., 9997 9998 9999]

    >>>

    >>> print(np.arange(10000).reshape(100,100))

    [[   0    1    2 ...,   97   98   99]

     [ 100  101  102 ...,  197  198  199]

     [ 200  201  202 ...,  297  298  299]

     ...,

     [9700 9701 9702 ..., 9797 9798 9799]

     [9800 9801 9802 ..., 9897 9898 9899]

     [9900 9901 9902 ..., 9997 9998 9999]]

    如果希望强制打印所有内容,可以更改打印参数。

    >>> np.set_printoptions(threshold='nan')

    基本运算

    数组的算术运算时机遇每个元素的,一个新的数组被创建并且填充运算结果

    >>> a = np.array( [20,30,40,50] )

    >>> b = np.arange( 4 )

    >>> b

    array([0, 1, 2, 3])

    >>> c = a-b

    >>> c

    array([20, 29, 38, 47])

    >>> b**2

    array([0, 1, 4, 9])

    >>> 10*np.sin(a)

    array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])

    >>> a<35

    array([ True, True, False, False], dtype=bool)

    不像其他矩阵语言,乘积运算符*在numpy数组中是按元素的,矩阵积可以通过dot函数或者方法执行

    >>> A = np.array( [[1,1],

    ...             [0,1]] )

    >>> B = np.array( [[2,0],

    ...             [3,4]] )

    >>> A*B                         # 按元素乘积

    array([[2, 0],

           [0, 4]])

    >>> A.dot(B)                    # 矩阵积

    array([[5, 4],

           [3, 4]])

    >>> np.dot(A, B)                # 另一种矩阵积

    array([[5, 4],

           [3, 4]])

    其他运算,如+=及*=,直接在原有基础上修改而不是创建新的对象

    >>> a = np.ones((2,3), dtype=int)

    >>> b = np.random.random((2,3))

    >>> a *= 3

    >>> a

    array([[3, 3, 3],

           [3, 3, 3]])

    >>> b += a

    >>> b

    array([[ 3.417022  ,  3.72032449,  3.00011437],

           [ 3.30233257,  3.14675589,  3.09233859]])

    >>> a += b                  # b is not automatically converted to integer type

    Traceback (most recent call last):

      ...

    TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

    Traceback (most recent call last):

      ...

    TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

    注:其实上面再不同的python版本会不一样,2.7.9不会出错

    当两个不同类型的数组进行运算的时候,运算结果的数组会趋向于更复杂的?(不懂)

    >>>

    >>> a = np.ones(3, dtype=np.int32)

    >>> b = np.linspace(0,pi,3)

    >>> b.dtype.name

    'float64'

    >>> c = a+b

    >>> c

    array([ 1.        ,  2.57079633,  4.14159265])

    >>> c.dtype.name

    'float64'

    >>> d = np.exp(c*1j)

    >>> d

    array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,

           -0.54030231-0.84147098j])

    >>> d.dtype.name

    'complex128'

    许多的一元的运算,例如计算所有元素之和,被封装成一个ndarray类

    >>> a = np.random.random((2,3))

    >>> a

    array([[ 0.18626021,  0.34556073,  0.39676747],

           [ 0.53881673,  0.41919451,  0.6852195 ]])

    >>> a.sum()

    2.5718191614547998

    >>> a.min()

    0.1862602113776709

    >>> a.max()

    0.6852195003967595

    默认情况下,这些运算可以应用于数组,就像列表中的数值,而不是它的shapes,然而,通过指定它的轴线参数,你可以把运算应用到单独的其中一轴上。

    >>> b = np.arange(12).reshape(3,4)

    >>> b

    array([[ 0,  1,  2,  3],

           [ 4,  5,  6,  7],

           [ 8,  9, 10, 11]])

    >>>

    >>> b.sum(axis=0)                            # sum of each column

    array([12, 15, 18, 21])

    >>>

    >>> b.min(axis=1)                            # min of each row

    array([0, 4, 8])

    >>>

    >>> b.cumsum(axis=1)                         # cumulative sum along each row

    array([[ 0,  1,  3,  6],

           [ 4,  9, 15, 22],

           [ 8, 17, 27, 38]])

    注:二维数组,axis=0取竖直方向,axis=1取水平方向  (axis二维以上求指教!!!不会)

    通用函数

    Numpy提供常见的数学函数,如sin,cos,exp。

    在numpy中,这些被称为universal functions(ufunc),在numpy内部,这些函数按元素在数组中计算,产生一个数组作为输出

    >>> B = np.arange(3)

    >>> B

    array([0, 1, 2])

    >>> np.exp(B)

    array([ 1.        ,  2.71828183,  7.3890561 ])

    >>> np.sqrt(B)

    array([ 0.        ,  1.        ,  1.41421356])

    >>> C = np.array([2., -1., 4.])

    >>> np.add(B, C)

    array([ 2.,  0.,  6.])

    其他函数

    all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, inv, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var, vdot, vectorize, where

    索引,切片,迭代

    一维数组能被索引,切片,迭代,非常像列表以及python中的其他序列

    >>> a = np.arange(10)**3

    >>> a

    array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])

    >>> a[2]

    8

    >>> a[2:5]

    array([ 8, 27, 64])

    >>> a[:6:2] = -1000    # 从0到6启动每隔“第二位”对于-1000

    >>> a

    array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])

    >>> a[ : :-1]                                 # reversed a

    array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])

    >>> for i in a:

    ...     print(i**(1/3.))

    ...

    nan

    1.0

    nan

    3.0

    nan

    5.0

    6.0

    7.0

    8.0

    9.0

    多维数组每个轴线都有索引。这些索引由一个逗号分隔的元组给出。

    >>> def f(x,y):

    ...     return 10*x+y

    ...

    >>> b = np.fromfunction(f,(5,4),dtype=int)

    >>> b

    array([[ 0,  1,  2,  3],

           [10, 11, 12, 13],

           [20, 21, 22, 23],

           [30, 31, 32, 33],

           [40, 41, 42, 43]])

    >>> b[2,3]

    23

    >>> b[0:5, 1]                       # b的每行中的第2列

    array([ 1, 11, 21, 31, 41])

    >>> b[ : ,1]                        # 与上面一样

    array([ 1, 11, 21, 31, 41])

    >>> b[1:3, : ]                      # 1到3行所有列

    array([[10, 11, 12, 13],

           [20, 21, 22, 23]])

    当索引值数量比轴的数量少的时候,缺失的索引会被切片“:”补全

    >>> b[-1]                                  # 相当于b[-1,:]

    array([40, 41, 42, 43])

    在b[i]括号里的表达式会当成i以及跟上足够的很多的“:”,代表剩下的轴。Numpy也允许你听过点来写,如b[i,....]

  • 相关阅读:
    GWT中如何将EMF对象存储为XMI/从XMI反序列化为对象
    springboot工程启动时报JSR330 'javax.inject.Inject' annotation found and supported for autowiring ,启动缓慢甚至卡住不动
    深度剖析ArrayList和LinkedList
    IRasterGeometryProc,一个可以控制raster对象的接口
    sessionStorage的使用
    spring配置问题:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors...java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are
    Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if exp
    No 'AccessControlAllowOrigin' header is present之跨域问题及解决方案
    Caused by: java.lang.NoClassDefFoundError: Ljavax/servlet/http/HttpServletRequest;
  • 原文地址:https://www.cnblogs.com/zhouxiaohui888/p/5920112.html
Copyright © 2020-2023  润新知