I have a 60000 by 200 numpy array. I want to make it 60000 by 201 by adding a column of 1's to the right. (so every row is [prev, 1]) Concatenate with axis = 1 doesn't work because it seems like concatenate requires all input arrays to have the same dimension. How should I do this? I can't find any existing useful answer, and most of the answers about this were written a few years ago so things might be different now.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Let me just throw in a very simple example with much smaller size. The principle should be the same.
Under cover all the
stack
variants (includingappend
andinsert
) end up doing aconcatenate
. They just precede it with some sort of array reshape.Here I made a (3,1) array of 1s, to match the (3,4) array. If I wanted to add a new row, I'd make a (1,4) array.
While the variations are handy, if you are learning, you should become familiar with
concatenate
and the various ways of constructing arrays that match in number of dimensions and necessary shapes.The first thing to think about is that
numpy
arrays are really not meant to change size. So you should ask yourself, can you create your original matrix as 60k x 201 and then fill the last column afterwards. This is usually best.If you really must do this, see How to add column to numpy array
I think the numpy method column_stack is more interesting because you do not need to create a column numpy array to stack it in the matrix of interest. With the column_stack you just need to create a normal numpy array.