https://cs231n.github.io/python-numpy-tutorial/#distance-between-points

python

1
2
3
4
5
6
7
8
9
def quicksort(arr):#快排
    if len(arr)<= 1:
        return arr
    pivot=arr[len(arr)//2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left)+middle+quicksort(right)
print(quicksort([3,6,8,10,1,2,1]))
[1, 1, 2, 3, 6, 8, 10]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x = 3
print(type(x)) # Prints "<class 'int'>"
print(x)       # Prints "3"
print(x + 1)   # Addition; prints "4"
print(x - 1)   # Subtraction; prints "2"
print(x * 2)   # Multiplication; prints "6"
print(x ** 2)  # Exponentiation; prints "9"
x += 1
print(x)  # Prints "4"
x *= 2
print(x)  # Prints "8"
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
<class 'int'>
3
4
2
6
9
4
8
<class 'float'>
2.5 3.5 5.0 6.25
1
2
3
4
5
6
7
t = True
f = False
print(type(t)) # Prints "<class 'bool'>"
print(t and f) # Logical AND; prints "False"
print(t or f)  # Logical OR; prints "True"
print(not t)   # Logical NOT; prints "False"
print(t != f)  # Logical XOR; prints "True"
<class 'bool'>
False
True
False
True
1
2
3
4
5
6
7
8
hello = 'hello'    # String literals can use single quotes
world = "world"    # or double quotes; it does not matter.
print(hello)       # Prints "hello"
print(len(hello))  # String length; prints "5"
hw = hello + ' ' + world  # String concatenation
print(hw)  # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12)  # sprintf style string formatting
print(hw12)  # prints "hello world 12"
hello
5
hello world
hello world 12
1
2
3
4
5
6
7
8
s = "hello"
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"
Hello
HELLO
  hello
 hello 
he(ell)(ell)o
world

Lists

1
2
3
4
5
6
7
8
9
xs = [3, 1, 2]    # Create a list
print(xs, xs[2])  # Prints "[3, 1, 2] 2"
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']"
[3, 1, 2] 2
2
[3, 1, 'foo']
[3, 1, 'foo', 'bar']
bar [3, 1, 'foo']
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#切片
nums = list(range(5))     # range is a built-in function that creates a list of integers
print(nums)               # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4])          # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print(nums[2:])           # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print(nums[:2])           # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print(nums[:])            # Get a slice of the whole list; prints "[0, 1, 2, 3, 4]"
print(nums[:-1])          # Slice indices can be negative; prints "[0, 1, 2, 3]"
nums[2:4] = [8, 9]        # Assign a new sublist to a slice
print(nums)
[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]

遍历

1
2
3
4

animals = ['cat', 'dog', 'monkey']
for animal in animals:
    print(animal)
cat
dog
monkey

遍历访问每个元素的索引enumerate

1
2
3
4
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
#1: cat
#2: dog
#3: monkey

列表推导式(生成式语法)

1
2
3
4
5
nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
    squares.append(x ** 2)
print(squares)   # Prints [0, 1, 4, 9, 16]
[0, 1, 4, 9, 16]
1
2
3
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print(squares)   # Prints [0, 1, 4, 9, 16]
[0, 1, 4, 9, 16]
1
2
3
4
#也可以包含条件
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)  # Prints "[0, 4, 16]"
[0, 4, 16]

Dictionaries

字典存储(键、值)对 (key, value)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
print(d['cat'])       # Get an entry from a dictionary; prints "cute"
print('cat' in d)     # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet'     # Set an entry in a dictionary
print(d['fish'])      # Prints "wet"
# print(d['monkey'])  # KeyError: 'monkey' not a key of d
#使用get方法通过键获取对应的值,如果取不到不会引发KeyError异常而是返回None或设定的默认值
print(d.get('monkey', 'N/A'))  # Get an element with a default; prints "N/A"
print(d.get('fish', 'N/A'))    # Get an element with a default; prints "wet"
del d['fish']         # Remove an element from a dictionary
print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"
cute
True
wet
N/A
wet
N/A

遍历

1
2
3
4
5
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
    legs = d[animal]
    print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"
A person has 2 legs
A cat has 4 legs
A spider has 8 legs

如果您想访问键及其对应的值,请使用 items 方法:

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

生成式语法

