【问题标题】:Binning data (scatter plot) in python?python中的分箱数据(散点图​​)?
【发布时间】:2017-09-07 17:45:23
【问题描述】:

我得到了一个 Volume(x-axis) 与 Price(dMidP,y-axis) 散点图的散点图,我想将 x 轴分成 30 个均匀分布的部分并对值进行平均,然后绘制平均值价值

这是我的数据:

我的代码没有返回我想要的情节:

V_norm = Average_Buy['Volume_norm']
df = pd.DataFrame({'X' : np.log(Average_Buy['Volume_norm']), 'Y' : Average_Buy['dMidP']})  #we build a dataframe from the data
total_bins = 30
bins = np.geomspace(V_norm.min(), V_norm.max(), total_bins)
data_cut = pd.cut(df.X,bins)         
grp = df.groupby(by = data_cut)        #we group the data by the cut
ret = grp.aggregate(np.mean)         #we produce an aggregate representation (median) of each bin
plt.loglog(np.log(Average_Buy['Volume_norm']),Average_Buy['dMidP'],'o')
plt.loglog(ret.X,ret.Y,'r-')

plt.show()

这是我得到的:

我的垃圾箱返回我:(看起来正确)

array([ 0.59101371,  0.64421962,  0.70221538,  0.76543219,  0.83434009,
    0.90945141,  0.99132461,  1.08056843,  1.17784641,  1.28388183,
    1.39946306,  1.52544948,  1.6627778 ,  1.81246908,  1.97563628,
    2.15349259,  2.34736038,  2.55868108,  2.7890259 ,  3.04010746,
    3.3137926 ,  3.61211619,  3.93729631,  4.29175071,  4.67811481,
    5.09926127,  5.55832137,  6.05870826,  6.6041424 ,  7.19867916])

但是,我的 data_cut 返回了我:

Time  Time
11    0                  NaN
      1                  NaN
      2                  NaN
      3                  NaN
      4                  NaN
      5                  NaN
      6                  NaN
      7                  NaN
      8                  NaN
      9                  NaN
      10      (0.991, 1.081]
      11                 NaN
      12                 NaN
      13                 NaN
      14                 NaN
      15                 NaN
      16                 NaN
      17                 NaN
      18                 NaN
      19                 NaN
      20                 NaN
      21                 NaN
      22                 NaN
      23                 NaN
      24                 NaN
      25                 NaN
      26                 NaN
      27                 NaN
      28                 NaN
      29                 NaN
                   ...      
14    30                 NaN
      31                 NaN
      32                 NaN
      33                 NaN
      34                 NaN
      35                 NaN
      36                 NaN
      37                 NaN
      38                 NaN
      39                 NaN
      40                 NaN
      41                 NaN
      42                 NaN
      43                 NaN
      44                 NaN
      45                 NaN
      46                 NaN
      47                 NaN
      48                 NaN
      49                 NaN
      50                 NaN
      51                 NaN
      52                 NaN
      53                 NaN
      54                 NaN
      55                 NaN
      56                 NaN
      57                 NaN
      58                 NaN
      59                 NaN

【问题讨论】:

标签: python


【解决方案1】:

您的 bins 变量不是您想要的。要么将 bins 从对数空间反向转换回线性空间,要么从一开始就在线性空间中获得对数间距的 bin:

bins = np.geomspace(Volume.min(), Volume.max(), total_bins)

编辑:将 np.logspace 更改为 np.geomspace

【讨论】:

  • 谢谢,但是当我将此代码包含在 total_bins=100 中时,我收到一条错误消息,提示 Bin 边缘必须是唯一的
  • 请注意,我将答案从 np.logspace 更改为 np.geomspace(start 和 stop 中的 np.logspace 不是我认为的那样;np.geomspace 做了直观的事情) .如果问题仍然存在,请发布bins 的值(以及音量的最小值/最大值)。
  • 图形发生变化,但看起来也不正确。 bin:数组([4.5099612E-03,1.79450189E-02,7.14027653E-02,2.84109754E-01,1.13046535E+00,4.49809235E 1.12749030e+03]);最小体积 = 0.0045099612158282188; Volume max= 1127(所以范围是正确的)
  • 但请查看问题的更新,包括此代码的问题
  • 嗨 Paul,如果开始为负数,则 np.geomspace 不起作用 (geomspace(np.log(Volume.min()), np.log(Volume.max()), total_bins))
猜你喜欢
  • 2017-11-16
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 2014-03-31
  • 2014-03-09
相关资源
最近更新 更多