数据挖掘工具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.]]
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值