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.
Related
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)
This question already has answers here:
Flatten numpy array
(2 answers)
Closed 2 years ago.
My issue is this. I have an array like this :
test = array([[1],[2],[10] .... [-5]])
I want to be able to just convert this to an array that looks like this
test = [1,2,10,..., -5]
The test is a numpy array.
You can use reshape() to change the array into 1D array as:
import numpy as np
test = np.array([[1],[2],[10],[-5]])
test=test.reshape((test.shape[0],)) # test.shape[0] gives the first dimension (the number of rows)
# reshape changes test array to one dimensional array with shape (test.shape[0],)
print(test)
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:
Pandas reverse of diff()
(6 answers)
Closed 4 years ago.
I converted my timeseries into stationary time series with differentiation
data['consumption_diff'] = data.consumption-data.consumption.shift(1)
How can I convert consumption_diff back into consumption?
You can use numpy's "r_" object which concatenates and flattens arrays and the "cumsum()" function which cumulatively sums values.
import numpy as np
undiffed = np.r_[data.consumption.iloc[0], data.consumption_diff.iloc[1:]].cumsum()
That is how you can undiff timeseries data and can be helpful if you've done a prediction into future dates that you need to undiff. However, you already have the undiffed values: data.consumption are your original undifffed data.
This question already has answers here:
How to add a new row to an empty numpy array
(7 answers)
Closed 5 years ago.
I need to create an empty numpy array of a shape (?, 10, 10, 3). ? means I don't know how many elements will be inserted. Then I have many numpy arrays of a shape (1, 10, 10, 3) which I want to be inserted to the prepared array one by one, so the ? mark will be increasing with inserted elements.
I'm trying all variations of numpy array methods like empty, insert, concatenate, append... but I'm not able to achieve this. Could you please give me a hand with this?
Using np.append works:
import numpy as np
mat = np.empty((0,10,10,3))
array = np.random.rand(1,10,10,3)
mat = np.append(mat, array, axis=0)
mat = np.append(mat, array, axis=0)
print(mat.shape)
>>>(2,10,10,3)