Python Forum
typeerror, building histogram from data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
typeerror, building histogram from data
#1
I am trying to run a code to build histogram from a list of data and keep getting the below error :

Error:
TypeError: list indices must be integers or slices, not float
Below is my code, can you help me with what i am doing wrong?

%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) :

    minimum = min(pdata)
    maximum = max(pdata)
    diff = maximum - minimum
    binwidth = diff/numbins
    histo = []
    for d in pdata :
        histo[d//binwidth]+=1
    return histo

histo = buildHisto(pdata, 10, minimum, maximum)
plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True)
Reply
#2
Please post the full error message. It has information we need.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Sorry about the miss, here is the error!

Error:
NameError Traceback (most recent call last) <ipython-input-33-322737c63f20> in <module>() 24 return histo 25 ---> 26 histo = buildHisto(pdata, 10, minimum, maximum) 27 plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True) NameError: name 'minimum' is not defined
Reply
#4
Okay, that's not the error you said you had, but we'll just slide past that. You only define (assign a value to) minimum in the function buildHisto. That definition is not available outside the function, such as when you call the function on line 27 (26?). Now, you also use minimum for plotHisto. So I would recommend moving the definitions of minimum and maximum outside the function by moving lines 18 and 19 to before line 27 (26?), and unindenting them one level. You might also review functions and scope to better understand the problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
#6
binwidth is calculated with normal division, it's probably a float. Note that // only returns an integer if both of it's arguments are integers. You appear to be forcing pdata to ints, you either need to force binwidth to an int or force d/binwidth to an int.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
gotcha!So i made the below 2 changes by adding int at 2 places. Now both the arguments are integers when using // :
binwidth = int(diff/numbins)
histo[int(d)//binwidth]+=1

I also removed the part where i am mapping the list to a list of integers. Basically the sample dataset has exam scores from 0 to 100. I still get an error as below.

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

def buildHisto(data, numbins, minimum, maximum) :   
    histo = []
    binwidth = int(diff/numbins)
    for d in data :
        histo[int(d)//binwidth]+=1
    return histo
data = np.loadtxt('math_scores.txt')
minimum = min(data)
maximum = max(data)
diff = maximum - minimum
histo = buildHisto(data, 10, minimum, maximum)
plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True)


   
Error:
IndexError Traceback (most recent call last) <ipython-input-13-1f71b662f892> in <module>() 14 maximum = max(data) 15 diff = maximum - minimum ---> 16 histo = buildHisto(data, 10, minimum, maximum) 17 plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True) <ipython-input-13-1f71b662f892> in buildHisto(data, numbins, minimum, maximum) 8 binwidth = int(diff/numbins) 9 for d in data : ---> 10 histo[int(d)//binwidth]+=1 11 return histo 12 data = np.loadtxt('math_scores.txt') IndexError: list index out of range
Reply
#8
Okay, first of all you start with histo being an empty list. So any index you choose is going to cause an IndexError. You need to initialize it to a list of zeros, and you need one zero for each bin. That would be histo = [0] * numbins.

But there's another problem. Your index is a data point (d) divided by the binwidth. Which would be fine if your first bin started at 0, but your first bin starts at minimum. So any data point over diff is going to cause another IndexError. So you need to subtract minimum from your data point, before dividing by binwidth to get the index of the bin.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


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