1
2
3
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print(even_num_to_square)  # Prints "{0: 0, 2: 4, 4: 16}"
{0: 0, 2: 4, 4: 16}

Sets

集合是不同元素的无序集合。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
animals = {'cat', 'dog'}
print('cat' in animals)   # Check if an element is in a set; prints "True"
print('fish' in animals)  # prints "False"
animals.add('fish')       # Add an element to a set
print('fish' in animals)  # Prints "True"
print(len(animals))       # Number of elements in a set; prints "3"
#添加已经存在的元素,不会发生什么变化
animals.add('cat')        # Adding an element that is already in the set does nothing
print(len(animals))       # Prints "3"
animals.remove('cat')     # Remove an element from a set
print(len(animals))       # Prints "2"
True
False
True
3
3
2

遍历

遍历一个集合和遍历一个列表具有相同的语法; 但是由于集合是无序的,你不能对访问集合元素的顺序做出假设:

1
2
3
4
5
6
animals = {'cat', 'dog', 'fish'}
for elem in animals:
    print(elem)
for idx, animal in enumerate(animals):
    print('#%d: %s' % (idx + 1, animal))
# Prints "#1: fish", "#2: dog", "#3: cat"
cat
dog
fish
#1: cat
#2: dog
#3: fish

enumerate

enumerate() 函数字面上是枚举、列举的意思,用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

enumerate(sequence,[start=0])

sequence – 一个序列、迭代器或其他支持迭代对象。

start – 下标起始位置。

1
2
3
4
5
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
print(list(enumerate(seasons, start=1)))  # 下标从 1 开始
for i, element in enumerate(seasons):
    print (i, element)
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
0 Spring
1 Summer
2 Fall
3 Winter

生成式

1
2
3
from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print(nums)  # Prints "{0, 1, 2, 3, 4, 5}"
{0, 1, 2, 3, 4, 5}

Tuples

元组是一个(不可变的)有序的值列表。元组在许多方面类似于列表; 最重要的区别之一是,元组可以用作字典中的键和集合的元素,而列表不能。

元组也是多个元素按照一定的顺序构成的序列。元组和列表的不同之处在于,元组是不可变类型,这就意味着元组类型的变量一旦定义,其中的元素不能再添加或删除,而且元素的值也不能进行修改。定义元组通常使用()字面量语法,

1
2
3
4
5
6
7
#用元组创建一个字典
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 "<class 'tuple'>"
print(d[t])       # Prints "5"
print(d[(1, 2)])  # Prints "1"
{(0, 1): 0, (1, 2): 1, (2, 3): 2, (3, 4): 3, (4, 5): 4, (5, 6): 5, (6, 7): 6, (7, 8): 7, (8, 9): 8, (9, 10): 9}
<class 'tuple'>
5
1

Functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def sign(x):
    if x > 0:
        return 'positive'
    elif x < 0:
        return 'negative'
    else:
        return 'zero'

for x in [-1, 0, 1]:
    print(sign(x))
# Prints "negative", "zero", "positive"
negative
zero
positive
1
2
3
4
5
6
7
8
def hello(name, loud=False):
    if loud:
        print('HELLO, %s!' % name.upper())
    else:
        print('Hello, %s' % name)

hello('Bob') # Prints "Hello, Bob"
hello('Fred', loud=True)  # Prints "HELLO, FRED!"
Hello, Bob
HELLO, FRED!

Classes

用 Python 定义类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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!"
Hello, Fred
HELLO, FRED!

NumPy

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

https://www.runoob.com/numpy/numpy-tutorial.html

Arrays

Numpy 数组是一个值网格,所有值的类型都相同,并由一组非负整数进行索引。维数是数组的秩; 数组的形状是一组整数,给出了数组在每个维度上的大小。

我们可以从嵌套的 Python 列表中初始化 numpy 数组,并使用方括号访问元素:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import numpy as np

a = np.array([1, 2, 3])   # Create a rank 1 array
#秩为1,即维数,创建一维数组
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
#表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"
#数组的维度,对于矩阵,n 行 m 列
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"
<class 'numpy.ndarray'>
(3,)
1 2 3
[5 2 3]
(2, 3)
1 2 4
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np

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((2,2), 7)  # Create a constant array
print(c)               # Prints "[[ 7.  7.]
                       #          [ 7.  7.]]"

