Python Numpy Tutorials: 数组--3

来源:互联网 发布:天猫与淘宝的盈利方式 编辑:程序博客网 时间:2024/06/03 17:16
# -*- coding: utf-8 -*-"""Python Version: 3.5Created on Thu May 11 11:18:49 2017E-mail: Eric2014_Lv@sjtu.edu.cn@author: DidiLv"""import numpy as npa = 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 exampleprint(np.array([a[0, 1], a[0, 1]]))  # Prints "[2 2]"# Create a new array from which we will select elementsa = 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 indicesb = np.array([0, 2, 0, 1])# Select one element from each row of a using the indices in bprint(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"# Mutate one element from each row of a using the indices in ba[np.arange(4), b] += 10print(a)  # prints "array([[11,  2,  3],         #                [ 4,  5, 16],         #                [17,  8,  9],         #                [10, 21, 12]])
0 0