在Python中增加矩阵的维数

时间:2019-03-02 20:55:23

标签: python data-science

我正在努力增加矩阵的尺寸,即添加1的第0列。我调整了3 * 3矩阵的形状,并在第一列上添加了1的列。现在,当我乘以X(transpose)* X时,它显示错误,即

错误:
“形状(3,47,1)和(1,47,3)未对齐:1(dim 2)!= 47(dim 1)”

这是我的代码

import numpy as np
data= open("data2.txt","r")
x=[]
y=[]

oneslist = []     对于我在范围(47)中:         oneslist.append(1)

rows=47
cols=2
a = [[0] * cols for i in range(rows)]
b= [[0] * 1 for i in range(rows)]

i=-1

for line in data:
    i=i+1
    xi,yj,z = line.split(",")
    #y.append(yj)
    if i<rows:
        for j in range(cols):
            if j==0:
                a[i][j] =  (xi)
                b[i][j]=   (z.strip())
            else:
                a[i][j]=   (yj)

a=np.array(a)

b=np.array(b)
z=np.array(oneslist)


pricevector=a[np.newaxis]        

pricevector = pricevector.reshape((1,pricevector.shape[1], 
pricevector.shape[2]))
pricevectorr=np.insert(pricevector,0,oneslist,2)

pricevectortranspose= pricevectorr.T

pricevectortranspose=np.array(pricevectortranspose, dtype = float)
pricevectorr=np.array(pricevectorr, dtype = float)
XtX=(pricevectortranspose).dot(pricevectorr)

0 个答案:

没有答案