Python Forum
typeerror, building histogram from data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
typeerror, building histogram from data
#5
So i tried that and also moved diff out, but now it goes back to the original error i got. Now i have already converted the list to integer, since the original list is made of 50000 datapoints which have data with decimals. not sure what's going on!

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from helper import plotHisto

data = np.loadtxt('math_scores.txt')
def myMap(inp, f) :
    res = []
    for i in inp :
        res.append(f(i))
    return res

def new(x) : return int(x)
pdata = myMap(data, new)

def buildHisto(pdata, numbins, minimum, maximum) :   
    histo = []
    binwidth = diff/numbins
    for d in pdata :
        histo[d//binwidth]+=1
    return histo

minimum = min(pdata)
maximum = max(pdata)
diff = maximum - minimum

histo = buildHisto(pdata, 10, minimum, maximum)
plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True)
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-36-a1b9b926837f> in <module>() 26 27 ---> 28 histo = buildHisto(pdata, 10, minimum, maximum) 29 plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True) <ipython-input-36-a1b9b926837f> in buildHisto(pdata, numbins, minimum, maximum) 18 binwidth = diff/numbins 19 for d in pdata : ---> 20 histo[d//binwidth]+=1 21 return histo 22 TypeError: list indices must be integers or slices, not float
Reply


Messages In This Thread
RE: typeerror, building histogram from data - by newatpython11 - Jul-17-2019, 02:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 994 May-15-2023, 01:45 PM
Last Post: deanhystad
  Plotting histogram of dataframe column Mark17 4 2,715 Jul-30-2020, 09:52 AM
Last Post: Mark17
  Can someone find out what kind of histogram is it? J_tin 1 1,836 Apr-26-2020, 05:23 PM
Last Post: buran
  Histogram using pandas dataframe not showing proper output ift38375 1 2,245 Jul-04-2019, 10:43 PM
Last Post: scidam
  Histogram and text file. pawlo392 1 4,172 May-24-2019, 03:14 AM
Last Post: heiner55
  how do i use get for my histogram function? pseudo 4 4,900 Oct-14-2018, 04:23 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020