I got a list of values and i would like to convert it in an array in order to extract easily columns, but i m embarassed with " which doesn t allow to use : " x = np.array(a, dtype=float)"
['"442116.503118","442116.251106"',
'"442141.502863","442141.247462"',
...
The message obtained is :
"could not convert string to float: "442116.503118","442116.251106""
Answering based on the VERY limited information given, but if that is your list it looks like a list of nested strings, not floats. Try
x = np.array([float(i.replace("\"","")) for i in a], dtype=float)"
This is just wrong... This does the trick for me though:
import numpy as np
wtf = ['"442116.503118","442116.251106"',
'"442141.502863","442141.247462"']
to_list = []
for nest1 in wtf:
nest2 = nest1.split(',')
for each in nest2:
to_list.append(float(each.strip('"')))
to_array = np.asarray(to_list)
Not exactly elegant. You need to deal with each level of nesting in your input data. I'd recommend you reconsider the way you're formatting the data you're inputting.
Related
I am trying to extract a list of integers from a input that looks like this:
[[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]
I am trying to get an output like so:
[0.57863575,0.57170157,0.44320711,0.37195535]
What are my options?
You can use a loop comprehension:
from numpy import matrix
l = [[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]
[e[0].flat[0] for e in l]
output: [0.57863575, 0.57170157, 0.44320711, 0.37195535]
The real question is, how did you get this format in the first place? It might be better to fix it there.
import random
import numpy as np
LOC = np.zeros(96)
LOC[0] = 'H'
for t in range(0,96):
if 32<t<40:
LOC[t] = random.choice(['H','W','I'])
Here, I want to initialize LOC with the character 'H' and has the check few conditions. But when I try to assign it, I am getting an error could not convert string to float: 'H'. How can I assign a character/string to the list LOC?
NumPy is not really made for mixing types of content. If you want an array of strings the empty values shouldn't be zero, but rather empty strings: ''
You can use random.choices() to get the random values and assign, but the trick is to set the dtype to something that's appropriate for strings::
import random
import numpy as np
LOC = np.zeros(96, dtype='<U1')
LOC[0] = 'H'
LOC[32:40] = random.choices(['H','W','I'], k = 40 - 32)
This will be an array of empty strings except where you've assigned random values. Regular python lists, of course work with mixed types, if you don't need NumPy, you can initialize the array with:
LOC = [0] * 96
and then proceed with setting values with whatever you want.
In python u can use the ord function to get the unicode code point of a charachter. So using
LOC[t] = ord(random.choice(['H','W','I']))
you should be able to achieve your goal, even though I would call it 'assigning a character to a numpy array' and not 'assigning a string to a list'.
I have a list of values and would like to convert it to the log of that list or pass the log of a list to a function. I'm more familiar with R and you can usually throw some () around anything. When I attempt this in Python I get the error:
TypeError: must be real number, not list
List looks like this:
pressures[:5]
Out[11]: [1009.58, 1009.58, 1009.55, 1009.58, 1009.65]
It doesn't really matter where I try to take the log, I get the same error...in a function:
plt.plot(timestamps, log(pressures))
plt.xlabel('Timestamps')
plt.ylabel('Air Pressure')
plt.show()
Whilst parsing data:
pressures = log([record['air_pressure'] for record in data])
There are a couple of ways to handle this. Python has some basic, built in functions in the math module. One is log. It takes a float or an int as a parameter and outputs a float:
> from math import log
> log(20)
2.995732273553991
To process a list with this function, you'd need to call it on every item in the list:
> data = [1, 2, 3]
> [log(x) for x in data]
[0.0, 0.6931471805599453, 1.0986122886681098]
On the other hand, and I mention this because it looks like you're already using some related libraries, numpy can process an entire list at once.
> import numpy as np
> np.log([1, 2, 3])
array([ 0. , 0.69314718, 1.09861229]) # Notice this is a numpy array
If you want to use numpy and get a list back, you could do this instead:
> list(np.log([1, 2, 3]))
[0.0, 0.69314718055994529, 1.0986122886681098]
You can only use log() with a single number. So you'll need to write a loop to iterate over your list and apply log() to each number.
Fortunately, you have already written a loop that, with some modification, will do the trick. Instead of:
pressures = log([record['air_pressure'] for record in data])
Write:
pressures = [log(record['air_pressure']) for record in data]
If you wanted to do logs and you have a list of integers you can use the math lib for that.
import math
my_data = [1,2,3,4,5,6,7,8,9]
log_my_data = [math.log(x) for x in my_data]
print(log_my_data)
I have the following string:
(1, 2, 3, 4)
I want to convert it to just the last two values:
(3, 4)
In my actual code all four fields are whole numbers but vary greatly in length. I've tried doing this with both regex and 'for' statements as well as trying the various answers to similar questions here on SO but so far no luck.
This gives you the last two terms in your tuple:
>> a = (1,2,3,4)
>> a[-2:]
(3,4)
It sounds like you want to use the slice operator.
Edit: Perhaps this is a better link. Scroll down a bit for the slice notation stuff. The examples deal with strings, but it should work with any sequence type.
If (1,2,3,4) is tuple:
data = (1,2,3,4)
newData = data[-2:]
If you have '(1,2,3,4)' then:
import ast
data = ast.literal_eval('(1,2,3,5)')
newData = data[-2:]
Or in case you have to split such list in a certain value:
def get_slice(inputData, searchVal):
if searchVal in inputData and inputData.index(searchVal) < len(inputData):
return inputData[inputData.index(searchVal)+1:]
return ()
get_slice((1,2,3,4),2)
I'm pretty new to numpy, and I'm trying to replace a value in a recarray. So I have this array:
import numpy as np
d = [('1', ''),('4', '5'),('7', '8')]
a = np.array(d, dtype=[('first', 'a5'), ('second', 'a5')])
I would like to do something like this:
ind = a=='' #Replace all blanks
a[ind] = '12345'
but that doesnt work properly. I was able to do this:
col = a['second']
ind = col=='' #Replace all blanks
col[ind] = '54321'
a['second'] = col
Which works, but I would rather have a way to do it over the entire recarray. Anyone have a better solution?
The "element-by-element" operations of numpy (with wich you can perform some function on all elements of the array at once without a loop) don't work with recarrays as far as I know. You can only do that with the individual columns.
If you want to use recarrays, I think the easiest solution is to loop the different columns, although you wanted another solution, but you can do it pretty automatic like this:
for fieldname in a.dtype.names:
ind = a[fieldname] == ''
a[fieldname][ind] = '54321'
But maybe you should consider if you really need recarrays, and can't just use normal ndarray. Certainly if you have only one data type (as in the example), then the only advantage are the column names.
One possible solution:
a[np.where(a['second']=='')[0][0]]['second']='12345'