This question already has answers here:
Stacking arrays in numpy
(2 answers)
Closed 2 years ago.
I have three arrays:
a = array([1,2,3,4])
b = array([5,6,7,8])
c = array([9,10,11,12])
I would like a single array:
result = array([1,5,9],
[2,6,10],
[3,7,11],
[4,8,12])
i.e. take the first column of every array and make it as the first row and so on.
I know it might sound trivial, but have been scratching my head.
Use the numpy module:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.array([9,10,11,12])
result = np.stack((a,b,c), axis = 1) # axis = 1 transposes the stacked matrix
print(result)
The code above gives the following output:
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
Which is what you wanted.
Related
This question already has answers here:
check for identical rows in different numpy arrays
(7 answers)
Closed 19 days ago.
I would like to ask a question with numpy array masking.
For instance given the array below:
a b
1 2
3 4
5 6
6 5
I have another array which is
a b
1 2
3 4
I want to compare two arrays and find the index numbers of second array in the first array.
For instance, the solution should be index=[0,1]
I have tried with
np.where np.where(~(np.abs(a - b[:,None]).sum(-1)==0).any(0))
but does not give me the final result
thanks for suggestions!
A possible solution, based on Broadcasting, where ar1 and ar2 are the two arrays, respectively:
np.nonzero(np.any(np.all(ar1 == ar2[:,None], axis=2), axis=0))[0]
Output:
array([0, 1])
a = np.array([[1,2],[3,4],[5,6],[6,5]])
b = np.array([[1,2],[3,4]])
np.where(np.all(a == b[:,None], axis=2))[1] # np.array([0,1])
How do I fill a 2d array value from an updated 1d list ?
for example I have a list that I get from this code :
a=[]
for k, v in data.items():
b=v/sumcount
a.append(b)
What I want to do is produce several 'a' list and put their value into 2d array with different column. OR put directly the b value into 2D array whic one colum represent loop for number of k.
*My difficulties here is, k is not integer. its dict keys (str). whose length=9
I have tried this but does not work :
row = len(data.items())
matrix=np.zeros((9,2))
for i in range (1,3) :
a=[]
for k, v in data.items():
b=v/sumcount
matrix[x][i].fill(b), for x in range (1, 10)
a list is
1
2
3
4
5
6
7
8
9
and for example I do the outer loop, what I expect is
*for example 1 to 2 outer loop so I expect there will be 2 column and 9 row.
1 6
2 7
3 8
4 9
5 14
6 15
7 16
8 17
9 18
I want to fill matrix value with b
import numpy as np
import pandas as pd
matrix = np.zeros((9, 2))
df = pd.DataFrame({'aaa': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
sumcount = [1, 2]
for i in range(len(sumcount)):
matrix[:, i] = df['aaa']/sumcount[i]
print(matrix)
As far as I understand you: you need to get the result of the column from the dataframe and place it in a numpy array. No need to iterate over each row if your sumcount is the same number. This will work slowly. In general, loops are used as a last resort, if there is no other possibility.
Slicing is used to set values in numpy.
bbb = np.array([df['aaa']/sumcount[i] for i in range(len(sumcount))]).transpose()
print(bbb)
Or do without a loop at all using list comprehension, wrap the result in np.array and apply transpose.
This question already has answers here:
How to filter numpy array by list of indices?
(5 answers)
Closed 9 months ago.
If I have a numpy array
a = np.repeat([i for i in range(10)], 1000)
and another numpy array
b = np.arange(10, 20)
How do I insert the values of b into a based on index? So that all 0 = 10, 1 = 11 and so on. Is there something similar to matlab where you can say b(a)
Hope this helps you,
import numpy as np
a = np.repeat([i for i in range(10)], 1000)
b = np.arange(10, 20)
a=b[a]
print(a)
The output:
[10 10 10 ... 19 19 19]
This question already has answers here:
Numpy slicing from variable
(2 answers)
Closed 1 year ago.
If i have a numpy array:
arr = np.array([1,2,3,4,5,6,7,8,9,10])
x = 3 # index
n = 5
m = 2
Is there a way to get an output like this?
output: np.array([1,2,3,4,5,6])
We start at 4 which is index x=3. The output consists of n=5 elements before said index, but does not wrap around (doesn't go beyond the 1 in this case). And also consists of m=2 elements after said index.
Thank you.
You can use this:
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9,10])
x = 3
n = 5
m = 2
arr[max(0, x-n):x+m+1]
# array([1, 2, 3, 4, 5, 6])
This question already has answers here:
Is it possible to use argsort in descending order?
(10 answers)
Closed 4 years ago.
import numpy as np
mat = np.array([[1,21,3],[5,4,2],[56,12,4]])
mat_sort = mat[mat[:,2].argsort()]
print(mat_sort)
Output:
[[ 5 4 2]
[56 12 4]
[ 1 21 3]]
If I wish to get the reverse sorting based on any column, say 3rd, what changes do i make to the code? Meaning, I wish to get:
[[56 12 4]
[ 1 21 3]
[ 5 4 2]]
P.s Yes I understand this is an easy question but I couldn't find an answer that I understood and was based for matrix and not an array or vector. TIA :)
Just reverse the argsort indices:
mat_sort = mat[mat[:, 2].argsort()[::-1]]
print(mat_sort[::-1]) #just print in reverse