NumPy Learning (2): operation of arrays

Source: Internet
Author: User

1. operation of a simple one-dimensional array

The operation of a one-dimensional array is similar to the list type of Python itself.

In []: arr = Np.arange (+) in [arrout[]: array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Sixth element value in [
    16]: arr[5]out[]: 5# 6,,7,8 The value of this three element in []: arr[5:8]out[+]: array ([5, 6, 7])

  Note: Unlike the list type, when you manipulate the subsequence of the original array, it is actually manipulating the data of the original array. This means that the data in the array is not copied, and any operations on its subsequence are mapped to the original array. This is because NumPy is a tool designed to handle large amounts of data, and its computational performance is greatly discounted if Replicated.

In [+]: Arr_slice = Arr[:4]in [+]: arr_slice[:] =[+]: arr_sliceout[]: array ([20, 20, (+])# arr_slice changes mapped to arr in [Max]: arrout[]: array ([4],  5,  6,  7,  8,  9])

  If you want to perform a copy operation that is displayed, you can use the Copy () function with the bar: arr[:3].copy

2. Operations on a high-dimensional array

2.1 Two-dimensional array index access:

# generates 1-9 of the number
In [+]: arr2d = Np.arange (1,10). reshape ((3,3)) in []: arr2dout[]: array ([[[1, 2, 3],< c5/>[4, 5, 6], [7, 8, 9]]# Access the first row of data in [: arr2d[0]out[]: Array ([1, 2 , 3])# Access the first row the second data in [to]: arr2d[0][1]out[]: 2 in[+]: arr2d[0,1] out[32]: 2

2.2 to divide (slice) to obtain part of the Data:

  

  2.3 Differences in indexing (index) access and partitioning (slice)

    An index is the location of the data through coordinates and gets its value, arr2d[[0,1],[0,1]] is actually getting (0,0), the data at the () location, is the data on the fetch Junction.

Partitioning is to extract the set range of data, arr2d[:2,: 2] in fact, 1-2 rows and 1-2 columns in the data is divided, is a region.

In []: arr2d = Np.arange (1,10). reshape (3,3)
# Divide the data in a divided way
In []: arr2d[:2,: 2]out[:array ([[[1, 2], [4, 5]])# Accessing data in an indexed way [ "arr2d[[0,1],[0,1]]out[": array ([1, 5])

The 1,3,5,7 rows, and 4,3,2,1 columns of a two-dimensional array of 8 * 4, are re-formed into a two-dimensional array. If you use the following methods, it is certainly not possible:

In []: arr = np.arange (+). reshape (8,4) in []: arrout[]: array ([[[0,  1,  2,  3],        4,  5,  6,  7],       8,  9, ten, one],       [all, +, +],       [16 , (+), (       +), [+], [+], [+], [+], [       +], [+], []       ] # The wrong approach is actually to get (0,3), (2,2), (4,1), (6,0) The value on the position In [arr[[0,2,4,6],[3,2,1]: "0]]out[": array ([3, 10, 17, 24])

The right way:

# Get data for 1,3,5,7 rows first
In [the]: arr_temp = arr[[0,2,4,6]]in [[]: arr_tempout[]: array ([[[0, 1, 2, 3 ], 8, 9, ten, one], [[+], [+], [+], [+, +]]]# Divide the array , and make up the new array in [the]: arr_temp[:,[3,2,1, 0]]out[3, 2, 1, 0], [one, ten, 9, 8], [27, 26, 25, 24]]

You can also invoke the Np.ix_ () function by implementing this function:

In [all]: arr[np.ix_ ([0,2,4,6],[3,2,1, 0])]out[3,  2,  1,  0],       [11, 10,  9,  8],       [27, 26, 25, 24       ]])

2.4 high-dimensional Array Access:

# Generate three-dimensional arrays with reshape
In [[]: Arr3d = Np.arange (1,13). reshape ((2,2,3)) in [34]: arr3dout[34]: Array ( [[[[1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]] )# Create a three-dimensional array in the traditional array function [: Arr3d = Np.array ([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]] ) in [36]: arr3dout[36]: Array ( [[[[1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]] )# arr3d[0] is an array of 2*3 in [37]: arr3d[0]out[37]: Array ([[1, 2, 3], [4, 5, 6] ] # arr3d[0,0] is a one-dimensional array in [38]: arr3d[0,0]out[: Array ([1, 2, 3]) in [39]: arr3d[0,0,0]out[39]: 1

2.5 Array Assignment Operation:

# The value of the first 2 * 3 array becomes all 32
in [max]: arr3d[0] = [+]:arr3dout[]: Array ([[[] [[[]], [[]], [+], 32 ]], 7, 8, 9 ], [ten, one, a]] ]) in []: old_values = arr3d[0]# Assigns an array of 2 to arr3d[0] in [a]: arr3d[0] = old_valuesin [] : arr3dout[ ]: Array ( [[[7], 8, 9 ], [10, 11, 12], [[] ]]])


3. Boolean Index
Data preparation:

# of seven Names
In [2]: names = Np.array (['Bob','Joe',' would','Bob',' would','Joe','Joe']) in [3]: namesout[3]: Array (['Bob','Joe',' would','Bob',' would','Joe','Joe'], Dtype='| S4'# Generates a random array of 7 rows, 4 columns, and each row corresponds to a Person's data in the names in [4]: data = Randn (7,4) in [5]: dataout[5]: Array ([[0.54643196, 0.98876451, 1.55499825, 1.88240798], [ 1.39695977,-1.59576937, 0.42445372, 1.73588157], [ 0.61992879,-0.13706965, 1.87869165, 1.28724149], [ 0.59114346,-0.50032077, 1.29182197, 0.54644327], [ 0.59344412,-0.06566168,-0.01759809, 0.22191015], [-1.57663889,-0.08607805,-0.36048361,-0.94029737], [-0.32203015, 0.59462972, 1.10737098,-1.9473386]])

Filter the rows of a two-dimensional array with a Boolean array, the size of the Boolean array and the number of rows in the two-dimensional array


In []: names=="Bob"out[]: array ([true, false, false, true, false, false, False], dtype=Bool)
# pick Bob's data In ["Bob"]out[0.54643196, 0.98876451, 1.55499825, 1.88240798], 0.59114346, -0.50032077, 1.29182197, 0.54644327])

Boolean index and normal index mixed use:

" Bob " ]out[0.54643196,  0.98876451,  1.55499825,  1.88240798],       0.59114346, -0.50032077,  1.29182197,  0.54644327]) in ["Bob", 2:] out[1.55499825,  1.88240798],       1.29182197,  0.54644327])

"with or non" Operation. The and , or, and other keywords in Python cannot be implemented Here.

# or action
In [+]: (names = = ' Bob ') | (names = = ' 'll ')
Out[20]: array ([true, false, true, true, true, false, false], Dtype=bool)
# with operations
In [+]: (names = = ' Bob ') & (names = = ' would ')
Out[21]: array ([false, false, false, false, false, false, false], Dtype=bool)
# There are two types of non-operational
In []: names! = "Bob"
Out[22]: array ([false, true, true, false, true, true, true], Dtype=bool)

in [23°c]:-(names = = "Bob")
Out[23]: array ([false, true, true, false, true, true, true], Dtype=bool)


Two-dimensional Boolean type index:

In []: Data <0out[24]: array ([[false, false, false, false], [false, true, false, false], [false, true, false, false],  [false, true, false, false], [false, true, true, false], [true, true, true, true], [true, false, False, True]], Dtype=Bool) # Change the value of elements less than 0 to 0 in []: Data[data < 0] =0In [26]: dataout[26]: Array ([[0.54643196, 0.98876451, 1.55499825, 1.88240798],       [ 1.39695977, 0. , 0.42445372, 1.73588157],       [ 0.61992879, 0. , 1.87869165, 1.28724149],       [ 0.59114346, 0. , 1.29182197, 0.54644327],       [ 0.59344412, 0.        , 0. , 0.22191015], [0.        , 0.        , 0.        , 0.        ], [0. ,  0.59462972, 1.10737098, 0. ]])

 

NumPy Learning (2): operation of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.