ValueError: could not convert string to float: id
Obviously some of your lines don't have valid float data, specifically some line have text id
which can't be converted to float.
When you try it in interactive prompt you are trying only first line, so best way is to print the line where you are getting this error and you will know the wrong line e.g.
#!/usr/bin/python
import os,sys
from scipy import stats
import numpy as np
f=open('data2.txt', 'r').readlines()
N=len(f)-1
for i in range(0,N):
w=f[i].split()
l1=w[1:8]
l2=w[8:15]
try:
list1=[float(x) for x in l1]
list2=[float(x) for x in l2]
except ValueError,e:
print "error",e,"on line",i
result=stats.ttest_ind(list1,list2)
print result[1]
ValueError: could not convert string to float: 'Yes, policy' while fitting to my Logistic Regression Model
It looks like your ANSWER
and TEXT
columns contains categorical values, and you have to encode them in numeric form before feeding it to the model. Because machine learning models don't understand text. Use this code on the dataframe before using train_test_split
from sklearn.preprocessing import LabelEncoder
df['TEXT'] = df['TEXT'].astype('str')
df['ANSWER'] = df['ANSWER'].astype('str')
df[['ANSWER', 'TEXT']] = df[['ANSWER', 'TEXT']].apply(LabelEncoder().fit_transform)
Also, it's a multi-class classification problem, so Logistic Regression
isn't going to give you good results. Use RandomForestClassifier
.
ValueError: could not convert string to float when converting input
Probable cause: you forgot to fill out one field
Explanation
a float can be constructed from a str, it just has to have the right format.
You should be able to convert strings that look like floats to floats by just using float(mystr)
.
Pay attention to:
- No spaces (or non-digit characters)
- Period (
.
) as decimal separator, not comma (,
) - some special things are allowed (e.g.
inf
,5e3
,
...)
Most probably (if you didn't cut anything off your error message), you forgot to fill out one field and are thus trying to perform float('')
.
Some examples
>>> float('') # probably your case
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
float('')
ValueError: could not convert string to float:
>>> float('1')
1.0
>>> float('1.0')
1.0
>>> float('no number')
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
float('no number')
ValueError: could not convert string to float: 'no number'
>>> float('10e30')
1e+31
>>> float('inf')
inf
>>> float('123.456')
123.456
>>> float('123,456') # , not allowed
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
float('123,456')
ValueError: could not convert string to float: '123,456'
>>> float('123 456') # no whitespace as separator
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
float('123 456')
ValueError: could not convert string to float: '123 456'
Further thoughts
An alternative for getting the float values is asking for them in a popup window -- this can be done with tkinter.simpledialog.askfloat
. This will return you a float
directly and show an error message if the string entered could not be converted.
But, it may also return None
when the user clicks the "Cancel" button, so you might want to check the result before computing stuff.
Example:
import tkinter as tk
from tkinter.simpledialog import askfloat
def click():
val = askfloat('Title', 'The prompt:') # float or None (cancel)
if val is not None:
tk.Label(root, text=f'The value "{val}" is a valid float').pack() # f-string, replace with `'The value "{}" is ...'.format(val)` if you get an error (Python < 3.6)
root = tk.Tk()
tk.Button(root, text='Click!', command=click).pack()
root.mainloop()
Edit:
If you keep the Entry
widgets, you may want to catch the error and show the user a message. This can easily be done with tkinter.mesagebox
, specifically of this case the showerror
function.
Example:
import tkinter as tk
from tkinter.messagebox import showerror
def click():
try:
val = float(entry.get())
except ValueError as e:
showerror('Error title', 'The number could not be converted to float:\n'+str(e))
else:
entry.delete(0, tk.END)
Label(root, text=val).pack()
root = tk.Tk()
entry = tk.Entry(root)
tk.Label(root, text='insert a float value below:').pack()
entry.pack()
tk.Button(root, text='and click!', command=click).pack()
root.mainloop()
ValueError: could not convert string to float: ' caing low enough for rotary follow on' | Python
The issue is that you're not correctly identifying the datetime in the string, so you end up trying to convert the wrong bit to a float.
One potential fix for this would be to not rely on splitting the string at the :
s, but instead to use a regex to look for the part of the string with the appropriate format.
For example:
import re
test_string = 'this is a string with 06:00:00 in it somewhere'
matches = re.search('(\d{2}):(\d{2}):(\d{2})', test_string)
matches = [float(m) for m in matches.groups()]
print(matches)
# [6.0,0.0,0.0]
Related Topics
How to Clamp an Integer to Some Range
Getting Computer's Utc Offset in Python
How to Login to a Website with Python
How to Save an Image Locally Using Python Whose Url Address I Already Know
Pip Cannot Uninstall <Package>: "It Is a Distutils Installed Project"
Unicodedecodeerror When Redirecting to File
How to Use 'Else' in a List Comprehension
How to Install Pycrypto on Windows
Ssl.Sslerror: Tlsv1 Alert Protocol Version
How to Calculate Mean Values Grouped on Another Column in Pandas
How to Install 2 Anacondas (Python 2 and 3) on MAC Os
Reimport a Module While Interactive
Two Variables in Python Have Same Id, But Not Lists or Tuples
Passing Table Name as a Parameter in Psycopg2
First Non-Null Value Per Row from a List of Pandas Columns
Python: Best Practice and Securest Way to Connect to MySQL and Execute Queries