This question already has answers here:
How to filter numpy array by list of indices?
(5 answers)
Closed 2 years ago.
A very basic question but I cannot find similar question in here og by googling.
tmp = np.array([1,2,3,4,5])
I can extract 2 by tmp[1] and 2 to 4 by tmp[1:4]
Suppose I want to extract 2 AND 4. What is the easiest way to do that?
You can use .take()
import numpy as np
tmp = np.array([1,2,3,4,5]).take([1,4])
# Out[4]: (2, 5)
Related
This question already has answers here:
NumPy stack or append array to array
(3 answers)
Closed 5 months ago.
I have three numpy arrays:
a1=np.array([5.048e-02, 2.306e+00, 0.000e+00])
a2=np.array([1.018e-01, 4.077e+00, 0.100e+00])
a3=np.array([1.02e-01, 5.077e+00, 0.200e+00])
As a combined result I would like to have:
array(
[5.048e-02, 1.018e-01, 1.02e-01],
[2.306e+00, 4.077e+00, 5.077e+00],
[0.000e+00, 0.100e+00, 0.200e+00]
)
How can I do this with numpy?
(Please excuse me for the error.)
np.array([a1,a2,a3])
Just create a new numpy array from those three individual array.
This question already has answers here:
Insert element into numpy array
(3 answers)
Closed 9 months ago.
Consider the following: If I have a 1 dimensional array like the following
import numpy as np
x=np.array([1,2,4,5])
Say now that I have the number 3, and wish to enter it into the 3rd position of the array (or 2nd position in "python language") without distrubing other entries so that we once we call array again after the function, we obtain:
x=np.array([1,2,3,4,5])
I'm looking for some kind of, for a lack of a better word, "splicing method" involving the array x and the number 3.
Thank you!
You can use numpy.insert
numpy.insert(arr, obj, values, axis=None)
arr -> array you want your value/values inserted to
obj -> could be an integer, a slice or a sequence of values
values -> as you can imagine, the value/values you want to insert
axis -> it works also for multi-dimensional array
Here's the solved example that you have proposed:
import numpy as np
idx = 2
val = 3
x = np.array([1,2,4,5])
x = np.insert(x,idx,val,axis=0)
Here's the result
pre-insert : [1 2 4 5]
post-insert : [1 2 3 4 5]
As you can see, this method will put in the position idx=2 of the array x in the value val=3
This question already has answers here:
Can NumPy take care that an array is (nonstrictly) increasing along one axis?
(2 answers)
Closed 3 years ago.
I have a 2d numpy array of the form:
array = [[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]]
I'd like to go to each of the rows, iterate over the entries until the value 1 is found, then replace every subsequent value in that row to a 1. The output would then look like:
array = [[0,0,0,1,1], [0,1,1,1,1], [1,1,1,1,1]]
My actual data set is very large, so I was wondering if there is a specialized numpy function that does something like this, or if there's an obvious way to do it that I'm missing.
Thanks!
You can use apply.
import numpy as np
array = np.array([[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]])
def myfunc(l):
i = 0
while(l[i]!=1):
i+=1
return([0]*i+[1]*(len(l)-i))
print(np.apply_along_axis(myfunc, 1, array))
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
The labels column in my test['labels'] dataframe, looks like:
0 ['Edit Distance']
1 ['Island Perimeter']
2 ['Longest Substring with At Most K Distinct Ch...
3 ['Valid Parentheses']
4 ['Intersection of Two Arrays II']
5 ['N-Queens']
For each value in the column, which is a string representation of list ("['Edit Distance']"), I want to apply the function below to convert it into an actual list.
ast.literal_eval(VALUE HERE)
What is a straightforward way to do this?
Use:
import ast
test['labels'] = test['labels'].apply(ast.literal_eval)
print (test)
labels
0 [Edit Distance]
1 [Island Perimeter]
2 [Longest Substring with At Most K Distinct Ch]
3 [Valid Parentheses]
4 [Intersection of Two Arrays II]
5 [N-Queens]
This question already has answers here:
numpy subtract every row of matrix by vector
(3 answers)
Closed 5 years ago.
I have a 4 x 2 matrix, i.e. a numpy vector(of length 4) of numpy vectors of length two. For example a = [[1,1],[1,2],[3,5],[8,3]]
I want to subtract the vector b = [3,6] from each row.
I tried to do the following:
np.vectorize(lamda x: x-b)(a)
but i get the error ValueError:
setting an array element with a sequence.
Can somebody explain me why and how to do this the right way?
first convert them to numpy array and then subtract b from a:
a = np.asarray(a)
b = np.asarray(b)
print a - b