Scipy库里的众数函数不严谨,我把它重构了。

关于这个众数函数

python 复制代码
from scipy import stats
result2 = stats.mode(Tensor)

我重构后没有那个心情去和numpy原生库函数互怼,不过可以在用numpy计算众数的时候,调用我重构的这个函数。我在阅读编程书时,书上说numpy没有众数函数,观察scipy的众数函数发现的问题,我还以为是自己逻辑错误了,然后经过验证,我的数学逻辑没有问题,那就是scipy的函数出问题了,我就选择把它重构了成了一个Mode_Calculation.py文件【模块】。Codes:

python 复制代码
from collections import Counter
def Find_Mode(Array):
    vector_translate = Array.ravel()
    counts = Counter(vector_translate)
    if max(counts.values()) == 1 or len(set(vector_translate)) == 1:return None
    else:
        mode_ls = [k for k,v in counts.items() if v == max(counts.values())]
        return mode_ls

From the above Codes,Find_Mode(Array), it's one parameter about np.array(x,y,z,c,v,b,n,,,,,,,) of the function. It could solve mode number calculation questions in 1-N Dimension Array. It also solve array = \[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2] hasn't mode number question. It also solve array = np.array(i for i in range(1,100000000)) hasn't mode number question. But I couldn't find a way to add the function to numpy. Thus, you need create one Mode_Calculation.py. And you must add these codes to the file. When you use numpy to calculate mode number, you will like my below codes to achieve it:

python 复制代码
import Mode_Calculation as MC
import numpy as np
from scipy import stats

array = np.random.randint(1,10,10)
array2 = np.array([i for i in range(1,10)])
array3 = np.random.random(10)
array4 = np.random.randint(1,100,100000000)
Tensor = np.reshape(array4,(200,500,10,100))

Matrix = np.reshape(array,(2,5))
mode_result = MC.Find_Mode(Matrix)
sp_result = stats.mode(Matrix)
mode_result2 = MC.Find_Mode(array2)
sp_result2 = stats.mode(array2)
mode_result3 = MC.Find_Mode(array3)
sp_result3 = stats.mode(array3)
mode_result4 = MC.Find_Mode(Tensor)
sp_result4 = stats.mode(Tensor)
print("The Array Mode is:{}".format(mode_result))
print("sp_result:\n{}".format(sp_result))
print("mode_result2:\n{}".format(mode_result2))
print("sp_result2:\n{}".format(sp_result2))
print("mode_result3:\n{}".format(mode_result3))
print("sp_result3:\n{}".format(sp_result3))
print("mode_result4:\n{}".format(mode_result4))
print("sp_result4:\n{}".format(sp_result4))

The result:

根据scipy的原有众数函数,继续升级,既能获得众数,也能获得众数在Array数组中的总量,change the Codes like the below Codes:

python 复制代码
from collections import Counter
def Find_Mode(Array):
    vector_translate = Array.ravel()
    counts = Counter(vector_translate)
    if max(counts.values()) == 1 or len(set(vector_translate)) == 1:return None
    else:
        mode_ls = [(k,v) for k,v in counts.items() if v == max(counts.values())]
        return mode_ls

再次升级【get progress】,Mode_Calculation.py Codes:

python 复制代码
from collections import Counter
def Find_Mode(Array):
    vector_translate = Array.ravel()
    counts = Counter(vector_translate)
    if len(set(vector_translate))==1:
        return None
    max_value = max(counts.values())
    if max_value==1:
        return None
    else:
        mode_ls = [(k,v) for k,v in counts.items() if v == max_value]
        return mode_ls

Again, let it become high-level better than before.

python 复制代码
from collections import Counter
import numpy as np
def Find_Mode(Array):
    try:
        vector_translate = Array.ravel()
        counts = Counter(vector_translate)
        if len(set(vector_translate))==1:
            return np.nan
        max_value = max(counts.values())
        if max_value==1:
            return np.nan
        else:
            mode_ls = [(k,v) for k,v in counts.items() if v == max_value]
            return mode_ls
    except Exception:
        return Array

Test Codes:

python 复制代码
import Mode_Calculation as MC
import numpy as np
import time

def Test():
    m = 10
    array4 = np.random.randint(11,23,30)
    array8 = np.array([m for n in range(0,10000000)])
    array6 = np.random.randint(1,50000,10000000)
    array5 = np.reshape(array4,(2,5,3))
    array9 = np.reshape(array6,(20,500,10,100))
    array7 = np.reshape(array8,(100,50,10,10,20))
    AY1 = np.array([])
    # print("array3:\n{}".format(array5))
    mode_result = MC.Find_Mode(array5)
    mode_result2 = MC.Find_Mode(array7)
    start_time = time.time()
    mode_result3 = MC.Find_Mode(array9)
    end_time = time.time()
    print("mode_result:{}".format(mode_result))
    print("mode_result2:{}".format(mode_result2))
    test = MC.Find_Mode(AY1)
    print("mode_result3:{}".format(mode_result3))
    print("time:%.4f秒"%(end_time-start_time))
    print("test:{}".format(test))

if __name__ == "__main__":
    Test()

Result:

结论

数据要科学,不能不科学。去菜市场买菜,和菜农说"你这菜要多少阶乘的钱一斤?",菜农说"4!块钱"。这很有趣不是吗?

相关推荐
BK贝壳4 小时前
亿级用户签到位图选型复盘:用 bool-hybrid-array 扛冷启动打卡,也把它和 Roaring/Redis 的边界测清楚了
数据结构·python·性能优化·numpy
2601_966949656 小时前
如何根据研究的起止时间截取分钟级行情?Python 实战与 QuantDash 数据处理指南
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
在世修行10 小时前
智学文枢:页面切换与跨页数据预填
python·桌面应用·signal·1024程序员节·pyside·跨页跳转·状态预填
2601_9628857210 小时前
如何用 Python 把 K 线重采样成任意周期?
开发语言·python
打工仔折腾 AI10 小时前
在 Ubuntu ARM64 部署 Q1 V3.0 MuJoCo 模型:加载、单步推理与执行器响应解析
linux·运维·人工智能·python·深度学习·ubuntu·性能优化
信誓旦旦的程序猿11 小时前
【Python量化实战宝典 #21】北向资金跟随手册:聪明钱择时信号(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
严谨的麻辣烫11 小时前
静态 ISP 代理请求超时怎么排查?Python 与 curl 实战
python·http·requests·1024程序员节·网络排障
KeepRobots13 小时前
11-01 温哥华不回拨:用 60 行 Python 查你的工单系统会不会差一小时(KeepRobots 工程笔记)
python
xiao77mwq13 小时前
你以为 OCR 慢?实测 81.3 s 里全是故障代价:如何把「定格态」消掉,让耗时从 6.5 s 到 13 倍差距显出原形
python
深圳市益普科技有限公司13 小时前
半导体良率管理YMS:良率与设备参数的相关性,先拉哪几组数据
人工智能·python·算法