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])
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:
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.
This question already has answers here:
remove zero lines 2-D numpy array
(4 answers)
Closed 6 years ago.
I'm trying to write a function to delete all rows in which have a zero value in.
This is not from my code, but an example of the idea I am using:
import numpy as np
a=np.array(([7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0]))
b=[]
for i in range(len(a)):
for j in range (len(a[i])):
if a[i][j]==0:
b.append(i)
print 'b=', b
for zero_row in b:
x=np.delete(a,zero_row, 0)
print 'a=',a
and this is my output:
b= [1, 3]
a= [[7 1 2 8]
[4 0 3 2]
[5 8 3 6]
[4 3 2 0]]
How do I get rid of the rows with the index in b?
Sorry, I'm fairly new to this any help would be really appreciated.
I'm trying to write a function to delete all rows in which have a zero value in.
You don't need to write a function for that, it can be done in a single expression:
>>> a[np.all(a != 0, axis=1)]
array([[7, 1, 2, 8],
[5, 8, 3, 6]])
Read as: select from a all rows that are entirely non-zero.
Looks like np.delete does't change the array, just returns a new array, so
Instead of
x = np.delete(a,zero_row, 0)
try
a = np.delete(a,zero_row, 0)
I think I have found the answer:
as #tuxcanfly said I changed x to a.
Also I have now removed the for loop as it removed the row with index 2 for some reason.
Instead I now just chose to delete the rows using b as the delete function with use the elements in the list to remove the row with that index.
the new code:
import numpy as np
a=np.array(([7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0]))
b=[]
for i in range(len(a)):
for j in range (len(a[i])):
if a[i][j]==0:
b.append(i)
print 'b=',b
for zero_row in b:
a=np.delete(a,b, 0)
print 'a=',a
and the output:
b= [1, 3]
a= [[7 1 2 8]
[5 8 3 6]]
I think this helps readability (and allows you to loop once, not twice):
#!/usr/bin/env python
import numpy as np
a = np.array(([7,1,2,8], [4,0,3,2], [5,8,3,6], [4,3,2,0]))
b = None
for row in a:
if 0 not in row:
b = np.vstack((b, row)) if b is not None else row
a = b
print 'a = ', a
In this version, you loop over each row and test for 0's membership in the row. If the row does not contain a zero, you attempt to use np.vstack to append the row to an array called b. If b has not yet been assigned to, it is initialized to the current row.