I basically first converted a multidimensional array to a string array in order to set the values as my dictionary key, and now I need to convert the string array back to a regular float array. For example, what I have is:
str_array = ['[0.25 0.2916666666666667]', '[0.5833333333333334 0.2916666666666667]',
'[0.5555555555555555 0.3333333333333332]']
And I literally just need it back as a regular array
array = [[0.25 0.2916666666666667], [0.5833333333333334 0.2916666666666667],
[0.5555555555555555 0.3333333333333332]]
I have tried all the following : (*independently)
for i in str_arr:
i.strip("'")
np.array(i)
float(i)
Yet none of them work. They either cannot convert str --> float or they still keep the type as a str. Please help.
Use ast.literal_eval to convert str to another data type
import ast
str_array = ['[0.25 0.2916666666666667]', '[0.5833333333333334 0.2916666666666667]',
'[0.5555555555555555 0.3333333333333332]']
result = [ast.literal_eval(i.replace(" ", ",")) for i in str_array]
print(result) # [[0.25, 0.2916666666666667], [0.5833333333333334, 0.2916666666666667], [0.5555555555555555, 0.3333333333333332]]
You can also use the basic function eval.
[eval(x.replace(" ",",")) for x in str_array]
Related
def openfiles():
file1 = tkinter.filedialog.askopenfilename(filetypes=(("Text Files",".csv"),("All files","*")))
read_text=pd.read_csv(file1)
displayed_file.insert(tk.END,read_text)
read_text['OPCODE'] = pd.to_numeric(read_text['OPCODE'],errors = 'coerce').fillna(0.0)
read_text['ADDRESS'] = pd.to_numeric(read_text['ADDRESS'],errors = 'coerce').fillna(0.0)
classtype1=np.argmax(model.predict(read_text), axis=-1)
tab2_display_text.insert(tk.END,read_text)
When running this code it shows "could not convert string to float".
Link of the csv file that is used to as datafram: https://github.com/Yasir1515/Learning/blob/main/Book2%20-%20Copy.csv
Complete code link (probmatic code is at line 118-119): https://github.com/Yasir1515/Learning/blob/main/PythonApplication1.py
In your data ADDRESS is a hexadecimal number and OPCODE is a list of hexadecimal numbers. I don't know why would you want to convert hex numbers to float. You should convert them to integers.
The method to_numeric is not suitable to convert hex string to integer, or handle a list of hex numbers. You need to write help function:
def hex2int(x):
try:
return int(x, 16)
except:
return 0
def hex_list2int_list(zz):
return [hex2int(el) for el in zz.split()]
Now replace relevant lines:
read_text['OPCODE'] = read_text['OPCODE'].apply(hex_list2int_list)
read_text['ADDRESS'] = read_text['ADDRESS'].apply(hex2int)
I look at your CSV file. The column OPCODE contains one row with a long string of some numbers separated by space(' '). therefor you cannot cast that type of value to numeric type (the string '88 99 77 66' != numeric type). I can suggest some solution to split those many values in the column OPCODE to many rows and then perform the to_numeric method after afterwards you can make manipulation and return it to the previous form.
what I suggest is:
read_text=pd.read_csv(file1)
new_df = pd.concat([pd.Series(row['ADDRESS'], row['OPCODE'].split(' '))
for _, row in a.iterrows()]).reset_index()
new_df['OPCODE'] = pd.to_numeric(new_df['OPCODE'],errors = 'coerce').fillna(0.0)
I am using PulP to solve a linear programming problem. I am having the following error in the objective function:
TypeError: list indices must be integers or slices, not str
My objective function is
prob += lpSum([routes_vars][man][pa]*costs_distance[man][pa] for (man,pa) in routes)
according to the error message, I think my problem is the costs_distance dictionary that has string values
costs_distance = {'201': {'10267': '167724.1407', '10272': '151859.5908', '10275': '150131.7254', '10277': '153266.1819', '10279': '147949.5275', '10281': '145429.9767', '10283': '144757.2507', '10286': '166474.849', '10288': '152733.6419'}, '2595': {'10267': '186216.5193', '10272': '170351.9694', '10275': '168624.1039', '10277': '171758.5604', '10279': '166441.906', '10281': '163922.3553', '10283': '163249.6293', '10286': '186363.4807', '10288': '171226.0204'},
How can I convert only the dictionary string values ('167724.1407', '151859.5908', '150131.7254'... ) into int values?
Your issue has nothing to do with the costs_distance dictionary (otherwise the error message wouldn't mention a list). It's this part:
[routes_vars][man][pa]
I'm not sure what you expect this to return, but it first constructs a list with a single element (routes_vars) then tries to slice it using [man], which doesn't make any sense.
If sure you meant converting dictionary values to float and not int
list1 = costs_distance['201'].values()
list2 = list(map(float, list1))
I have a piece of code that is meant to read a CSV file that has data in it. I get this error message when I run the program, "ValueError: could not convert string to float:" How can I make my strings into floats?
,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r = np.loadtxt('car.txt', delimiter = '\s', unpack = True)
plt.plot(o,r, label='Loaded from file!')
plt.xlabel('o')
plt.ylabel('r')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
ValueError: could not convert string to float:
Python use float() function to convert a string into a float. The ValueError happens when Python doesn't know how to do so
for example
float("1.234") # return 1.234
float("1.45 ") # return 1.45
but if you do
float("abc")
This will cause a ValueErrror since Python doesn't know how to convert it.
Back to your question, maybe try to print your variable and check if it is valid to be converted to float.
if all your data are float, maybe:
1,the first rou are names not float
2,maybe there are 'backspace' in your data, they will be recognize as '/t', find a spack in your data,press ctrl+r,change backspace to space
It looks like I have a malformed numpy array in Python3.x---this was saved as a list of lists of strings.
foo = [[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06]
[1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06] [7.2646574e-06 7.1252006e-06 3.0184277e-01 ... 1.0048618e-05 3.1828706e-06 1.0196264e-06]..]
I get the following error trying to read in this data as np.float32 into a numpy array:
np.asarray(foo, dtype=np.float32)
error:
ValueError: could not convert string to float:[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06][1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06] [7.2646574e-06 7.1252006e-06 3.0184277e-01 ... 1.0048618e-05 3.1828706e-06 1.0196264e-06]..]
I've tried explicitly converting each list element into a float as follows:
try2 = np.asarray(map(np.float32, foo))
but it snags on a bracket:
ValueError: could not convert string to float: [
What is the recommended way to convert a list of lists of strings into a numpy array, type float?
If you replace the spaces with commas, you can use json.loads to read the string as a list, and pass that to np.asarray:
import json
import numpy as np
foo = "[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06] \
[1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06]]"
a = np.asarray(json.loads(foo.replace(" ", ",")), dtype=np.float32)
print(a)
#array([[7.0352220e-01, 5.3130367e-06, 1.5167372e-05, 1.0797821e-06],
# [1.3130367e-06, 2.4584832e-01, 2.2375602e-05, 7.3299240e-06]])
print(a.dtype)
#float32
This assumes there is exactly 1 space between values. If that is not the case, you can use re.sub to replace multiple spaces with a comma:
import re
a = np.asarray(json.loads(re.sub("\s+", ",", foo)))
#array([[7.0352221e-01, 5.3130366e-06, 1.5167372e-05, 1.0797821e-06],
# [1.3130367e-06, 2.4584831e-01, 2.2375601e-05, 7.3299238e-06]],
# dtype=float32)
As far as I have seen, np.asarray() works only if dtype has a different datatype from the initial datatype. Please try and remove that argument and see if it works.
How is your string data shaped? Probably the simplest way is to use split() and iterate over the list. Example (list of lists of strings) that worked for me:
foo = [['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06'],
['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06']]
arr = np.array([[value.split() for value in row][0] for row in foo], dtype='<f8')
(Note: the [0] is used as split creates a list itself. You can use np.reshape in alternative)
EDIT: if its a string representation (not a list of strings as stated in the OP):
foo = '[[7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06][7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06]'
arr=np.array([line.split() for line in foo.replace('[','').replace(']]','').split(']')], dtype='<f8')
Given:
foo = [['7.0352220e-01 5.3130367e-06 1.5167372e-05 1.0797821e-06'],
['1.3130367e-06 2.4584832e-01 2.2375602e-05 7.3299240e-06'],
['7.2646574e-06 7.1252006e-06 3.0184277e-01 1.0048618e-05']]
Try this to split each string
foo = [row[i].split() for row in foo for i in range(len(foo[0]))]
This for changing type to floats.
foo = [[float(row[i]) for i in range(len(foo[0]))] for row in foo]
print(type(foo[0][1]))
>> float
Then turn it into a numpy array:
foo = np.array(foo)
print(type(foo[0][1]))
>> numpy.float64
In my data frame,one of column has string values as array look.I get them and stored in an array.Then array look like,
S=['[18831]', '[12329]', '[4526, 5101, 11276]', '[14388, 14389]']
I want it to be
S= [18831,12329,[4526, 5101, 11276],[14388, 14389]]
as 2d array to access this IDs.How to do this using python
Those lists are in JSON format so you could use the built in JSON parser.
import json
stringArray = "[1,2,3]"
integerArray = json.loads(stringArray) # [1,2,3]
Check out https://docs.python.org/2/library/json.html
Try this:
[eval(a)[0] if len(eval(a)) == 1 else eval(a) for a in S]