python中如何在矩阵中添加一列或是一行??
发布网友
发布时间:2022-04-23 00:31
我来回答
共5个回答
懂视网
时间:2022-05-10 11:13
下面就为大家分享一篇python 矩阵增加一行或一列的实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
矩阵增加行
np.row_stack() 与 np.column_stack()
import numpy as np
a = np.array([[4, 4,], [5, 5]])
c = np.row_stack((a, [8,9]))
d = np.column_stack((a, [8,9]))
热心网友
时间:2022-05-10 08:21
例如文件t.data数据格式如下 1,2,3 4,5,6 7,8,9 //读入文件 file=open("t.data","r") //初始化矩阵 matrix=[] //读入数据并加到矩阵中 for line in file: line.strip() matrix.append(line.split(',')) //打印 print(matrix)
热心网友
时间:2022-05-10 09:39
>>> import numpy as np
>>> a = np.arange(1,11).reshape(10,1)
>>> b = a * 1.1
>>> c = a / 1.1
>>> a
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10]])
>>> b
array([[ 1.1],
[ 2.2],
[ 3.3],
[ 4.4],
[ 5.5],
[ 6.6],
[ 7.7],
[ 8.8],
[ 9.9],
[ 11. ]])
>>> c
array([[ 0.90909091],
[ 1.81818182],
[ 2.72727273],
[ 3.63636364],
[ 4.54545455],
[ 5.45454545],
[ 6.36363636],
[ 7.27272727],
[ 8.18181818],
[ 9.09090909]])
>>> x = np.array([
... np.reshape(a, len(a)),
... np.reshape(b, len(b)),
... np.reshape(c, len(c))
... ]).transpose()
>>> x
array([[ 1. , 1.1 , 0.90909091],
[ 2. , 2.2 , 1.81818182],
[ 3. , 3.3 , 2.72727273],
[ 4. , 4.4 , 3.63636364],
[ 5. , 5.5 , 4.54545455],
[ 6. , 6.6 , 5.45454545],
[ 7. , 7.7 , 6.36363636],
[ 8. , 8.8 , 7.27272727],
[ 9. , 9.9 , 8.18181818],
[ 10. , 11. , 9.09090909]])
>>>
is it?
热心网友
时间:2022-05-10 11:14
a = [[1],
[2],
[3]]
b = [[4],
[5],
[6]]
c = [[7],
[8],
[9]]
res = map(lambda x,y,z: [x[0],y[0],z[0]], a, b, c)
print res
如果是行的话:
a = [[1,2,3]]
b = [[4,5,6]]
c = [[7,8,9]]
res = a+b+c
print res
热心网友
时间:2022-05-10 13:05
np.hstack