This question already has answers here:
Concatenating two one-dimensional NumPy arrays
(6 answers)
Closed 5 years ago.
I have written this code to append two numpy arrays:
td_XN = searchNegative(X,y,10)
td_XP = searchPosotive(X,y,10)
print(np.array(td_XN).shape, np.array(td_XP).shape)
print(type(td_XN), type(td_XP))
td_X = np.concatenate(td_XP, td_XN)
td_y = [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
print(td_X.shape, len(td_y))
However, it produces this error:
TypeError: only length-1 arrays can be converted to Python scalars
On this line:
td_X = np.concatenate(td_XP, td_XN)
If you want to concatenate side by side (that is, create an array of 10 -by- 2544*2): you can do
td_X = np.concatenate([td_XP, td_XN],axis=1)
For example
td_X = np.concatenate([[[1,2,3,7],[4,5,6,8]],[[1,2,3],[4,5,6]]],axis=1)
gives
array([[1, 2, 3, 7, 1, 2, 3],
[4, 5, 6, 8, 4, 5, 6]])
On the other hand, if you want to add td_XN below td_XP you can do
td_X = np.concatenate([td_XP, td_XN],axis=0)
For example,
td_X = np.concatenate([[[1,2,3],[4,5,6]],[[1,2,7],[4,5,8]]],axis=0)
gives
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 7],
[4, 5, 8]])
Related
This question already has answers here:
Roll rows of a matrix independently
(6 answers)
Closed 4 months ago.
Is there an elegant way to perform a per-line roll of a numpy array?
Example:
>>> arr
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> desired_func(arr, shift=np.array([1, 2]))
array([[4, 1, 2, 3],
[7, 8, 5, 6]])
The function np.roll doesn't seem to allow that.
You can use fancy indexing:
# define the roll per row
roll = np.array([1, 2])
# compute the rolled indices
idx = (np.arange(arr.shape[1]) - roll[:,None]) % arr.shape[1]
# index as 2D
out = arr[np.arange(arr.shape[0])[:,None], idx]
output:
array([[4, 1, 2, 3],
[7, 8, 5, 6]])
This question already has answers here:
How to make a multidimension numpy array with a varying row size?
(7 answers)
Closed 1 year ago.
I want to stack arrays with this code.
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([7, 8])
np.stack((a, b), axis=-1)
But it returns
ValueError: all input arrays must have the same shape error.
I expect the output to be:
array([[[1, 2, 3], 7],
[[4, 5, 6], 8]])
I don't think that's a valid numpy array. You could probably do this by letting the array's dtype be an object (which could be anything, including a ragged sequence, such as yours).
data = [[[1, 2, 3], 7], [[4, 5, 6], 8]]
ar = np.array(data, dtype=object)
To build data, you can do:
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([7, 8])
data = [[_a, _b] for _a, _b in zip(a, b)]
This question already has answers here:
concatenate numpy arrays which are elements of a list
(2 answers)
Closed 2 years ago.
I have a list of numpys of the same length each. for example:
my_list = [np.array([2, 3, 5, 5]),
np.array([5, 4, 1, 4]),
np.array([8, 4, 5, 1]),
np.array([7, 4, 5, 1])]
I want to turn the list into 2d numpy:
[[2, 3, 5, 5],
[5, 4, 1, 4],
[8, 4, 5, 1],
[7, 4, 5, 1]]
The following code does perform the operation but in a sloppy manner.
The result is also not arranged in the desired order:
combined = []
for i in my_list :
if len(combined) == 0:
combined = i
else:
combined = np.vstack((i,combined))
print(combined)
What needs to be changed to get the desired result?
The most straightforward way
np.vstack(my_list)
or
np.concatenate(my_list).reshape(len(my_list),-1)
Simply doing np.array(my_list) can get the job done.
This question already has answers here:
Finding indices of matches of one array in another array
(4 answers)
Closed 5 years ago.
Given an array of integers, I need to find the indexes of many of its elements stored in a different array. This is:
import numpy as np
a1 = np.array([ 4, 5, 6, 1, 2, 3, 7, 86, 9, 15])
a2 = np.array([ 2, 3, 5, 6, 9])
Where a1 is my initial array of elements, and a2 is the array that contains the elements for which I need their indexes in a1.
In this case, the result should be:
a3 = ([4, 5, 1, 2, 8])
This seems like a rather simple operation, but I haven't been able to figure out how to do it.
you may try this:
In [378]: (a1[:, None] == a2).argmax(axis=0)
Out[378]: array([4, 5, 1, 2, 8], dtype=int64)
This question already has answers here:
How to flatten a hetrogenous list of list into a single list in python? [duplicate]
(11 answers)
Closed 5 years ago.
I have a name list, and I want to construct a new list of the same type of data inside the list. How can I do it? What if it is a numpy array?
x=[1,2,3,4]
newx=[x,5,6]
[[1, 2, 3, 4], 5, 6]
#numpy
y=np.array([0,1,2,3,4])
newy=[y,5,6]
[array([0, 1, 2, 3, 4]), 5, 6]
Desired output
[1, 2, 3, 4, 5, 6]
For mere Python lists you do:
x = [1, 2, 3, 4]
x += [5, 6]
>>> [1, 2, 3, 4, 5, 6]
and for numpy-arrays:
x = np.array([1, 2, 3, 4])
x = np.concatenate((x, np.array([5, 6])))
>>> np.array([1, 2, 3, 4, 5, 6])
Mind the double parentheses in np.concatenate, since the arguments must be passed as a tuple.