利用python 學習資料分析 (學習二)
內容學習自:
Python for Data Analysis, 2nd Edition
就是這本
純英文學的很累,對不對取決於百度翻譯了
前情提要:
各種方法貼:
https://www.cnblogs.com/baili-luoyun/p/10250177.html
本內容主要講的是:
繼續陣列和向量
一:花式索引
定義:花式索引指的是利用整數進行索引,
假設我們有一個 8 *4的陣列
arr = np.empty([8,4]) print(arr)#傳入的元祖或者列表for i in range(8): arr[i] =i print(arr)
>>>>>
[[4.67296746e-307 1.69121096e-306 1.29061074e-306 1.69119873e-306]
[1.78019082e-306 3.56043054e-307 7.56595733e-307 1.60216183e-306]
[8.45596650e-307 1.86918699e-306 1.78020169e-306 6.23054633e-307]
[1.95821439e-306 8.01097889e-307 1.78020169e-306 7.56601165e-307]
[1.02359984e-306 1.29060531e-306 1.24611741e-306 1.11261027e-306]
[7.56591659e-307 1.33511290e-306 6.89804133e-307 1.20160711e-306]
[6.89806849e-307 8.34446411e-308 1.22383391e-307 1.33511562e-306]
[1.42410974e-306 1.00132228e-307 1.33511969e-306 2.18568966e-312]]
>>>>> 賦值轉換成這個
[[0. 0. 0. 0.]
[1. 1. 1. 1.]
[2. 2. 2. 2.]
[3. 3. 3. 3.]
[4. 4. 4. 4.]
[5. 5. 5. 5.]
[6. 6. 6. 6.]
[7. 7. 7. 7.]]
然後我拿 [3,4,5,4] 行
arr1 =arr[[3,4,5,4]] print(arr1) >>>>> [[0. 0. 0. 0.] [1. 1. 1. 1.] [2. 2. 2. 2.] [3. 3. 3. 3.] [4. 4. 4. 4.] [5. 5. 5. 5.] [6. 6. 6. 6.] [7. 7. 7. 7.]] >>>> [3,4,5,4] 拿3 4 5 4 行 [[3. 3. 3. 3.] [4. 4. 4. 4.] [5. 5. 5. 5.] [4. 4. 4. 4.]]
可以按照索引倒著拿 這次我拿 -1 -2 -3
arr2 =arr[[-1,-2,-3]] print(arr2) >>>>>>>>>>>> [[0. 0. 0. 0.] [1. 1. 1. 1.] [2. 2. 2. 2.] [3. 3. 3. 3.] [4. 4. 4. 4.] [5. 5. 5. 5.] [6. 6. 6. 6.] [7. 7. 7. 7.]] >>>>>>>>>> [[7. 7. 7. 7.] [6. 6. 6. 6.] [5. 5. 5. 5.]]
多維陣列中拿到具體某行某列中的值
:>1 索引
:拿第1 ,4,3,2 行之後 ,拿第一行的0索引,第4行的第4個索引....
arr1 =np.arange(1,31).reshape(6,5) #指定範圍1,31 內的 以6行5列顯示 l1 =arr1[[1,4,3,2],[0,4,3,4]] print(arr1) print(l1)
>>>>>>
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]]
>>>>>>>>
[ 6 25 19 15]
>2:切片