#numpy_tutorial

import numpy as np
print('数组尺寸$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s=np.array([1,2,3])
print(s.shape)
print(s[0],s[1],s[2])
s1=np.array([[2,3,4],[4,5,6]])
print(s1.shape)
print(s1[0,0],s1[0,1])
print('特定矩阵$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s2=np.eye(2)
print(s2)
s3=np.ones((1,2))
print(s3)
s4=np.full((2,2),8)
print(s4)
s5=np.zeros((2,3))
print(s5)
s6=np.random.random((3,3))
print(s6)
print('矩阵切片$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s7=np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
s8=s7[:2,1:3]
print(s8)
s9_firstRowOfs7_v1=s7[0,:]
s9__firstRowOfs7_v2=s7[:1,:]
print(s9_firstRowOfs7_v1, s9_firstRowOfs7_v1.shape)
print(s9__firstRowOfs7_v2, s9__firstRowOfs7_v2.shape)
s10_firstColumnOfs7_v1=s7[:,0]
s10_firstColumnOfs7_v2=s7[:,:1]
print(s10_firstColumnOfs7_v1,s10_firstColumnOfs7_v1.shape)
print(s10_firstColumnOfs7_v2,s10_firstColumnOfs7_v2.shape)
print('矩阵包含其他矩阵元素$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s11=np.array([[1,2], [3, 4], [5, 6]])
print(s11[[0, 1, 2], [0, 1, 0]])
print(np.array([s11[0, 0], s11[1, 1], s11[2, 0]]))
print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s12=np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
s13=np.array([0,1,2,1])
# Select one element from each row of a using the indices in b
print(s12[np.arange(4),s13])#在s12中四行行列式中选出列下标为s13中对应元素的元素值之集合
s12[np.arange(4),s13]+=10
print(s12)
print('索引限定$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
s14 = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (s14 > 2)
print(bool_idx)
print(s14[bool_idx])
print(s14[s14>2])
print('指定数据类型$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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"
print('矩阵运算$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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))
print(x - y)
print(np.subtract(x, y))
print(x * y)
print(np.multiply(x, y))
print(x / y)
print(np.divide(x, y))
print(np.sqrt(x))
print('矩阵相乘$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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))
print(np.dot(x, v))
# Matrix / matrix product; both produce the rank 2 array
print(x.dot(y))
print(np.dot(x, y))
print('按行求和/按列求和$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
x = np.array([[1,2],[3,4]])
print(np.sum(x))# Compute sum of all elements
print(np.sum(x, axis=0))  # Compute sum of each column; output "[4 6]"
print(np.sum(x, axis=1))  # Compute sum of each row; output "[3 7]"
print('转置矩阵$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
x = np.array([[1,2,4], [3,4,24],[44,34,0]])
print(x)
print(x.T)
print('按尺寸的矩阵复制$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)   # Create an empty matrix with the same shape as x
for i in range(4):
    y[i, :] = x[i, :] + v# Add the vector v to each row of the matrix x with an explicit loop
print(y)
print('矩阵叠加$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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
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]]"
print('特殊矩阵相加$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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]]"
print('矩阵尺寸再规划$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
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 column 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)
数组尺寸$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
(3,)
1 2 3
(2, 3)
2 3
特定矩阵$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[1. 0.]
 [0. 1.]]
[[1. 1.]]
[[8 8]
 [8 8]]
[[0. 0. 0.]
 [0. 0. 0.]]
[[0.48948673 0.93655708 0.32162858]
 [0.65390705 0.77466667 0.33364625]
 [0.40735409 0.95101094 0.60764559]]
矩阵切片$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[2 3]
 [6 7]]
[1 2 3 4] (4,)
[[1 2 3 4]] (1, 4)
[1 5 9] (3,)
[[1]
 [5]
 [9]] (3, 1)
矩阵包含其他矩阵元素$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[1 4 5]
[1 4 5]
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[ 1  5  9 11]
[[11  2  3]
 [ 4 15  6]
 [ 7  8 19]
 [10 21 12]]
索引限定$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[False False]
 [ True  True]
 [ True  True]]
[3 4 5 6]
[3 4 5 6]
指定数据类型$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int32
float64
int64
矩阵运算$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[ 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.        ]]
矩阵相乘$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
219
219
[29 67]
[29 67]
[[19 22]
 [43 50]]
[[19 22]
 [43 50]]
按行求和/按列求和$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
10
[4 6]
[3 7]
转置矩阵$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[ 1  2  4]
 [ 3  4 24]
 [44 34  0]]
[[ 1  3 44]
 [ 2  4 34]
 [ 4 24  0]]
按尺寸的矩阵复制$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[ 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]]
特殊矩阵相加$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]
矩阵尺寸再规划$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[[ 4  5]
 [ 8 10]
 [12 15]]
[[2 4 6]
 [5 7 9]]
[[ 5  6  7]
 [ 9 10 11]]
[[ 5  6  7]
 [ 9 10 11]]
[[ 2  4  6]
 [ 8 10 12]]

Process finished with exit code 0

 

上一篇:python tutorial


下一篇:2月4日学习记录