Numpy

作者: 我是聪 | 来源:发表于2018-12-28 14:42 被阅读0次

    Arrays

    A numpy array is a grid of values, all of the same type(类型相同), and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

    We initialize numpy arrays from nested(嵌套) Python lists, and access elements using square brackets(方括号):

    import numpy as np

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

    print(type(a))    print(a.shape)    print(a[0], a[1], a[2])    a[0] = 5

    b = np.array([[1, 2, 3], [4, 5, 6]])

    print(b.shape)    print(b[0, 0], b[0, 1], b[1, 0])

    Numpy also provides many functions to create arrays:

    import numpy as np

    a = np.zeros((2, 2))

    b = np.ones((1, 2))

    e = np.random.random((2, 2))

    Array indexing

    Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional, you must specify a slice for each dimension of the array:

    import numpy as np 

    a = np.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]])

    b = a[:2, 1:3]

    print(a[0, 1])    b[0, 0] = 77    print(a[0, 1]) # b[0, 0] is the same piece of data as a[0, 1]

    Boolean array indexing: Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition. Here is an example:

    import numpy as np 

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

    bool_idx = (a > 2)

    print(bool_idx)

    print(a[a > 2]) #We can do all of the above in a single concise statement

    Datatypes

    Every numpy array is a grid of elements of the same type. Numpy provides a large set of numeric datatypes that you can use to construct arrays. Numpy tries to guess a datatype(猜测数据类型) when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype(显示指定数据类型). Here is an example:print

    import numpy as np

    x = np.array([1, 2])#Let numpy choose the datatype

    print(x.dtype) #Prints "int64"

    x = np.array([1.0, 2.0]) # Let numpy choose the datatype

    ptint(x.dtype) # Print "float64

    Array math

    Basic mathematical functions operate elementwise on arrays, and are available both as operator overloads and as functions in the numpy module:

    import numpy sa np

    x = np.array([[1,2],[3, 4]], dtype = np.float64)

    y = np.array([[5, 6],[7, 8]], dtype = np.float64)

    print(x + y)

    print(np.add(x, y))


    Broadcasting

    Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when performing arithmetic operations(不同大小数组之间的操作). Frequently we have a smaller array and a la#rger array, and we want to use the smaller array multiple times to perform some operation on the larger array.

    import numpy as np

    #we will add the vector v to each row of the matric x,

    # storing the result in the matrix y                                                                                                                              x = np.array([[1, 2, 3], [4, 5, 6], [7,8, 9], [10, 11, 12]])

    y = np.array([1, 0, 1])

    y = np.empty_like(x) # Create an each row of the matrix with an explicit loop

    for i in range(4)

        y[i, :] = x[i, :] + v

    This works; however when the matrix x is very large, computing an explict loop in Python could be slow.

    Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix vv by stacking multiple copies of v vertically,then performing elementwise summation of x and vv.We could implement this approach like 

    import numpy as np

    #We will add the vector v to each row of the matrix x 

    #storing the result in the matrix y

    x = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9],[10, 11, 12]])

    v = np.array([1, 0, 1])

    vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other

    y = x + w

    print(y)

    Numpy broadcasting allows us to perform this computation without actually creating multiple copies of v.Consider this version, using broacasting:

    import numpy as np  

    x = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]])

    v = x + v # Add v to each row of x using broadcasting

    print(y)


    SciPy

    Numpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays.Scipy builds on this,and provides a large number of functions that operate on numpy arrays and are useful for different types of scientific and engineering applications.

    Image operations

    Scipy provides some basic functions to work with images. For example, it has functions to read images from disk into numpy arrays, to write numpy arrays to disk as images, and to resize images.

    from scipy.misc import imread, imsave,imresize 

    # Read an JPEG image into a numpy array

    img = imread('asset/cat.jpg')

    print(img.dtype, img.shape) # Prints "unit8 (400, 248, 3)"

    # We can tint the image by scaling each of the color channels

    # by a different scalar constant.The image has shape (400, 248, 3)

    #We multiply it by the array [1, 0.95, 0.9 ] of (3,);

    #numpy broadcasting means that this leaves the red channel unchanged

    #and multiple the green and blue channels by 0.95 and 0.9 respecctively

    img_tinted = img * [1, 0.95 ,0.9]

    #Resize the tinted image to be 300 by 300 pixels

    img_tinted = imresize(img_tinted, 300, 300))

    # Write the tinted images back to disk

    imsave('assets/cat_tinted.jpg', img_tinted)


    Matplotlib

    import numpy as np

    import matplotlib.pyplot as plt

    # Compute the x and y coordinates for points on a sine curve

    x = np.arrage(0, 3 * np.pi, 0.1)

    y = np.sin(x)

    # Plot the points using matplotlib

    plt.plot(x, y)

    plt.show() # You must call plt.show() to make graphics appear

    With just a little bit of extra work we can easily plot multiple lines at once,

    and add a title, legend, and axis label

    import numpy as np

    import matplotlib.pyplot as plt

    x = np.arange(0, 3 * np.pi, 0..1)

    y_sin = np.sin(x)

    y_cos = np.cos(x)

    # Plot the points using matplotlib

    plt.plot(x, y_sin)

    plt.plot(x, y_cos)

    plt.xlabel('x axis label')

    plt.ylabe("y axis label")

    plt.title("Since and Cosine")

    plt.legend([ "Sine", "Cosine"])

    plt.show()

    Subplots

    you can plot different things in the same figure using the subplot function

    import numpy as np

    import matplotlib.pyplot as plt 

    # Compute the x and y coordinates for points on sine and cosine curves

    x = np.arange(0, 3 * np.pi, 0.1)

    y_sin = np.sin(x)

    y_sin = np.cos(x)


    相关文章

      网友评论

          本文标题:Numpy

          本文链接:https://www.haomeiwen.com/subject/yblykqtx.html