NumPy Array (4)-Two-dimensional array

Source: Internet
Author: User

Python creates a two-dimensional list by storing a list in a list:

L = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

NumPy can create a two-dimensional array directly:

Import= Np.array ([    [1,2,3,4],    [5,6,7,8],    [9,10,11,12],    [13,14,15,16]])

NumPy a two-dimensional array to get a value:

[A, b] : a for the row index, b for the column index, that is, to get the b element of line a

print l[1, 3]#  8

You can also intercept a section and make up a new NumPy array:

Print (L[1:3, 2:4])# take 第1-3 row, 第2-4 column of data  # Results: [[7  8] [11 12]]

You can also get a row or a column and get a new one-dimensional numpy array:

Print (L[1,:]) # Results: [5 6 7 8]
Print (l[:, 3]) # Results: [4 8 12 16]

The two-dimensional numpy array also has a built-in method such as mean (), STD (), which calculates all the values of the entire array, regardless of the row or columns:

Print(L.mean ()) # results: 8.5

The vectorization of two-dimensional arrays is the same:

A = Np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9= Np.array ([[1, 1, 1], [2, 2, 2], [3, 3, 3]])print (A + b) # Results: [[2  3  46  7  8] [10 11 12]]

A comprehensive chestnut:

# Suppose there are 5 subway stations for 10 days of passenger traffic data ridership = Np.array ([    [   0,    0    ,2,    5,    0],    [ 1478, 3877, 3674, 2328, 2539],    [1613, 4088, 3991, 6461, 2691],    [1560, 3392, 3826, 47 2613],    [1608, 4802, 3932, 4477, 2705],    [1576, 3933, 3909, 4979, 2685],    [ c21/>,  229,  255,  496,  201],    [   2,    0,    1,    ,    0],    [1438, 3785, 3589, 4174, 2215],    [1342, 4043, 4009, 4665, 3033]]

Find the station with the most traffic on the first day, then return the average daily passenger flow to the station and return the average daily passenger flow to all stations as a comparison:

def mean_riders_for_max_station (ridership):     = ridership[0,:].argmax ()                     # Gets the index of the largest value of the first day (line No. 0)     = ridership[:, Max_index].mean ()            # By getting the index, get the corresponding column, take the average     = Ridership.mean ()    return  (Overall_mean, Mean_for_max)    Print  (mean_riders_for_max_station (ridership))    #  results:(2342.6, 3239.9)

Add:

Pest Max_index = ridership[0,:].argmax () can also be written as Max_index = Np.argmax (Ridership,axis=1) [0]

One is the method of the array, and the other is the NumPy method, where axis represents the axis, which is followed by detail.

NumPy Array (4)-Two-dimensional array

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.