打印numpy的数组作为表格表

问题描述 投票:0回答:1

我试图与基地蟒蛇做到这一点...我知道有模块,这将做到这一点。

以下是所希望的结果,我想有一个打印从控制台或终端具有相等的间距。像下面的东西是期望的,更小的背景颜色。黑色是头,该表是返回numpy的阵列的只是行。

enter image description here

代码:import numpy的为NP

A=np.array([[6.0,-2.0,1.0],[-2.0,7.0,2.0],[1.0,2.0,-5.0]])
b=np.array([11.0,5.0,-1.0])

class Solution:

    def print_out(temp_d):
        temp_report=np.zeros((np.shape(temp_d[0])[0],len(temp_d.keys())))
        i=0
        for key,val in temp_d.items(): 
            for j in range(len(val)):
                temp_report[j][i]=val[j]
            i+=1
        return temp_report

    def Jacobi(A,b,N=25,guess=None,threshold=0.000001):

        ### Initialize dictionary
        temp_d={}
        ### Set lag to zero for current and prior value comparison.
        x2=0
        ### Create components.
        D=np.zeros(A.shape,float)
        np.fill_diagonal(D,np.diag(A))
        L=np.tril(A,k=-1)
        U=np.triu(A,k=1)

        c=np.diag(np.linalg.inv(D)*b).reshape(A.shape[0],1)
        G=np.diag(np.linalg.inv(D)).reshape(A.shape[0],1)*(L+U)*-1

        if guess is None:
            x=np.zeros(A.shape[0])

        for i in range(N):
            x=c+np.dot(G,x)
            if abs(np.sum(x)-np.sum(x2)) < threshold:
                break        
            x2=x

            temp_d[i]=np.diag(x)

        report_array=Solution.print_out(temp_d)

        return report_array

jacobi_solution=Solution.Jacobi(A,b,N=15,guess=None)

print(jacobi_solution)  
python numpy printing formatting
1个回答
0
投票

我想加入以下代码在你的到底会做你想要什么。

header = np.array(range(0,15))
#
with np.printoptions(linewidth=160,formatter={'int': '{:8d}'.format}):
    print(header)
with np.printoptions(linewidth=160,formatter={'float': '{: 0.5f}'.format}):    
    for line in range(0,jacobi_solution.shape[0],1):
        print(jacobi_solution[line,:])
© www.soinside.com 2019 - 2024. All rights reserved.