numpy学习笔记
Posted shadowwalker9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy学习笔记相关的知识,希望对你有一定的参考价值。
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]])
以上是关于numpy学习笔记的主要内容,如果未能解决你的问题,请参考以下文章