zoukankan      html  css  js  c++  java
  • [cs231n][Module 0: Preparation] Python Numpy Tutorial

    原文:[python/numpy tutorial]

    翻译:Python Numpy教程

    -----------------------------------------------------------------------------------------

    cs231n课程中的python快速入门

    以下为阿幻的学习笔记:

    Python

    Table of contents:

    python

    • Basic data types
    • Containers(容器)
      • lists
      • dictionaries
      • sets(集合)
      • tuples(元组)
    • Functions
    • Classes

    Numpy

    • Arrays
    • Array indexing(访问数组)
    • Datatypes
    • Array math
    • Broadcasting(广播?)

    SciPy

    • image operations
    • MATLAB files
    • Distance between points

    Matplotlib

    • Potting(绘图)
    • Subplots(绘制多个图形)
    • Images

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    介儿个 上边是 目录 

                            介儿个 下边是 我的笔记

    vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

    python

    • Basic data types

    1. n次方  **n   (介个我以前不知道)

    2. python中没有 x++ 或 x-- 操作

    3. python中没有 && 和 || ,用and,or,not,!=

    4. 字符串操作 

    s = "hello"
    print(len(s))             #print 5
    print(s.capitalize())  # Capitalize a string; prints "Hello"
    print(s.upper())       # Convert a string to uppercase; prints "HELLO"
    print(s.rjust(7))      # Right-justify a string, padding with spaces; prints "  hello"
    print(s.center(7))     # Center a string, padding with spaces; prints " hello "
    print(s.replace('l', '(ell)'))  # Replace all instances of one substring with another;
                                    # prints "he(ell)(ell)o"
    print('  world '.strip())  # Strip leading and trailing whitespace; prints "world"    
    • Containers(容器)
      • lists

    5. 末尾的添加和删除, 以及slicing(切片)操作

    xs = [3, 1, 2]    # Create a list
    print(xs[-1])     # Negative indices count from the end of the list; prints "2"
    xs[2] = 'foo'     # Lists can contain elements of different types
    print(xs)         # Prints "[3, 1, 'foo']"
    xs.append('bar')  # Add a new element to the end of the list
    print(xs)         # Prints "[3, 1, 'foo', 'bar']"
    x = xs.pop()      # Remove and return the last element of the list
    print(x, xs)      # Prints "bar [3, 1, 'foo']"
    
    print(xs[1:])    # Prints "[1, 'foo']"     slicing(切片)

    6. 想要在循环体内访问每个元素的指针,可以使用内置的enumerate函数

    animals = ['cat', 'dog', 'monkey']
    for idx, animal in enumerate(animals):
        print '#%d: %s' % (idx + 1, animal)
    # Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line

    7. 列表推导List comprehensions:在编程的时候,我们常常想要将一种数据类型转换为另一种。下面是一个简单例子,将列表中的每个元素变成它的平方。

    nums = [0, 1, 2, 3, 4]
    squares = [x ** 2 for x in nums if x % 2 == 0]
    print squares # Prints [0, 4, 16]
      • dictionaries

    8. 字典用来储存(键, 值)对,想要访问键和对应的值,那就使用iteritems方法:

    d = {'person': 2, 'cat': 4, 'spider': 8}
    for animal, legs in d.iteritems():
        print 'A %s has %d legs' % (animal, legs)
    # Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

    字典推导Dictionary comprehensions:和列表推导类似,但是允许你方便地构建字典。

      • sets(集合)

    9. 集合是独立不同个体的无序集合

    animals = {'cat', 'dog'}
    animals.add('fish')      # Add an element to a set
    animals.remove('cat')    # Remove an element from a set

    在集合中循环的语法和在列表中一样,但是集合是无序的,所以你在访问集合的元素的时候,不能做关于顺序的假设。

    集合推导Set comprehensions:和字典推导一样,可以很方便地构建集合:

      • tuples(元组)

    10. 元组是一个值的有序列表(不可改变)。从很多方面来说,元组和列表都很相似。和列表最重要的不同在于,元组可以在字典中用作键,还可以作为集合的元素,而列表不行。

    d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
    print d
    t = (5, 6)       # Create a tuple
    print type(t)    # Prints "<type 'tuple'>"
    print d[t]       # Prints "5"
    print d[(1, 2)]  # Prints "1"
    • Functions

    11. Python函数使用def来定义函数

    • Classes

    12. Python对于类的定义是简单直接的:

    class Greeter(object):
        # Constructor
        def __init__(self, name):
            self.name = name  # Create an instance variable
        # Instance method
        def greet(self, loud=False):
            if loud:
                print 'HELLO, %s!' % self.name.upper()
            else:
                print 'Hello, %s' % self.name
    
    g = Greeter('Fred')  # Construct an instance of the Greeter class
    g.greet()            # Call an instance method; prints "Hello, Fred"
    g.greet(loud=True)   # Call an instance method; prints "HELLO, FRED!"

    Numpy

    Numpy是Python中用于科学计算的核心库。它提供了高性能的多维数组对象,以及相关工具。

    • Arrays

    13. 一个numpy数组是一个由不同数值组成的网格。网格中的数据都是同一种数据类型,可以通过非负整型数的元组来访问。维度的数量被称为数组的阶,数组的大小是一个由整型数构成的元组,可以描述数组不同维度上的大小。

    import numpy as np
    b = np.array([[1,2,3],[4,5,6]])   # Create a rank 2 array
    print b                           # 显示一下矩阵b
    print b.shape                     # Prints "(2, 3)"
    print b[0, 0], b[0, 1], b[1, 0]   # Prints "1 2 4"
    
    a = np.zeros((2,2))  # Create an array of all zeros
    print a              # Prints "[[ 0.  0.]
                         #          [ 0.  0.]]"
    b = np.ones((1,2)) # Create an array of all ones print b # Prints "[[ 1. 1.]]"
    c = np.full((1,2), 7) # Create a constant array
    d = np.eye(3) # Create a 2x2 identity matrix print d # Prints "[[ 1. 0. 0.] # [ 0. 1. 0.]
                 #      
    [ 0. 0. 1.]]"
     e = np.random.random((2,2)) # Create an array filled with random values 

    print e # Might print "[[ 0.91940167 0.08143941]
    # [ 0.68744134 0.87236687]]"
    • Array indexing(访问数组)

    14.  整形数组访问  布尔型数组访问  

    切片: 必须为每个维度指定好切片。

    import numpy as np
    
    # Create the following rank 2 array with shape (3, 4)
    # [[ 1  2  3  4]
    #  [ 5  6  7  8]
    #  [ 9 10 11 12]]
    a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    
    # Use slicing to pull out the subarray consisting of the first 2 rows
    # and columns 1 and 2; b is the following array of shape (2, 2):
    # [[2 3]
    #  [6 7]]
    b = a[:2, 1:3]
    
    # A slice of an array is a view into the same data, so modifying it
    # will modify the original array.
    print a[0, 1]   # Prints "2"
    b[0, 0] = 77    # b[0, 0] is the same piece of data as a[0, 1]
    print a[0, 1]   # Prints "77"

    print a[a>10] # Print [11,12]

    整型数组访问语法还有个有用的技巧,可以用来选择或者更改矩阵中每行中的一个元素:

    import numpy as np
    # Create a new array from which we will select elements
    a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
    print a  # prints "array([[ 1,  2,  3],
             #                [ 4,  5,  6],
             #                [ 7,  8,  9],
             #                [10, 11, 12]])"
    
    # Create an array of indices
    b = np.array([0, 2, 0, 1])
    # Select one element from each row of a using the indices in b
    print a[np.arange(4), b]  # Prints "[ 1  6  7 11]"
    
    # Mutate one element from each row of a using the indices in b
    a[np.arange(4), b] += 10
    print a  # prints "array([[11,  2,  3],
             #                [ 4,  5, 16],
             #                [17,  8,  9],
             #                [10, 21, 12]]) 
    • Datatypes

    每个Numpy数组都是数据类型相同的元素组成的网格。Numpy提供了很多的数据类型用于创建数组。当你创建数组的时候,Numpy会尝试猜测数组的数据类型,你也可以通过参数直接指定数据类型 

    • Array math

    基本数学计算函数会对数组中元素逐个进行计算,既可以利用操作符重载,也可以使用函数方式:

    import numpy as np
    
    x = np.array([[1,2],[3,4]], dtype=np.float64)
    y = np.array([[5,6],[7,8]], dtype=np.float64)
    
    # Elementwise sum; both produce the array
    # [[ 6.0  8.0]
    #  [10.0 12.0]]
    print x + y
    print np.add(x, y)
    
    # Elementwise difference; both produce the array
    # [[-4.0 -4.0]
    #  [-4.0 -4.0]]
    print x - y
    print np.subtract(x, y)
    
    # Elementwise product; both produce the array
    # [[ 5.0 12.0]
    #  [21.0 32.0]]
    print x * y
    print np.multiply(x, y)
    
    # Elementwise division; both produce the array
    # [[ 0.2         0.33333333]
    #  [ 0.42857143  0.5       ]]
    print x / y
    print np.divide(x, y)
    
    # Elementwise square root; produces the array
    # [[ 1.          1.41421356]
    #  [ 1.73205081  2.        ]]
    print np.sqrt(x)

    *是元素逐个相乘,而不是矩阵乘法。在Numpy中使用dot来进行矩阵乘法:

    # Matrix / matrix product; both produce the rank 2 array
    # [[19. 22.]
    #  [43. 50.]]
    print x.dot(y)
    print np.dot(x, y)

    sum函数

    import numpy as np
    x = np.array([[1,2],[3,4]])
    print np.sum(x)  # Compute sum of all elements; prints "10"
    print np.sum(x, axis=0)  # Compute sum of each column; prints "[4 6]"
    print np.sum(x, axis=1)  # Compute sum of each row; prints "[3 7]"

    在Numpy中,使用T来转置矩阵:

    import numpy as np
    x = np.array([[1,2], [3,4]])
    print x    # Prints "[[1 2]
               #          [3 4]]"
    print x.T  # Prints "[[1 3]
               #          [2 4]]"
    
    # Note that taking the transpose of a rank 1 array does nothing:
    v = np.array([1,2,3])
    print v    # Prints "[1 2 3]"
    print v.T  # Prints "[1 2 3]"
    • Broadcasting(广播?)

    一种强有力的机制,它让Numpy可以让不同大小的矩阵在一起进行数学计算. 支持广播机制的函数是全局函数。

    举个例子,如果我们要把一个向量加到矩阵的每一行:

    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])
    y = x + v  # Add v to each row of x using broadcasting
    print y  # Prints "[[ 2  2  4]
             #          [ 5  5  7]
             #          [ 8  8 10]
             #          [11 11 13]]"

    对两个数组使用广播机制要遵守下列规则:

    1. 如果数组的秩不同,使用1来将秩较小的数组进行扩展,直到两个数组的尺寸的长度都一样。
    2. 如果两个数组在某个维度上的长度是一样的,或者其中一个数组在该维度上长度为1,那么我们就说这两个数组在该维度上是相容的。
    3. 如果两个数组在所有维度上都是相容的,他们就能使用广播。
    4. 如果两个输入数组的尺寸不同,那么注意其中较大的那个尺寸。因为广播之后,两个数组的尺寸将和那个较大的尺寸一样。
    5. 在任何一个维度上,如果一个数组的长度为1,另一个数组长度大于1,那么在该维度上,就好像是对第一个数组进行了复制。
    import numpy as np
    
    # Compute outer product of vectors
    v = np.array([1,2,3])  # v has shape (3,)
    w = np.array([4,5])    # w has shape (2,)
    # To compute an outer product, we first reshape v to be a column
    # vector of shape (3, 1); we can then broadcast it against w to yield
    # an output of shape (3, 2), which is the outer product of v and w:
    # [[ 4  5]
    #  [ 8 10]
    #  [12 15]]
    print np.reshape(v, (3, 1)) * w
    
    # Add a vector to each row of a matrix
    x = np.array([[1,2,3], [4,5,6]])
    # x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),
    # giving the following matrix:
    # [[2 4 6]
    #  [5 7 9]]
    print x + v
    
    # Add a vector to each column of a matrix
    # x has shape (2, 3) and w has shape (2,).
    # If we transpose x then it has shape (3, 2) and can be broadcast
    # against w to yield a result of shape (3, 2); transposing this result
    # yields the final result of shape (2, 3) which is the matrix x with
    # the vector w added to each column. Gives the following matrix:
    # [[ 5  6  7]
    #  [ 9 10 11]]
    print (x.T + w).T
    
    # Another solution is to reshape w to be a row vector of shape (2, 1);
    # we can then broadcast it directly against x to produce the same
    # output.
    print x + np.reshape(w, (2, 1))
    
    # Multiply a matrix by a constant:
    # x has shape (2, 3). Numpy treats scalars as arrays of shape ();
    # these can be broadcast together to shape (2, 3), producing the
    # following array:
    # [[ 2  4  6]
    #  [ 8 10 12]]
    print x * 2

    SciPy(提供了一些操作图像的基本函数。)

    • image operations

    熟悉SciPy的最好方法就是阅读文档

    from scipy.misc import imread, imsave, imresize, imshow
    # Read an JPEG image into a numpy array
    img = imread('1.jpg')
    print img.dtype, img.shape  # Prints "uint8 (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 shape (3,);
    # numpy broadcasting means that this leaves the red channel unchanged,
    # and multiplies the green and blue channels by 0.95 and 0.9
    # respectively.
    img_tinted = img * [1, 0, 0] #处理RGB颜色通道

    # Resize the tinted image to be 300 by 300 pixels. img_tinted = imresize(img_tinted, (300, 300))
    # Write the tinted image back to disk imsave('assets/cat_tinted.jpg', img_tinted)


    • MATLAB files

    函数scipy.io.loadmat和scipy.io.savemat能够让你读和写MATLAB文件。具体请查看文档

    • Distance between points

    函数scipy.spatial.distance.pdist能够计算集合中所有两点之间的距离:

    import numpy as np
    from scipy.spatial.distance import pdist, squareform
    
    # Create the following array where each row is a point in 2D space:
    # [[0 1]
    #  [1 0]
    #  [2 0]]
    x = np.array([[0, 1], [1, 0], [2, 0]])
    print x
    
    # Compute the Euclidean distance between all rows of x.
    # d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
    # and d is the following array:
    # [[ 0.          1.41421356  2.23606798]
    #  [ 1.41421356  0.          1.        ]
    #  [ 2.23606798  1.          0.        ]]
    d = squareform(pdist(x, 'euclidean'))

    pdist(x, 'euclidean')    # array([ 1.41421356, 2.23606798, 1. ])

    Matplotlib

    Matplotlib是一个作图库。这里简要介绍matplotlib.pyplot模块,功能和MATLAB的作图功能类似。

    • Potting(绘图)

    matplotlib库中最重要的函数是Plot。该函数允许你做出2D图形,如下:

    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_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.ylabel('y axis label')
    plt.title('Sine and Cosine')
    plt.legend(['Sine', 'Cosine'])
    plt.show()

    • Subplots(绘制多个图形)

    可以使用subplot函数来在一幅图中画不同的东西:

    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_cos = np.cos(x)
    # Set up a subplot grid that has height 2 and width 1,
    # and set the first such subplot as active.
    plt.subplot(2, 1, 1)
    
    # Make the first plot
    plt.plot(x, y_sin)
    plt.title('Sine')
    # Set the second subplot as active, and make the second plot.
    plt.subplot(2, 1, 2)
    plt.plot(x, y_cos)
    plt.title('Cosine')

      ###(plt.subplot(2, 1, *) )         ###(plt.subplot( 1, 2, *) )
    • Images

    可使用imshow函数来显示图像

    import numpy as np
    from scipy.misc import imread, imresize
    import matplotlib.pyplot as plt
    
    img = imread('assets/cat.jpg')
    img_tinted = img * [1, 0.95, 0.9]
    
    # Show the original image
    plt.subplot(1, 2, 1)
    plt.imshow(img)
    
    # Show the tinted image
    plt.subplot(1, 2, 2)
    
    # A slight gotcha with imshow is that it might give strange results
    # if presented with data that is not uint8. To work around this, we
    # explicitly cast the image to uint8 before displaying it.
    plt.imshow(np.uint8(img_tinted))
    plt.show()

  • 相关阅读:
    C/C++ _wcsupr_s 函数 – unicode 字符串小写转大写 C语言零基础入门教程
    C/C++ atof函数 C语言零基础入门教程
    C/C++ ultoa函数 C语言零基础入门教程
    C/C++ _strlwr_s 函数 – 字符串大写转小写 C语言零基础入门教程
    C/C++ ceil 函数 C语言零基础入门教程
    C/C++ atol函数 C语言零基础入门教程
    idea在商店无法搜索到插件
    Go 关于 protoc 工具的小疑惑
    Golang 关于 proto 文件的一点小思考
    Go 如何编写 ProtoBuf 插件(二)?
  • 原文地址:https://www.cnblogs.com/guweixin/p/10430581.html
Copyright © 2011-2022 走看看