Beginner NumPy (iii)

Source: Internet
Author: User
Tags pprint

Boolean index

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 " "4 Description: Beginner NumPy (iii)--Boolean index5 Created on October 8, 20156 @author: Zenwan7 @version:8 " "9 ImportNumPy as NPTen  fromPprintImportPprint One  Anames = Np.array (['Beijing','Anhui','Shanghai','Jiangsu','Zhejiang','Shanghai','Anhui'])#There are duplicates -data = Np.random.randn (7,4)#generate random numbers for normal distributions - pprint (names) the pprint (data) -  - " " - suppose each name in the names corresponds to a row in the data array, and we want to select all the rows that correspond to the name ' Shanghai '.  + we want to select all the data rows that correspond to ' Shanghai '. - As with arithmetic operations, the array's comparison operations (such as = =) are also vectorized, so the array names and the string ' Shanghai ' comparison operations produce a Boolean array arr_bool, + This type of Boolean array can be used for the group index A " " atArr0_bool = (Names = ='Shanghai')#array-to-string comparison operations produce a Boolean array -Pprint (Arr0_bool)#array ([False, False, True, False, False, True, false], Dtype=bool) - Pprint (Data[arr0_bool]) -  - #Mix Boolean arrays with slices, integers, integer sequences, and more -Pprint (Data[names = ='Shanghai', 2:]) inPprint (Data[names = ='Shanghai', 3]) -  to #Select other combinations of values other than ' Shanghai ' +Arr1_bool = (Names! ='Shanghai') -Arr2_bool = ((Names! ='Shanghai')| (names=='Jiangsu')) theArr2_bool = ((Names! ='Shanghai') & (names=='Jiangsu')) *  $ #set all positive numbers in data to-1Panax NotoginsengData[data > 0] = 1#The comparison operation of the array is vectorized - pprint (data) the  + #set a certificate row or column through a one-dimensional Boolean array, such as setting the entire row of data for ' Shanghai ' to 0 Adata [names = ='Shanghai'] =0 thePprint (data)

Operation Result:

1Array (['Beijing','Anhui','Shanghai','Jiangsu','Zhejiang','Shanghai',2        'Anhui'], 3Dtype='| S8')4Array ([[[0.84608482, 0.51050985,-0.43504218,-1.08530829],5[0.51865817,-1.0914367,-0.6953841,-1.59947846],6[-0.9308739,-0.2748063,-0.410821,-1.66078485],7[0.2290689,-0.85357479,-0.42896687, 0.97397006],8[-0.60247776,-0.24399895,-1.03297297,-0.76709796],9[-0.31226921,-0.42020231, 1.12008391, 0.54366616],Ten[0.53271514, 0.50497622, 1.02203728,-0.12969158]]) OneArray ([False, False, True, False, False, True, false], dtype=bool) AArray ([[-0.9308739,-0.2748063,-0.410821,-1.66078485], -[-0.31226921,-0.42020231, 1.12008391, 0.54366616]]) -Array ([[-0.410821,-1.66078485], the[1.12008391, 0.54366616]]) -Array ([-1.66078485, 0.54366616]) -Array ([[-1].        ,-1. ,-0.43504218,-1.08530829], -[-1. ,-1.0914367,-0.6953841,-1.59947846], +[-0.9308739,-0.2748063,-0.410821,-1.66078485], -[-1. ,-0.85357479,-0.42896687,-1. ], +[-0.60247776,-0.24399895,-1.03297297,-0.76709796], A[-0.31226921,-0.42020231,-1. ,-1. ], at[-1.        ,-1.        ,-1. ,-0.12969158]]) -Array ([[-1].        ,-1. ,-0.43504218,-1.08530829], -[-1. ,-1.0914367,-0.6953841,-1.59947846], - [0., 0.        , 0.        , 0. ], -[-1. ,-0.85357479,-0.42896687,-1. ], -[-0.60247776,-0.24399895,-1.03297297,-0.76709796], in [0., 0.        , 0.        , 0. ], -[-1.        ,-1.        ,-1. ,-0.12969158]])

Beginner NumPy (iii)

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.