Connecting an array of numpys [duplicate] - python

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.

Related

Per line numpy roll [duplicate]

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

How can I add same index of element of multiple arrays inside a huge array? [duplicate]

This question already has answers here:
In Python, how to numerically sum nested lists of integers: [[1,0], [1,1], [1,0]] → [3,1]
(4 answers)
Closed last year.
what I'm trying to do is to create a new array and each element of the new array contains the sum of all the values of the same index of arrays inside the big array.
'''
array = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
(length of array inside =5)
new = [4,8,12,16,20]
'''
But the length of the arrays inside the big array is identical but can be flexible
You have two solutions to it. You can use sum for every "little array" with it :
array = [sum([1, 2, 3, 4, 5]), sum([1, 2, 3, 4, 5]), sum([1, 2, 3, 4, 5]), sum([1, 2, 3, 4, 5])]
The other solutions is create variables lists and use append when you like.
array_one=sum([1, 2, 3, 4, 5])
array_two=sum([1, 2, 3, 4, 5])
array_total=[]
array_total.append(array_one)
array_total.append(array_two)
If you want the sum of this arrray_total you can use sum(array_total) too

Double elements in Python Numpy Array [duplicate]

This question already has answers here:
Repeating each element of a numpy array 5 times
(2 answers)
Closed 4 years ago.
I'm trying to copy every element into a 2d array in Python such that it doubles the size of the array and adds the element directly after the element that it intends to copy.
For example:
[[1,2,3],
[4,5,6],
[7,8,9]]
becomes
[[1,1,2,2,3,3],
[4,4,5,5,6,6],
[7,7,8,8,9,9]]
Can anyone help with this problem? Thanks!
You can use np.repeat(..) [numpy-doc] for this:
>>> import numpy as np
>>> np.repeat(a, 2, axis=1)
array([[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6],
[7, 7, 8, 8, 9, 9]])
We thus repeat, for the second axis (axis=1) the elements two times.
We can also use list-comprehension, but given that the data has the same time, using numpy is faster, and more declarative:
times2 = [[xi for x in row for xi in [x, x]] for row in a]
This the produces:
>>> [[xi for x in row for xi in [x, x]] for row in a]
[[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6], [7, 7, 8, 8, 9, 9]]

Create new list (or numpy.array) with a named list (or numpy.array) [duplicate]

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.

Concatenate two numpy arrays [duplicate]

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

Categories