數據挖掘工具numpy(四)Numpy數據篩選運算

一,numpy中數值的修改

import numpy as np

temp = np.arange(30).reshape(6,5)
print(temp)
temp[:,1] = 0
print(temp)

# -------------output---------------------
[[ 0  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]]
 
[[ 0  0  2  3  4]
 [ 5  0  7  8  9]
 [10  0 12 13 14]
 [15  0 17 18 19]
 [20  0 22 23 24]
 [25  0 27 28 29]]

二,布爾索引

import numpy as np

temp = np.arange(30).reshape(6,5)
print(temp)
temp[temp<10] = 3
print(temp)
temp[np.logical_or(temp<3,temp>15)] = 0
print(temp)
temp[np.logical_and(temp>3,temp<15)] = 0
print(temp)

# -------------output---------------------
[[ 0  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]]
[[ 3  3  3  3  3]
 [ 3  3  3  3  3]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]]
[[ 3  3  3  3  3]
 [ 3  3  3  3  3]
 [10 11 12 13 14]
 [15  0  0  0  0]
 [ 0  0  0  0  0]
 [ 0  0  0  0  0]]
[[ 3  3  3  3  3]
 [ 3  3  3  3  3]
 [ 0  0  0  0  0]
 [15  0  0  0  0]
 [ 0  0  0  0  0]
 [ 0  0  0  0  0]]

三,三元運算符where

1,where語句
import numpy as np

temp = np.arange(30).reshape(6,5)
print(temp)
temp = np.where(temp<10,0,11)
print(temp)

# -------------output---------------------
[[ 0  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]]
 
[[ 0  0  0  0  0]
 [ 0  0  0  0  0]
 [11 11 11 11 11]
 [11 11 11 11 11]
 [11 11 11 11 11]
 [11 11 11 11 11]]
2,python的語法中的三元運算符
# python的語法中的三元運算符
a = 3 if 3>4 else 2
print(a)
3,logical_and和np.logical_or
符合邏輯需要結合np.logical_and和np.logical_or使用
import numpy as np

temp = np.arange(15).reshape(3,5)
print(temp)
print(np.where(temp<6,1,0))
print(np.where(np.logical_and( temp > 3, temp < 6), 1, 0))
print(np.where(np.logical_or( temp < 3, temp > 6), 1, 0))

# -------------output---------------------
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
 
[[1 1 1 1 1]
 [1 0 0 0 0]
 [0 0 0 0 0]]
 
[[0 0 0 0 1]
 [1 0 0 0 0]
 [0 0 0 0 0]]
 
[[1 1 1 0 0]
 [0 0 1 1 1]
 [1 1 1 1 1]]

四,通用判斷函數

1,numpy.all和numpy.any
import numpy as np

t = np.arange(30).reshape(6,5)
print(t[0:2, 0:3])
print('-'*50)
print(np.all(t[0:2, 0:3]))  # 取
print(np.any(t[0:2, 0:3]))  # 取
print('-'*50)
print(np.all(t[0:2, 0:3]>3))
print(np.any(t[0:2, 0:3]>3))

# -------------output---------------------
[[0 1 2]
 [5 6 7]]
--------------------------------------------------
False
True
--------------------------------------------------
False
True
2,numpy.unique
import numpy as np

t = np.arange(16).clip(5,8).reshape(4,4)
print(t)
print('-'*50)

# 將序列中數值值唯一且不重複的值組成新的序列
change_int = t.astype(int)
print(np.unique(change_int ))

# -------------output---------------------
[[5 5 5 5]
 [5 5 6 7]
 [8 8 8 8]
 [8 8 8 8]]
--------------------------------------------------
[5 6 7 8]

五,clip裁剪

1,將數組中的數據設置爲np.nan
import numpy as np

# 由於nan爲float,需要先將數組數據改爲float格式
temp = np.arange(30).reshape(6,5).astype(float)
print(temp)
temp[3,2] = np.nan
print(temp)
2,將數組進行裁剪
import numpy as np

temp = np.arange(30).reshape(6,5).astype(float)
# np.nan與任何元素進行計算都爲nan
temp[0,3] = np.nan
print(temp)
temp = temp.clip(10,18)
print(temp)

# -------------output---------------------
[[ 0.  1.  2. nan  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.]]
[[10. 10. 10. nan 10.]
 [10. 10. 10. 10. 10.]
 [10. 11. 12. 13. 14.]
 [15. 16. 17. 18. 18.]
 [18. 18. 18. 18. 18.]
 [18. 18. 18. 18. 18.]]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章