ValueError: could not convert string to float: '[-0.32062087,0.27050002,......]' - python-3.x

My dataframe has columns where one has list of float values. When I train that column as X_train, I showing cannot string to float or tensorflow float data type.
DataSet:
I tried this:
df['sent_to_vec'].apply(lambda x: float(x))
or nested for loop to convert values in float type; but didn't get executed.

Try passing a string that's really just a floating-point number to the Python float() function:
f1 = float('0.1')
print(f1)
It works.
Try passing a string that's not just a floating-point number, but is instead some sort of array or list representation with multiple numbers separated by other punctuation:
f2 = float('[0.1, 0.2]')
print(f2)
You'll get the same error as you're asking about. That string, '[0.1, 0.2]' is not a representation of a floating-point number that float() can read.
You should look for a function that can read a string like '[0.1, 0.2]'. Can you see the code that wrote the Vectorized data.csv file? (Did you write that code, or that file?)
You'll want to use some function that does the reverse of whatever wrote that column of the file.

Related

How to convert a String to a float in Python 2.7

I am trying to convert decimal geographic coordinates as strings to a float.
The coordinates are in a csv like this '51213512'. With my Python script I am just reading the coordinates and add the '.'. If I am not adding the comma the rest of my script isn't working.
I already tried a few things but nothing worked for me. This is what I got so far.
latitude=float(long('51.213512'))
The Result is a ValueError:
ValueError: invalid literal for long() with base 10: 'Long'
not too sure why you are using long in this examply if you want to convert this variable to a float just use the float function on its own, you seem to be confusing the long and float functions. dont use both you will be confusing python (basically dosent know what to do because your giving it 2 arguments at once)
I recommend just using the float function on its own. This will avoid confusion
latitude = float('51.2135512')
Get rid of the 'long' and it should work
latitude = float('51.213512')
Edit: Okay, since you're getting the coordinates and manually converting to decimal strings, all you need to do is use the code I said originally. The long function
converts integers or strings of integers to long types, not float types.
>>> long(5)
5L
>>> long('5')
5L
>>> long(5.5)
5L
>>> long('5.5')
ValueError: invalid literal for long() with base 10: '5.5'

Float to Binary and Binary to Float in Python

Can anyone tell me how to convert a float number to 32-bit binary string and from a 32-bit binary string to a float number in python?
'bin' function in python works only for integers.
I need a single bit string as in internal representation. I do not want separate bit strings for the number before and after the decimal places joined by a decimal place in between.
EDIT: The question flagged does not explain how to convert binary string to float back.
Copied from this answer and edited per suggestion from Mark Dickinson:
import struct
def float_to_bin(num):
return format(struct.unpack('!I', struct.pack('!f', num))[0], '032b')
def bin_to_float(binary):
return struct.unpack('!f',struct.pack('!I', int(binary, 2)))[0]
print float_to_bin(3.14) yields “01000000010010001111010111000011”.
print bin_to_float("11000000001011010111000010100100") yields “-2.71000003815”.
I was able to create a program that takes bin decimals as string an returns int decimals!
I used a for loop to start from 1 until the len() of the str+1 to use i number to elevate 2 and, then just keep track of the result with result +=:
def binary_poin_to_number(bin1)->float:
#Try out string slicing here, later
result = 0
for i in range(1,len(bin1)+1):
if bin1[i-1] == '1':
result += 2**-i
return result

How to convert the string '100+20*50/10-9' to float value using python

Haii friends
How can I convert the string expression value to float value
ex: s1 = '100+20*50/10-9' this s1 convert to float value. Based on the arithmetic operator priority rule it should give 191. But the string expression is not convert to float.
I had use float('100+20*50/10-9') and it raises error:
Traceback (most recent call last): File "", line 1, in
ValueError: could not convert string to float: '100+20*50/10-9`
Thanks!
If you are trying to parse a calculation string to a value, you have to evaluate the string first. Like this:
eval('100+20*50/10-9')
Then you may convert it to a float like this float(eval('100+20*50/10-9')).
However, if the calculation string looked like this 101+20*50/3, thus returning 434,3333(3), then you should think of a better solution, parsing the values within to a float before doing the calculation. See the difference:
print float(eval('101+20*50/3')) '434.0
print float(eval('101.0+20.0*50.0/3.0')) '434.333333333
Also using another method:
s = '101+20*50/3'
print("{:.8f}".format(eval(s)))
#ouput:
434.00000000

How to sum up all numbers in an array

I have an array which outputs the following:
charges = [5.00, 26.00, 8.00, 4.00, 4.00, -8.00, 54.00, 52.48]
When I try to perform a sum using this:
charges.sum()
It gives me:
5.0026.008.004.004.00-8.0054.0052.48
I am assuming I need to convert it from a string to a float so I did:
Float.valueOf((String) charges.sum())
and it gives me an error which states 'multiple points'.
My question is how do I add all of these figures up?
If your list is actually of strings, you can quickly do the conversion with sum()'s closure form.
charges.sum { it.toBigDecimal() }
It is unclear what your list has in it, it seems like the entries in your list are actually strings (since sum concatenates them), or something that prints a string value (has a toString method that returns a string showing the value so you think it is numeric even though it isn’t) . They are definitely not numeric, so convert each of them to a numeric type before summing:
charges.collect { new BigDecimal(it.toString()) }.sum()
(What your code was doing was concatenating the string values together, then converting that to a numeric type.)
You must delete the cast (String)
Float.valueOf(charges.sum())

Extracting integer and float numbers

I have the following string "10P_57.53%_568AA". I want to extract only numbers (integer and float numbers) without any other things. The output should be like this:
10 57.53 568
Since you've not provided any language, this would be general method:
Run a loop, get each character of string in a variable say X. Compare X as if(X>=0||X<=9||X=='.'){ Concat value of X to some string }

Resources