COUPLING DATA IN COLUMN DIRECTION

Asked 2 years ago, Updated 2 years ago, 50 views

Up until now, we have connected the two data in the column direction using the following code.
Each row * column has data1 in the form of 2*32, data2 in the form of 5*32, and data3 in the form of 8*32.
For data1 and data2 only, I could do the following. (7*32 shape)

import numpy as np
# Verify index has been deleted
data1=np.loadtxt('210122-945 and 1200-del-stand-t-all.csv', delimiter=", ")
data2=np.loadtxt('210123-945 and 1200-del-stand-t-pre.csv', delimiter=", ")
data=np.append(data1,data2,axis=0)
print(data)

This time, when I connected more than three pieces of data with the following code, an error got caught in Axis=0. I'm aiming for the form of 15*32.

import numpy as np
# Verify index has been deleted
data1=np.loadtxt('210122-945 and 1200-del-stand-t-all.csv', delimiter=", ")
data2=np.loadtxt('210123-945 and 1200-del-stand-t-pre.csv', delimiter=", ")
data3=np.loadtxt('210124-945 and 1200-del-stand-t-pre.csv', delimiter=",")
data=np.append(data1,data2,data3,axis=0)
print(data)

How can I get there smoothly? Can't I use append in the first place?

python python3 numpy

2022-09-30 16:40

1 Answers

This article will be helpful.
Combine the NumPy array ndarray (concatenate, stack, block, etc.)
How To Concatenate Arrays in NumPy?

numpy.append(arr,values,axis=None) can only specify two arrays, so

Parameters

  • arr:array_like
    Values are attached to a copy of this array.
  • values:array_like
    These values are applied to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis).Ifaxis is not specified, values can be any shape and will be flapped before use.
  • axis:int, optional
    The axis along which values are applied. If axis is not given, both arr and values are flat before use.

You can use numpy.concatenate(a1,a2,...),axis=0,out=None) to concatenate.(It can be used in two situations)

Parameters

  • a1,a2,...:sequence of array_like
    The arrays must have the same shape, except in the dimension correcting to axis (the first, by default).
  • axis:int, optional
    Theaxis along which the arrays will be joined.Ifaxis is None, arrays are flat before use.Default is 0.
  • out:ndarray, optional
    If provisioned, the destination to place the result.The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

For the source of the question:

data=np.append(data1,data2,data3,axis=0)

You can do it if you use a list or a tuple like this:

 data=np.concatenate([data1,data2,data3],axis=1)

or:

data=np.concatenate(data1,data2,data3),axis=1)

You can also do the same with numpy.hstack(tup), which eliminates the need to specify axis=.

Parameters

  • tup:sequence of ndarrays
    The arrays must have the same shape all but the second axis, except1-Darrays which can be any length.

containateSame as this:

data=np.hstack([data1,data2,data3])

or:

data=np.hstack(data1,data2,data3))

Incidentally, if "coupling in the column direction" is a misunderstanding and actually "coupling in the row direction", the axis=1 parameter in containate should be axis=0 or


2022-09-30 16:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.