#创建2*2的单位矩阵
d = np.eye(2)         # Create a 2x2 identity matrix
print(d)              # Prints "[[ 1.  0.]
                      #          [ 0.  1.]]"

#创建一个2*2的随机矩阵
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]]"
[[0. 0.]
 [0. 0.]]
[[1. 1.]]
[[7 7]
 [7 7]]
[[1. 0.]
 [0. 1.]]
[[0.69843379 0.7441049 ]
 [0.54347292 0.01288906]]

Array indexing

切片: 类似于 Python 列表,numpy 数组可以被切片。由于数组可能是多维的,所以必须为数组的每个维度指定一个切片

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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]])

print('数组a的秩为%s,形状为%s'%(a.ndim,a.shape))
# 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]#使用切片提取由前2行组成的子数组和列1和列2; b 是下面的形状数组(2,2)
print(b,b.shape)
# 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"
数组a的秩为2,形状为(3, 4)
[[2 3]
 [6 7]] (2, 2)
2
77

还可以将整数索引和切片索引混合使用。但是,这样做将产生比原始数组更低秩的数组。

访问数组中间行数据的两种方法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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]])

# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :]    # Rank 1 view of the second row of a
row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
print(row_r1, row_r1.shape)  # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape)  # Prints "[[5 6 7 8]] (1, 4)"

# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape)  # Prints "[ 2  6 10] (3,)"
print(col_r2, col_r2.shape)  # Prints "[[ 2]
                             #          [ 6]
                             #          [10]] (3, 1)"
[5 6 7 8] (4,)
[[5 6 7 8]] (1, 4)
[ 2  6 10] (3,)
[[ 2]
 [ 6]
 [10]] (3, 1)

整数数组索引: 当您使用切片索引到 numpy 数组时,生成的数组视图将始终是原始数组的子数组。相比之下,整数数组索引允许您使用来自另一个数组的数据构造任意数组。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import numpy as np

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

# An example of integer array indexing.
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]])  # Prints "[1 4 5]"
#a[x,y]:打印第x行y列。而x=[0,1,2],y=[0,1,0],即分别打印第0,1,2行,对应的第0,1,0列

# The above example of integer array indexing is equivalent to this:
print(np.array([a[0, 0], a[1, 1], a[2, 0]]))  # Prints "[1 4 5]"

# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]])  # Prints "[2 2]"

# Equivalent to the previous integer array indexing example
print(np.array([a[0, 1], a[0, 1]]))  # Prints "[2 2]"
[1 4 5]
[1 4 5]
[2 2]
[2 2]

整数数组索引的一个有用的技巧是从矩阵的每一行中选择或修改一个元素:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
#使用 b 中的索引从 a 的每一行中选择一个元素
print(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"

# Mutate one element from each row of a using the indices in b
#使用 b 中的索引从 a 的每一行变换一个元素
a[np.arange(4), b] += 10

print(a)  # prints "array([[11,  2,  3],
          #                [ 4,  5, 16],
          #                [17,  8,  9],
          #                [10, 21, 12]])
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[ 1  6  7 11]
[[11  2  3]
 [ 4  5 16]
 [17  8  9]
 [10 21 12]]

布尔数组索引: 布尔数组索引允许您挑选数组的任意元素。这种类型的索引通常用于选择满足某些条件的数组元素。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np

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

#Find the elements of a that are bigger than 2;
bool_idx = (a > 2)   # Find the elements of a that are bigger than 2;
                     # this returns a numpy array of Booleans of the same
                     # shape as a, where each slot of bool_idx tells
                     # whether that element of a is > 2.

print(bool_idx)      # Prints "[[False False]
                     #          [ True  True]
                     #          [ True  True]]"

# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
##我们使用布尔数组索引来构造一个秩1数组,由对应于 True 值的元素组成
print(a[bool_idx])  # Prints "[3 4 5 6]"

#我们可以用一句简洁的话来概括以上所有的内容
# We can do all of the above in a single concise statement:
print(a[a > 2])     # Prints "[3 4 5 6]"
[[False False]
 [ True  True]
 [ True  True]]
[3 4 5 6]
[3 4 5 6]

Datatypes

每个 numpy 数组都是由相同类型的元素组成的网格。Numpy 提供了一个大型的数字数据类型集,您可以使用它来构造数组。在创建数组时,Numpy 试图猜测数据类型,但构造数组的函数通常还包含一个可选参数,以显式指定数据类型。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
print(x.dtype)             # Prints "float64"

x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)                         # Prints "int64"
int32
float64
int64

