I have theses points that I need to be in a 2,N array. I tried using Numpy but couldn't get it to work. Any help will do.
30.90,401.01;96.91,384.22;80.70,340.79;67.38,337.90;55.80,300.26;72.01,299.68;92.28,310.10;99.81,331.53;92.86,340.79;108.49,380.16;108.49,382.48;168.13,373.22;352.83,380.74;474.43,393.48;535.80,409.69;560.70,369.16;550.28,362.79;562.44,333.26;592.55,328.05;605.28,329.79;594.28,363.95;572.28,370.32;549.12,413.17;714.14,469.33;716.45,479.17;203.45,479.75;0.00,479.17;0.22,365.11
You can use matrix:
s = '30.90,401.01;96.91,384.22;80.70,340.79'
np.array(np.matrix(s))
Output:
array([[ 30.9 , 401.01],
[ 96.91, 384.22],
[ 80.7 , 340.79]])
Related
I think I have an easy problem, but can't find a solution.
I have an array X_train with a list of strings
4 [visa, card, geldanlage, 74843e]
Name: Keyword_clean, dtype: object
Then I want to transform this to a pandas dataframe. I use the following code:
X_train = pd.DataFrame(data=X_train, columns = ['Keyword_clean'])
X_train
The X_train dataframe then looks like this
Index
Keyword_clean
4
visa,card,geldanlage,74843e
What I would like to achieve is, that it looks like this (the list of the array is still kept)
Index
Keyword_clean
4
[visa,card,geldanlage,74843e]
Any ideas?
thanks a lot
You could convert it to a pd.Series first:
import pandas as pd
array = [["visa", "card", "geldanlage", "74843e"]] * 10
df = pd.DataFrame(pd.Series(array))
i have the following numpy array extracted from a dataframe that i want to reshape
Extraction
x = c_df['x'].values
y = c_df['y'].values
z = c_df['z'].values
convert to array
x_y_z = np.array([x, y, z])
x_y_z
Array looks like this
array([[748260.27757, 748262.56478, 748263.52455, ..., 730354.86406,
730374.75 , 730388.45066],
[333346.25 , 333308.43521, 333296.25 , ..., 331466.13593,
331453.84365, 331446.25 ],
[ 2840. , 2840. , 2840. , ..., 2400. ,
2400. , 2400. ]])
basically i want to reshape it to be able to plot using plt.contourf which required Z to be 2D array
so i assume the array needs to be reshaped to something like
YYYYYYYYYY
Xzzzzzzzzzz
Xzzzzzzzzzz
Xzzzzzzzzzz
Xzzzzzzzzzz
is my assumption correct? if yes how to reshape the array.
If I understand you correctly Numpy Mgrid should be able to help you. However you might want some more explanation which can be found on this thread.
For the next time you can make it easier if you provide a simplified example of your problem.
I have a CSV file:
8.84,17.22,13.22,3.84
3.99,11.73,19.66,1.27
Def jo(x):
data=np.loadtxt(x,delimiter=',')
Return data
Print(jo('data.csv')
The code returns:
[ [8.84 17.22 13.22 3.84]
[3.99 11.73 19.66 1.27] ]
But I want all these elements in a single array, because I want to find their mean and median.
How to combine these 2 arrays into 1 ?
use numpy.rehshape
# data: data is your array
>>> data.reshape(-1)
In [245]: txt="""8.84,17.22,13.22,3.84
...: 3.99,11.73,19.66,1.27"""
In [246]: data = np.loadtxt(txt.splitlines(), delimiter=',')
In [247]: data
Out[247]:
array([[ 8.84, 17.22, 13.22, 3.84],
[ 3.99, 11.73, 19.66, 1.27]])
In [248]: data.shape
Out[248]: (2, 4)
That is one array, just 2d.
There are various ways of turning that into a 1d array:
In [259]: arr = data.ravel()
In [260]: arr
Out[260]: array([ 8.84, 17.22, 13.22, 3.84, 3.99, 11.73, 19.66, 1.27])
But there's no need to do that. mean (and median) without axis parameter acts on the raveled array. Check the docs:
In [261]: np.mean(data)
Out[261]: 9.971250000000001
In [262]: np.mean(arr)
Out[262]: 9.971250000000001
enter image description here
I am getting odd behavior with Jupyter/Numpy/Tranpose()/1D Arrays.
I found another post where transpose() will not transpose a 1D array, but in previous Jupyter notebooks, it does.
I have an example where it is inconsistent, and I do not understand:
Please see the picture attached of my jupyter notebook if 2 more or less identical arrays with 2 different outputs.
It seems it IS and IS NOT transposing the 1D array. Inconsistency is bad
outputs is (1000,) and (1,1000), why does this occur?
# GENERATE WAVEORM:
#---------------------------------------------------------------------------------------------------
N = 1000
fxc = []
fxn = []
for t in range(0,N):
fxc.append(A1*m.sin(2.0*pi*50.0*dt*t) + A2*m.sin(2.0*pi*120.0*dt*t))
fxn.append(A1*m.sin(2.0*pi*50.0*dt*t) + A2*m.sin(2.0*pi*120.0*dt*t) + 5*np.random.normal(u,std,size=1))
#---------------------------------------------------------------------------------------------------
# TAKE TRANSPOSE:
#---------------------------------
fc = np.transpose(np.array(fxc))
fn = np.transpose(np.array(fxn))
#---------------------------------
# PRINT DIMENSION:
#---------------------------------
print(fc.shape)
print(fn.shape)
#---------------------------------
Remove size=1 from your call to numpy.random.normal. Then it will return a scalar instead of a 1-d array of length 1.
For example,
In [2]: np.random.normal(0, 3, size=1)
Out[2]: array([0.47058288])
In [3]: np.random.normal(0, 3)
Out[3]: 4.350733438283539
Using size=1 in your code is a problem, because it results in fxn being a list of 1-d arrays (e.g. something like [[0.123], [-.4123], [0.9455], ...]. When NumPy converts that to an array, it has shape (N, 1). Transposing such an array results in the shape (1, N).
fxc, on the other hand, is a list of scalars (e.g. something like [0.123, 0.456, ...]). When converted to a NumPy array, it will be a 1-d array with shape (N,). NumPy's transpose operation swaps dimensions, but it does not create new dimensions, so transposing a 1-d array does nothing.
I have a list of 50 numpy arrays called vectors:
[array([0.1, 0.8, 0.03, 1.5], dtype=float32), array([1.2, 0.3, 0.1], dtype=float32), .......]
I also have a smaller list (means) of 10 numpy arrays, all of which are from the bigger list above. I want to loop though each array in means and find its position in vectors.
So when I do this:
for c in means:
print(vectors.index(c))
I get the error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I've gone through various SO questions and I know why I'm getting this error, but I can't find a solution. Any help?
Thanks!
One possible solution is converting to a list.
vectors = np.array([[1, 2, 3], [4, 5, 6], [7,8,9], [10,11,12]], np.int32)
print vectors.tolist().index([1,2,3])
This will return index 0, because [1,2,3] can be found in index 0 of vectors
Th example above is a 2d Numpy array, however you seem to have a list of numpy arrays,
so I would convert it to a list of lists this way:
vectors = [arr.tolist() for arr in vectors]
Do the same for means:
means = [arr.tolist() for arr in means]
Now we are working with two lists of lists:
So your original 'for loop' will work:
for c in means:
print(vectors.index(c))