1.Python
(1)基本的数据类型
Python没有单目操作x++和x--。
Booleans: 使用单词and, or, not, !=
(2)容器
Lists: [] 由数组组成,长度可以变化,可以是不同类型数据的组合。方法:append(element),pop()
list comphrehensions
Dictionaries: {key:value,key:value,....}
想获得关键字和相关的值,使用items()
dictionary comphrehensions
Sets: {},方法add(element), remove(element)
Tuples: () 不可变的,按顺序的
(3)函数
使用关键字def
(4)类
2. Numpy
(1) 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.
np.array([..])
np.zeors(())
np.ones(())
np.full((),value) # Create a constant array
np.eye()
np.random.random(())
(2) Array indexing
Slicing: A slice of an array is a view into the same data, so modifying it will modify the original array.
integer array indexing
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]"
# 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]"
(3) Datatypes
(4) Array math
elementwise:
- sum: +, np.add(x, y)
- difference: -, np.substract(x,y)
- product: *, np.multiply(x, y)
- dividion: /, np.divide(x, y)
- square root: np.sqrt(x)
matrix multiplication:
- x.dot(y) # y乘以x翻转后的值
- np.dot(x,y)
Numpy provides many useful functions for performing computations on arrays; one of the most useful is 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]"
Taking the transpose of a rank 1 array does nothing
inner product: 内积, 点乘,矩阵相乘
outer product:外积,叉乘,元素相乘
(5) Broadcasting
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]]"
The line y = x+v works even though x has shape (4, 3)
and v
has shape (3,)
due to broadcasting; this line works as if v
actually had shape (4, 3)
, where each row was a copy of v
, and the sum was performed elementwise.
也有each column上copy的
3. SciPy
(1) Image operations
对图像的读写,缩放
from scipy.misc import imread, imsave, imresize
# Read an JPEG image into a numpy array
img = imread('assets/cat.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.95, 0.9]
# 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)
(2) MATLAB files
The functions scipy.io.loadmat and scipy.io.savemat allow you to read and write MATLAB files.
(3) Distance between points
The function scipy.spatial.distance.pdist computes the distance between all pairs of points in a given set:
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'))
print(d)
4. Matplotlib
(1) Plotting
(2) Subplots
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()
(3) Images
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()