Numpy similar way of indexing as matlab [duplicate] - python

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]

Related

Numpy array masking(Python) [duplicate]

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 to slice numpy array starting from x-n elements? [duplicate]

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])

Python three numpy arrays, combine columns [duplicate]

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.

Reverse sort of matrix using numpy based on a specific column [duplicate]

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

Converting DataFrame in Python [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 4 years ago.
Considering the following dataframe:
import pandas as pd
import numpy as np
import random
np.random.seed(10)
df1 = pd.DataFrame({'x':[1,2,3,4,5,1,2,3,4,5],
'y':[10,10,10,10,10,20,20,20,20,20],
'z':np.random.normal(size = 10)
})
I want to convert the x values into columns and y values into index (decreasing) with corresponding z values in the dataframe. It's something like this df2:
df2 = pd.DataFrame(np.random.randn(2,5), index = [20,10], columns=[1,2,3,4,5])
How can I conver df1 into df2's style?
You can use pandas.pivot_table:
res = df1.pivot_table(index='y', columns='x', values='z')
You may wish to remove or change your index names, but this is your result:
x 1 2 3 4 5
y
10 1.331587 0.715279 -1.545400 -0.008384 0.621336
20 -0.720086 0.265512 0.108549 0.004291 -0.174600

Categories