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!块钱"。这很有趣不是吗?

相关推荐
承渊政道3 分钟前
【Python编程—从入门到实践】(设置样式并部署:给Django网站换新装,把学习笔记发布到互联网)
python·学习·pycharm·django
用户2986985301429 分钟前
使用 Python 将 JSON 转换为 Word 文档的实践指南
python·json·api
卷无止境1 小时前
AI 加速开发之后,如何应对尚未发现的代码安全威胁
后端·python
cpolar技术支持1 小时前
容器数是 6 个、6 个、还是 6 个,可服务早被换掉了:用一个 5.5MB 的 Go 单文件把机器每周的变化记下来,再给它套一层别人看不懂的入口
python·docker·ai·cpolar
ONEYAC唯样1 小时前
YXC扬兴科技小型化有源晶振OT4JI-111-24M,提升红外热成像稳定性
人工智能·python·pygame
威哥爱编程2 小时前
模拟面试智能体开发实战 09:把面试官装进手机,鸿蒙端聊天界面用 SSE 流式输出让回复逐字滚出来
python·agent·harmonyos
威哥爱编程2 小时前
模拟面试智能体开发实战 10:配置写死在代码里还怎么换服务端,把设置和简历做成界面可配、可存
python·agent·harmonyos
青少儿编程课堂2 小时前
第十八届蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会图形化考级:校园三消乐大挑战解析
c++·python·算法·bfs·信息学竞赛
青少儿编程课堂2 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:网格连通块与岛屿数量(DFS+BFS双解)
c++·python·算法·bfs·信息学竞赛
M--Y12 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph