在python中向矩阵添加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32827269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:20:22  来源:igfitidea点击:

Adding columns to matrix in python

pythonnumpy

提问by

Is there some way in python to add columns into a matrix.I want to add a column in the beginning of my mxnmatrix in python.Say for example,I have 1000x100matrix and I want to make it into 1000x101matrix. I want to insert new column having all onesin the beginning i.e. it will be my new first column. Is it possible in python?

python中是否有某种方法可以将列添加到矩阵中。我想在python中的矩阵的开头添加一列。mxn比如说,我有1000x100矩阵,我想把它变成1000x101矩阵。我想插入一ones开始就包含所有内容的新列,即它将成为我新的第一列。在python中可能吗?

Here is my code- vector1is a list, cntis 1000

这是我的代码 - vector1是一个列表,cnt是 1000

data=np.array(vector1)  
shape = ( cnt, 100 )
data=data.reshape(shape)

Now to this I want to add a new column at beginning with all ones

现在,我想在所有列开头添加一个新列

采纳答案by YuppieNetworking

The function you are looking for in numpy.hstackand numpy.ones:

您正在寻找的功能numpy.hstacknumpy.ones

For example,

例如,

import numpy as np

X = np.random.uniform(size=(10,3))
n,m = X.shape # for generality
X0 = np.ones((n,1))
Xnew = np.hstack((X,X0))

print(X)
[[ 0.78614426  0.24150772  0.94330932]
 [ 0.60088812  0.20427371  0.19453546]
 [ 0.31853252  0.31669057  0.82782995]
 [ 0.71749368  0.54609844  0.74924888]
 [ 0.86883981  0.54634575  0.83232409]
 [ 0.89313181  0.8006561   0.05072146]
 [ 0.79492088  0.07750024  0.45762175]
 [ 0.92350837  0.20587178  0.76987197]
 [ 0.0092076   0.0044617   0.04673518]
 [ 0.69569363  0.3315923   0.15093861]]

print(X0)
[[ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]
 [ 1.]]

print(Xnew)
[[ 0.78614426  0.24150772  0.94330932  1.        ]
 [ 0.60088812  0.20427371  0.19453546  1.        ]
 [ 0.31853252  0.31669057  0.82782995  1.        ]
 [ 0.71749368  0.54609844  0.74924888  1.        ]
 [ 0.86883981  0.54634575  0.83232409  1.        ]
 [ 0.89313181  0.8006561   0.05072146  1.        ]
 [ 0.79492088  0.07750024  0.45762175  1.        ]
 [ 0.92350837  0.20587178  0.76987197  1.        ]
 [ 0.0092076   0.0044617   0.04673518  1.        ]
 [ 0.69569363  0.3315923   0.15093861  1.        ]]

回答by Sukesh Kaul

I found the numpy.c_function pretty handy at adding a column to an matrix. The following code would add a column with all zeros to the matrix.

我发现该numpy.c_函数在向矩阵中添加一列时非常方便。以下代码将向矩阵添加一个全为零的列。

import numpy as np

np.c_[np.ones((100,1)),X]

Here, Xis the original matrix.

这里,X是原始矩阵。

回答by Navin Shrinivas

data=np.loadtxt('ex1data1.txt',delimiter=',')
X=data[:,0]
Y=data[:,1]
X=(np.column_stack((np.ones(np.size(X)),X)))

printing x gives

打印 x 给出

[[ 1.      6.1101]
 [ 1.      5.5277]
 [ 1.      8.5186]
 [ 1.      7.0032]
 [ 1.      5.8598]
 [ 1.      8.3829]
 [ 1.      7.4764]
 [ 1.      8.5781]
 [ 1.      6.4862]
 [ 1.      5.0546]
 [ 1.      5.7107]
 [ 1.     14.164 ]
 [ 1.      5.734 ]
 [ 1.      8.4084]
 [ 1.      5.6407]
 [ 1.      5.3794]
 [ 1.      6.3654]
 [ 1.      5.1301]
 [ 1.      6.4296]
 [ 1.      7.0708]
 [ 1.      6.1891]
 [ 1.     20.27  ]
 [ 1.      5.4901]
 [ 1.      6.3261]
 [ 1.      5.5649]
 [ 1.     18.945 ]
 [ 1.     12.828 ]
 [ 1.     10.957 ]
 [ 1.     13.176 ]
 [ 1.     22.203 ]
 [ 1.      5.2524]
 [ 1.      6.5894]
 [ 1.      9.2482]
 [ 1.      5.8918]
 [ 1.      8.2111]
 [ 1.      7.9334]
 [ 1.      8.0959]
 [ 1.      5.6063]
 [ 1.     12.836 ]
 [ 1.      6.3534]
 [ 1.      5.4069]
 [ 1.      6.8825]
 [ 1.     11.708 ]
 [ 1.      5.7737]
 [ 1.      7.8247]
 [ 1.      7.0931]
 [ 1.      5.0702]
 [ 1.      5.8014]
 [ 1.     11.7   ]
 [ 1.      5.5416]
 [ 1.      7.5402]
 [ 1.      5.3077]
 [ 1.      7.4239]
 [ 1.      7.6031]
 [ 1.      6.3328]
 [ 1.      6.3589]
 [ 1.      6.2742]
 [ 1.      5.6397]
 [ 1.      9.3102]
 [ 1.      9.4536]
 [ 1.      8.8254]
 [ 1.      5.1793]
 [ 1.     21.279 ]
 [ 1.     14.908 ]
 [ 1.     18.959 ]
 [ 1.      7.2182]
 [ 1.      8.2951]
 [ 1.     10.236 ]
 [ 1.      5.4994]
 [ 1.     20.341 ]
 [ 1.     10.136 ]
 [ 1.      7.3345]
 [ 1.      6.0062]
 [ 1.      7.2259]
 [ 1.      5.0269]
 [ 1.      6.5479]
 [ 1.      7.5386]
 [ 1.      5.0365]
 [ 1.     10.274 ]
 [ 1.      5.1077]
 [ 1.      5.7292]
 [ 1.      5.1884]
 [ 1.      6.3557]
 [ 1.      9.7687]
 [ 1.      6.5159]
 [ 1.      8.5172]
 [ 1.      9.1802]
 [ 1.      6.002 ]
 [ 1.      5.5204]
 [ 1.      5.0594]
 [ 1.      5.7077]
 [ 1.      7.6366]
 [ 1.      5.8707]
 [ 1.      5.3054]
 [ 1.      8.2934]
 [ 1.     13.394 ]
 [ 1.      5.4369]]

I found column_stack working flawlessly...hope it helps

我发现 column_stack 工作完美......希望它有帮助