I have some lists that I want to convert into a 2D numpy array.
list1 = [ 2, 7 , 8 , 5]
list2 = [18 ,29, 44,33]
list3 = [2.3, 4.6, 8.9, 7.7]
The numpy array I want is:
[[ 2. 18. 2.3]
[ 7. 29. 4.6]
[ 8. 44. 8.9]
[ 5. 33. 7.7]]
which I can get by typing the individual items from the list directly into the numpy array expression as np.array(([2,18,2.3], [7,29, 4.6], [8,44,8.9], [5,33,7.7]), dtype=float).
But I want to be able to convert the lists into the desired numpy array.
One way to do it would be to create your numpy array and then use the transpose function to convert it to your desired output:
import numpy as np
list1 = [ 2, 7 , 8 , 5]
list2 = [18 ,29, 44,33]
list3 = [2.3, 4.6, 8.9, 7.7]
arr = np.array([list1, list2, list3])
arr = arr.T
print(arr)
Output
[[ 2. 18. 2.3]
[ 7. 29. 4.6]
[ 8. 44. 8.9]
[ 5. 33. 7.7]]
you could use np.transpose directly:
np.transpose([list1, list2, list3])
and this will convert the list of your lists to a numpy array and transpose it (change rows to columns and columns to rows) afterwards:
array([[ 2. , 18. , 2.3],
[ 7. , 29. , 4.6],
[ 8. , 44. , 8.9],
[ 5. , 33. , 7.7]])
also you can use zip function like this
In [1]: import numpy as np
In [2]: list1 = [ 2, 7 , 8 , 5]
In [3]: list2 = [18 ,29, 44,33]
In [4]: list3 = [2.3, 4.6, 8.9, 7.7]
In [5]: np.array(zip(list1,list2,list3))
Out[5]:
array([[ 2. , 18. , 2.3],
[ 7. , 29. , 4.6],
[ 8. , 44. , 8.9],
[ 5. , 33. , 7.7]])
Related
Input:
array([[ 1. , 5. , 1. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ]])
Expected Output:
array([[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 1. , 5. , 1. ]])
i tried doing the below:
for i in M:
ls = i.mean()
x = np.append(i,ls)
print(x) #found the mean
After this i am unable to arrange each column based on the mean value in each row. All i can do
is to arrange each row in descending order but that is not what i wanted.
You can do this:
In [405]: row_idxs = np.argsort(np.mean(a * -1, axis=1))
In [406]: a[row_idxs, :]
Out[406]:
array([[ 19. , 9. , 100. ],
[ 11. , 11. , 11. ],
[ 10. , 7. , 1.5],
[ 6.9, 5. , 1. ],
[ 1. , 5. , 1. ]])
Using argsort will sort the indices. Multiplying by -1 allows you to get descending order.
I have a 2D array and I want to delete a point out of it but suppose it's so big meaning I can't specify an index and just grab it and the values of the array are float
How can I delete this point? With a LOOP and WITHOUT LOOP?? the following is 2D array and I want to delete [ 32.9, 23.]
[[ 1. , -1.4],
[ -2.9, -1.5],
[ -3.6, -2. ],
[ 1.5, 1. ],
[ 24. , 11. ],
[ -1. , 1.4],
[ 2.9, 1.5],
[ 3.6, 2. ],
[ -1.5, -1. ],
[ -24. , -11. ],
[ 32.9, 23. ],
[-440. , 310. ]]
I tried this but doesn't work:
this_point = np.asarray([ 32.9, 23.])
[x for x in y if x == point]
del datapoints[this_point]
np.delete(datapoints,len(datapoints), axis=0)
for this_point in datapoints:
del this_point
when I do this, the this_point stays in after printing all points, what should I do?
Python can remove a list element by content, but numpy does only by index. So, use "where" to find the coordinates of the matching row:
import numpy as np
a = np.array([[ 1. , -1.4],
[ -2.9, -1.5],
[ -3.6, -2. ],
[ 1.5, 1. ],
[ 24. , 11. ],
[ -1. , 1.4],
[ 2.9, 1.5],
[ 3.6, 2. ],
[ -1.5, -1. ],
[ -24. , -11. ],
[ 32.9, 23. ],
[-440. , 310. ]])
find = np.array([32.9,23.])
row = np.where( (a == find).all(axis=1))
print( row )
print(np.delete( a, row, axis=0 ) )
Output:
(array([10], dtype=int64),)
[[ 1. -1.4]
[ -2.9 -1.5]
[ -3.6 -2. ]
[ 1.5 1. ]
[ 24. 11. ]
[ -1. 1.4]
[ 2.9 1.5]
[ 3.6 2. ]
[ -1.5 -1. ]
[ -24. -11. ]
[-440. 310. ]]
C:\tmp>
I have a 3d list of lists or numpy array and I need to sort it, by the smallest first item index.
This are the last two tentatives I did on this program. Sorry, I am quite sure it is an easy/silly question, but as a newbie in programming 'way of thinking', it is kind of hard for me.
First Try:
lstsArray = [[[54,21,31], [1,2,3], [15,25,35]],
[[12,22,32], [3,2,1], [16,26,36]],
[[34,24,38], [0.1,1,1], [17,27,37]]]
val = np.array(lstsArray)
menor = 120e26
for item in val:
for i in item:
if menor >= i[0] and i[0] >= min(i):
menor = i[0]
print(menor)
lstA = list(val)
a = sorted(lstA, key=itemgetter(menor))
print(a)
Second Try
for i in val:
for j in i:
print(sorted((i), key =itemgetter(j[0])))
Desired Output
[[[0.1,1,1],[1,2,3],[3,2,1]],
[[12,22,32],[15,25,35],[16,26,36]],
[[17,27,37],[34,24,38],[54,21,31]]]
Your list, and array made from it. Note the floats in the array:
In [124]: lstsArray = [[[54,21,31], [1,2,3], [15,25,35]],
...: [[12,22,32], [3,2,1], [16,26,36]],
...: [[34,24,38], [0.1,1,1], [17,27,37]]]
In [125]: val=np.array(lstsArray)
In [126]: val
Out[126]:
array([[[54. , 21. , 31. ],
[ 1. , 2. , 3. ],
[15. , 25. , 35. ]],
[[12. , 22. , 32. ],
[ 3. , 2. , 1. ],
[16. , 26. , 36. ]],
[[34. , 24. , 38. ],
[ 0.1, 1. , 1. ],
[17. , 27. , 37. ]]])
This is a (3,3,3) shaped array. But your sorting ignores the initial (3,3) layout, so let's go ahead and reshape it:
In [133]: val = np.array(lstsArray).reshape(-1,3)
In [134]: val
Out[134]:
array([[54. , 21. , 31. ],
[ 1. , 2. , 3. ],
[15. , 25. , 35. ],
[12. , 22. , 32. ],
[ 3. , 2. , 1. ],
[16. , 26. , 36. ],
[34. , 24. , 38. ],
[ 0.1, 1. , 1. ],
[17. , 27. , 37. ]])
Now we can easily reshape on the first column value. argsort gives the sort order:
In [135]: idx = np.argsort(val[:,0])
In [136]: idx
Out[136]: array([7, 1, 4, 3, 2, 5, 8, 6, 0])
In [137]: val[idx]
Out[137]:
array([[ 0.1, 1. , 1. ],
[ 1. , 2. , 3. ],
[ 3. , 2. , 1. ],
[12. , 22. , 32. ],
[15. , 25. , 35. ],
[16. , 26. , 36. ],
[17. , 27. , 37. ],
[34. , 24. , 38. ],
[54. , 21. , 31. ]])
and to get it back to 3d:
In [138]: val[idx].reshape(3,3,3)
Out[138]:
array([[[ 0.1, 1. , 1. ],
[ 1. , 2. , 3. ],
[ 3. , 2. , 1. ]],
[[12. , 22. , 32. ],
[15. , 25. , 35. ],
[16. , 26. , 36. ]],
[[17. , 27. , 37. ],
[34. , 24. , 38. ],
[54. , 21. , 31. ]]])
or in list display:
In [139]: val[idx].reshape(3,3,3).tolist()
Out[139]:
[[[0.1, 1.0, 1.0], [1.0, 2.0, 3.0], [3.0, 2.0, 1.0]],
[[12.0, 22.0, 32.0], [15.0, 25.0, 35.0], [16.0, 26.0, 36.0]],
[[17.0, 27.0, 37.0], [34.0, 24.0, 38.0], [54.0, 21.0, 31.0]]]
But if the list had just one level of nesting:
In [140]: alist = val.tolist()
In [141]: alist
Out[141]:
[[54.0, 21.0, 31.0],
[1.0, 2.0, 3.0],
[15.0, 25.0, 35.0],
[12.0, 22.0, 32.0],
[3.0, 2.0, 1.0],
[16.0, 26.0, 36.0],
[34.0, 24.0, 38.0],
[0.1, 1.0, 1.0],
[17.0, 27.0, 37.0]]
the python sorted works quite nicely:
In [142]: sorted(alist, key=lambda x:x[0]) # or itemgetter
Out[142]:
[[0.1, 1.0, 1.0],
[1.0, 2.0, 3.0],
[3.0, 2.0, 1.0],
[12.0, 22.0, 32.0],
[15.0, 25.0, 35.0],
[16.0, 26.0, 36.0],
[17.0, 27.0, 37.0],
[34.0, 24.0, 38.0],
[54.0, 21.0, 31.0]]
The fact that you have a double nested list, but want the sort to ignore one layer, complicates the list processing. That's where numpy reshape helps a lot.
For now I won't test the relative speeds of these approaches.
I've got a one dimensional array (n) called edges and want to insert the values by the index from the vertices array (n,3)
vertices = [[ 1.25, 4.321, -4], [2, -5, 3.32], [23.3, 43, 12], [32, 4, -23]]
edges = [1, 3, 2, 0]
result = [[2, -5, 3.32], [32, 4, -23], [23.3, 43, 12], [ 1.25, 4.321, -4]]
I tried np.take(vertices, edges) but It doesn't work for multi dimensional arrays.
take with axis parameter works
In [313]: vertices=np.array(vertices)
In [314]: edges=[1,3,2,0]
In [315]: np.take(vertices, edges,0)
Out[315]:
array([[ 2. , -5. , 3.32 ],
[ 32. , 4. , -23. ],
[ 23.3 , 43. , 12. ],
[ 1.25 , 4.321, -4. ]])
In [316]: vertices[edges,:]
Out[316]:
array([[ 2. , -5. , 3.32 ],
[ 32. , 4. , -23. ],
[ 23.3 , 43. , 12. ],
[ 1.25 , 4.321, -4. ]])
You can simply use indexing here:
vertices[edges]
# ^ ^ indexing
If you index with a list, then numpy will reshuffle the original matrix such that the highest dimension here follows the indices as specified by edges.
like:
>>> vertices = np.array([[ 1.25, 4.321, -4], [2, -5, 3.32], [23.3, 43, 12], [32, 4, -23]])
>>> edges = [1, 3, 2, 0]
>>> vertices[edges]
array([[ 2. , -5. , 3.32 ],
[ 32. , 4. , -23. ],
[ 23.3 , 43. , 12. ],
[ 1.25 , 4.321, -4. ]])
>>> vertices[edges].base is None
True
The fact that base is None means that this does not generate a view, it makes a copy of the matrix (with filtered/reordered rows). Changes you thus later make to the elements of vertices will not change the elements of the result of vertices[edges] (given you make the copy before altering vertices of course).
I have data in an array.
The first column is time. Second, latitude, third longitude, fourth precipitation
Sample:
2 70 100 5.6
2 70 110 5.9
2 80 100 6.2
2 80 110 5.0
3 70 100 2.3
3 70 110 1.1
3 80 100 0.0
3 80 110 7.9
I would like to convert this into an array where the y axis is longitude, the z axis is latitude, and the x axis is time. Precipitation amounts will be located at each 3d grid point.
For instance, in the following image:
The sizes of the bubbles represent different precipitation amounts (ignore the colors)
How can I use python to do this?
So far I have:
import numpy as np<br>
a=open('time.dat') #original file
b=open('three.dat','w+')
dif=np.fromfile(a)
tim=dif[:,[0]]
lat=dif[:,[1]]
lon=dif[:,[2]]
pre=dif[:,[3]]
c=np.empty(780,360,720)
780 time steps, 360 latitudes, 720 longitudes
So you want a 2 dimensional array with the inner dimension containing all of the data, and the outer dimension ordered by lon, lat, time.
You can read in the file as a array of values, convert to a 2d array to group them into each 4 tuple. Then translate the column order of the inner array. Next sort the outer dimension on the inner dimension.
>>> data = np.array([2, 70, 100, 5.6, 2, 70, 110, 5.9, 2, 80, 100, 6.2, 2, 80, 110, 5.0, 3, 70, 100, 2.3, 3, 70, 110, 1.1, 3, 80, 100, 0.0, 3, 80, 110, 7.9])
>>> data2 = data.reshape((8, 4))
>>> data2
array([[ 2. , 70. , 100. , 5.6],
[ 2. , 70. , 110. , 5.9],
[ 2. , 80. , 100. , 6.2],
[ 2. , 80. , 110. , 5. ],
[ 3. , 70. , 100. , 2.3],
[ 3. , 70. , 110. , 1.1],
[ 3. , 80. , 100. , 0. ],
[ 3. , 80. , 110. , 7.9]])
>>> data2 = data2[:,[1,2,0,3]]
>>> data2
array([[ 70. , 100. , 2. , 5.6],
[ 70. , 110. , 2. , 5.9],
[ 80. , 100. , 2. , 6.2],
[ 80. , 110. , 2. , 5. ],
[ 70. , 100. , 3. , 2.3],
[ 70. , 110. , 3. , 1.1],
[ 80. , 100. , 3. , 0. ],
[ 80. , 110. , 3. , 7.9]])
The goofiness with view and sort described here
You can't use the numpy reshape for a simple reason : you have data duplicity in your original array (time and positions) and not in the result you want. Before and after a reshape the number of elements must be the same.
You have to do a loop to read your initial array and fill your new array.
Hope it helped