Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能把它转换成矩阵_Python_Numpy_Matrix - Fatal编程技术网

Python 我怎样才能把它转换成矩阵

Python 我怎样才能把它转换成矩阵,python,numpy,matrix,Python,Numpy,Matrix,我的test.txt文件包括: 2 2 4 1 1 3 4 1 1 1 1 1 4 1 2 2 2 1 3 2 3 2 1 1 我的Python代码: import numpy as np from numpy.linalg import import os A=np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt") n=3 for i in range(0,n): b=[row[i] for row in A] D=n

我的test.txt文件包括:

2 2 4
1 1 3 
4 1 1 
1 1 1 
4 1 2
2 2 1
3 2 3
2 1 1
我的Python代码:

import numpy as np
from numpy.linalg import 
import os
A=np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt")
n=3 
for i in range(0,n):
    b=[row[i] for row in A]
    D=np.array(b)
    size=[len(D),np.max(D)]
    B=np.zeros(size)
    for i in range(len(D)):
        idx=D[i]-1
        B[i,idx]=1
        U=B.transpose(1, 0)

    print U
通过上面写的代码,这些代码告诉我:

[[ 0.  1.  0.  1.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  1.  0.  1.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  1.  0.  0.  0.]]
[[ 0.  1.  1.  1.  1.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  1.  1.  0.]]
[[ 0.  0.  1.  1.  0.  1.  0.  1.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  1.  0.]
[ 1.  0.  0.  0.  0.  0.  0.  0.]]
这不是矩阵!我想如下转换矩阵,但每次尝试都失败了

[[ 0.  1.  0.  1.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  1.  0.  1.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.]
 [ 0.  0.  1.  0.  1.  0.  0.  0.]
 [ 0.  1.  1.  1.  1.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  1.  1.  0.]
 [ 0.  0.  1.  1.  0.  1.  0.  1.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  1.  0.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.]]

试着这样做:

NewU=list()
for i in U:
    NewU+=i
print NewU

我不熟悉numpy,所以我不确定它是否能工作,但值得一试。

我建议用以下内容替换for循环:

A = np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt").T

s = A.max(axis=1).cumsum()
data = np.zeros((int(s[-1]), A.shape[1]))

A[1:] += s[:2, None]
i = (A - 1).astype(int)
j = np.tile(np.arange(A.shape[1]), (A.shape[0], 1))

data[i, j] = 1
因此,
>>数据
给出:

array([[ 0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  1.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  0.,  0.,  1.],
       [ 1.,  0.,  0.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  1.,  0.,  1.,  0.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
这应该更快。如果要继续使用较慢的for循环解决方案,则应
vstack
生成的阵列:

A=np.loadtxt("C:\Users\KEMAL\Desktop\piton\test.txt")
n=3
arrs = []
for i in range(0,n):
    b=[row[i] for row in A]
    D=np.array(b)
    size=[len(D),np.max(D)]
    B=np.zeros(size)
    for i in range(len(D)):
        idx=D[i]-1
        B[i,idx]=1
        U=B.transpose(1, 0)

    arrs.append(U)

np.vstack(arrs)

对不起,我的标题是“如何将其转换为矩阵…
a
from
loadtxt
应为2d数组(让我们跳过
矩阵
名称)。用语言描述你想要完成的事情。
U
应该是什么?看起来您正在打印4个
U
实例,每个实例都是2d数组。我很高兴能提供帮助!