I have been working with Python for a couple of months..Now,I have to perform min-max normalization for a column of my dataset(.csv file) for which I get the above mentioned type error..I have tried a lot but it still persists..Correct values are retrieved for min and max functions but the types of the results are list rather than float/integer..
This is the line that causes error
for i in range(num):
normalized[i]=(krr[i]-min(krr)/(max(krr)-min(krr))
where krr is the column retrieved from the dataset.Please help.
I have a function "normal" which does the min-max normalization.. I have taken column values using eval as shown in code
def normal(self,arr,num):
print("------------------->entered Normalisation block----------------->")
for i in range(num):
# trr=eval(str(arr[i]))[0:-31]
self.krr[i]=map(float,eval(str(arr[i]))[0:-31]) //extracting one particular column
#mn=min(self.krr)
#mx=max(self.krr)
print(self.krr)
ls=min(self.krr)
hs=max(self.krr)
diff=hs-ls
for i in range(num):
normalized[i]=(self.krr[i]-ls)/diff
OK, so the key issue here is that you are working on a list of sublists, with each sublist containing one number.
If you look at your formula:
(krr[i]-min(krr)/(max(krr)-min(krr))
As you mention, python can deal with the max and min - it will return the sublist that contains the biggest/smallest number. (Though note that getting a list containing one number is very different to getting just the one number) However, subtraction and division between lists is not supported, hence your error message. So sooner or later, you need to get the values out of the sublists.
My recommendation is that immediately after you finish constructing krr, you add the following line to your code:
krr = [element[0] for element in krr]
which converts krr from a list of sublists, to a list of the first element of each sublist.
Edit:
An alternative that I think will work, and is more efficient, is to change
def normal(self,arr,num):
print("------------------->entered Normalisation block----------------->")
for i in range(num):
# trr=eval(str(arr[i]))[0:-31]
self.krr[i]=map(float,eval(str(arr[i]))[0:-31]) # This row
into this:
self.krr[i]=float(eval(str(arr[i]))[0:-31][0])
map
applies float
to each element of the following list, and creates a new list. Instead, we're asking for the first element of that list, and applying float
directly to it. That float is assigned to the index in krr.
PS eval(str(arr[i]))[0:-31]
looks rather scary - does eval
really need to be invoked here?