标签:sha 使用 表示 each 数组下标 下标 nbsp 切片 print
1. numpy切片和整型数组访问
(1)
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
b = a[1,:] b.shape = (4,), b = [1,2,3,4]
c = a[1:2, :], c.shape = (1,4), c = [[1,2,3,4]]
(2)
数组下标,可以使用数组来表示
a = np.array([[1,2], [3, 4], [5, 6]])
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
a = np.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]])
标签:sha 使用 表示 each 数组下标 下标 nbsp 切片 print
原文地址:http://www.cnblogs.com/shadowwalker9/p/7629972.html