Array math

基本的数学函数在数组上以元素形式运行,可以作为操作符重载和 numpy 模块中的函数:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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))
[[ 6.  8.]
 [10. 12.]]
[[ 6.  8.]
 [10. 12.]]
[[-4. -4.]
 [-4. -4.]]
[[-4. -4.]
 [-4. -4.]]
[[ 5. 12.]
 [21. 32.]]
[[ 5. 12.]
 [21. 32.]]
[[0.2        0.33333333]
 [0.42857143 0.5       ]]
[[0.2        0.33333333]
 [0.42857143 0.5       ]]
[[1.         1.41421356]
 [1.73205081 2.        ]]

dot函数来计算向量的内积,向量与矩阵的乘积

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
#向量的内积
print(v.dot(w))
print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))#9+20,27+40
print(np.dot(x, v))
print(v.dot(x))#9+30,18+40

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
#  [43 50]]
print(x.dot(y))
print(np.dot(x, y))
219
219
[29 67]
[29 67]
[39 58]
[[19 22]
 [43 50]]
[[19 22]
 [43 50]]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import numpy as np

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

print(np.sum(x))  # Compute sum of all elements; prints "10"
#axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;
#axis=1,表示沿着第1轴进行操作,即对每一行进行操作。
#列和
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]"
10
[4 6]
[3 7]

除了使用数组计算数学函数之外,我们还经常需要对数组中的数据进行重塑或以其他方式操作。这类操作最简单的例子是移位矩阵; 要移位矩阵,只需使用数组对象的 T 属性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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]"
[[1 2]
 [3 4]]
[[1 3]
 [2 4]]
[1 2 3]
[1 2 3]

Broadcasting

广播是一种强大的机制,允许 numpy 在执行算术运算时使用不同形状的数组。通常我们有一个较小的数组和一个较大的数组,我们希望多次使用较小的数组来对较大的数组执行一些操作。

例如,假设我们要向矩阵的每一行添加一个常量向量。我们可以这样做:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
#我们将向量 v 加到矩阵 x 的每一行,
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
#创建一个和x相同形状的空矩阵
y = np.empty_like(x)   # Create an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
#通过显式循环将向量 v 添加到矩阵 x 的每一行
for i in range(4):
    y[i, :] = x[i, :] + v

# Now y is the following
# [[ 2  2  4]
#  [ 5  5  7]
#  [ 8  8 10]
#  [11 11 13]]
print(y)
[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]

然而,当矩阵 x 非常大时,在 Python 中计算显式循环可能会很慢。注意,将向量 v 加到矩阵 x 的每一行等价于形成一个垂直叠加 v 的多个副本,然后求 x 和 v 的元素相加的矩阵 v。我们可以这样实施这个方法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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])
#将v的四个副本放在一起
vv = np.tile(v, (4, 1))   # Stack 4 copies of v on top of each other
print(vv)                 # Prints "[[1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]]"
y = x + vv  # Add x and vv elementwise
print(y)  # Prints "[[ 2  2  4
          #          [ 5  5  7]
          #          [ 8  8 10]
          #          [11 11 13]]"
[[1 0 1]
 [1 0 1]
 [1 0 1]
 [1 0 1]]
[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]

使用 Numpy 广播,我们可以在不创建 v 的多个副本的情况下执行这个计算:

尽管由于广播,x 具有形状(4,3) ,v 具有形状(3,) ,y = x + v 仍然可以工作; 这条线的工作方式就好像 v 实际上具有形状(4,3) ,其中每一行都是 v 的副本,并且求和是逐项执行的。

广播通常使您的代码更简洁、更快捷,因此您应该尽可能地使用它

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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]]"
[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]

将两个阵列同时广播遵循以下规则:

如果数组不具有相同的秩,则用1预置低秩数组的形状,直到两个形状具有相同的长度。

如果两个数组的维度大小相同,或者其中一个数组的维度大小为1,则称这两个数组在维度上是兼容的。

