鱼C论坛

 找回密码
 立即注册
查看: 2678|回复: 2

上次发帖没人帮助 这次大家一定要理我啊 ##python 关于数据分组的的一个error

[复制链接]
发表于 2017-8-24 18:00:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
要求写一个程序,对values进行类似于直方图的数据分组,dividers是分组的节点,例如
values=(2.09, 0.5, 3.48, 1.44, 5.2, 2.86, 2.62, 6.31)
dividers=(2,4)
那么最后就会输出一个array([2,4,2])
大家可以看我的程序


def histogram(values,dividers):
    b=np.array([0])
    a=b.repeat(len(dividers)+1)
    for index1 in range(0,len(values)):
        if values[index1] <= dividers[0]:
            a[0] += 1
        else:
            if values[index1] > dividers[len(dividers)-1]:
                a[len(dividers)] += 1
            else:
                for index2 in range(0,len(dividers)-1):
                    if dividers[index2] < values[index1] <= dividers[index2+1]:
                        a[index2+1] += 1

老师提供了一个验证程序的方法:
import numpy.random as rnd
values = rnd.normal(0, 1, 50)
import numpy as np
range = np.max(values) - np.min(values)
dividers = (np.arange(1, 10) * (range / 10)) + np.min(values)  

Traceback (most recent call last):

  File "<ipython-input-21-d45162dc8553>", line 1, in <module>
    histogram(a,b)

  File "<ipython-input-16-792a88f84037>", line 4, in histogram
    for index1 in range(0,len(values)):

TypeError: 'numpy.float64' object is not callable

出现这个错误是什么情况

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-24 19:46:32 | 显示全部楼层
这个错误是类型错误,从错误报告应该是在你的代码第4行出错
你的代码排版有点乱 ,我不知到哪个是第4行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-24 20:55:48 | 显示全部楼层
  1. values=[2.09, 0.5, 3.48, 1.44, 5.2, 2.86, 2.62, 6.31]
  2. dividers=[2,4]
  3. def histogram(values,dividers):
  4.         return [len([i for i in values if i<dividers[0]]), len([i for i in values if dividers[0]<=i<dividers[1]]), len([i for i in values if i>=dividers[1]])]
  5. print(histogram(values, dividers))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 11:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表