如果阵列在所有维度都兼容,那么它们可以一起广播。

广播之后,每个数组的行为就好像它的形状等于两个输入数组的元素最大形状一样。

在任何维度中,如果一个数组的大小为1,而另一个数组的大小大于1,那么第一个数组的行为就好像是沿着该维度复制的

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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]]
#为了计算外积,我们首先将 v 重塑为形状为 (3, 1) 的列向量; 
#然后我们可以针对 w 广播它以产生形状 (3, 2) 的输出,它是 v 和 w 的外积:
print(np.reshape(v, (3, 1)) * w)
[[ 4  5]
 [ 8 10]
 [12 15]]
1
2
3
4
5
6
7
8
# 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)
[[2 4 6]
 [5 7 9]]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 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]]
#向矩阵的每一列添加一个向量,如果我们转置 x,那么它就有形状(3,2)并且可以广播
print((x.T + w).T)
[[ 5  6  7]
 [ 9 10 11]]
1
2
3
4
5
# Another solution is to reshape w to be a column vector of shape (2, 1);
# we can then broadcast it directly against x to produce the same
# output.
#另一种解决方案是将 w 重塑为形状(2,1)的列向量;
print(x + np.reshape(w, (2, 1)))
[[ 5  6  7]
 [ 9 10 11]]
1
2
3
4
5
6
7
8
# 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)
[[ 2  4  6]
 [ 8 10 12]]

Numpy的更多资料 https://numpy.org/doc/stable/reference/

SciPy

Numpy 提供了一个高性能的多维数组和基本工具,可以使用这些数组进行计算和操作。SciPy 以此为基础,提供了大量的函数,这些函数可以在 numpy 数组上运行,并且对于不同类型的科学和工程应用程序非常有用。

官方文档:https://docs.scipy.org/doc/scipy/reference/index.html

Image operations

SciPy 提供了一些处理图像的基本函数。例如,它具有将磁盘中的映像读入 numpy 数组、将 numpy 数组作为映像写入磁盘以及调整映像大小的功能。

1
from scipy.misc import imread, imsave, imresize
---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_16772/1637721638.py in <module>
----> 1 from scipy.misc import imread, imsave, imresize


ImportError: cannot import name 'imread' from 'scipy.misc' (d:\python\python37\lib\site-packages\scipy\misc\__init__.py)

报错:scipy已经将imread等命令删除,需要换另一个库使用imageio https://blog.csdn.net/weixin_45798684/article/details/106333756

解决方案:https://blog.csdn.net/qq_38835585/article/details/105489085

1
2
3
4
5
6
7
8
#from scipy.misc import imread, imsave, imresize

# Read an JPEG image into a numpy array
#img = imread('assets/cat.jpg')
import imageio
img = imageio.imread('cat.jpg')
print(img.dtype, img.shape)  # Prints "uint8 (400, 248, 3)"

uint8 (400, 248, 3)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 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.95, 0.9]

# Resize the tinted image to be 300 by 300 pixels.
from skimage.transform import resize
img_tinted = resize(img_tinted, (300, 300))

1
2
# Write the tinted image back to disk
imageio.imwrite('cat_tinted.jpg', img_tinted)
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.

Distance between points

SciPy 定义了一些用于计算点集之间距离的有用函数。

函数 scipy.spatial.distance.pdist 计算给定集合中所有点对之间的距离

类似的函数(scipy.spatial.distance.cdist)计算两组点之间的所有对之间的距离

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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, :],
#D [ i,j ]是 x [ i,: ]和 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'))
print(d)
[[0 1]
 [1 0]
 [2 0]]
[[0.         1.41421356 2.23606798]
 [1.41421356 0.         1.        ]
 [2.23606798 1.         0.        ]]

Matplotlib

Matplotlib 是一个绘图库。本节简要介绍 matplotlib.pyplot 模块,该模块提供了一个类似于 MATLAB 的绘图系统。

Plotting

Matplotlib 中最重要的函数是 plot,它允许您绘制2d 数据。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a sine curve
x = np.arange(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.

只需要一点额外的工作,我们就可以很容易地同时绘制多条线,并添加标题、图例和轴标签:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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')

# Show the figure.
plt.show()

Images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import numpy as np
import imageio
import matplotlib.pyplot as plt

img = imageio.imread('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()