2017-03-12 77 views
1

如何打印3列表(索引,协方差矩阵,均方误差)?Jupyter Notebook中的打印表

from sklearn import linear_model # Machine Learning tool 
import numpy as np # Mathematics and Linear Algebra tool 
import pandas as pd # data structure tool 
import matplotlib.pyplot as plt # scientific plotting tool 
import seaborn as sns # # scientific plotting tool 
%matplotlib inline 

from sklearn import datasets, linear_model 
from sklearn.metrics import mean_squared_error 

diabetes = datasets.load_diabetes() # Load the diabetes dataset 
n = 10 # 10 datasets for analysis 
y_train = diabetes.target[:-20] 
y_test = diabetes.target[-20:] 
MSE = np.empty([n,1]) # mean square error 
COV = [None] * n # covariance 
regr = [None] * n 
table= [None] * n 
for i in range(n): 
    x = diabetes.data[:, np.newaxis, i] # select feature from dataset 
    x_train = x[:-20] 
    x_test = x[-20:] 
    regr[i] = linear_model.LinearRegression() 
    regr[i].fit(x_train, y_train) 
    y_predict = regr[i].predict(x_test) 
    MSE[i] = mean_squared_error(y_predict, y_test) 
    COV[i] = np.cov(x_train.T, np.reshape(y_train,[422,1]).T) 
    table[i] = [i, MSE[i], COV[i]] 
    print(table[i]) 

矩阵table包含一切必要的。但是,我应该如何对齐它才能理解?不需要有光泽的乳胶,但可以使用。

+0

您好。我建议你创建一个最小的工作示例,因为你的问题不涉及你发布的数据或算法,并且使其工作起来是浪费时间(目前不)。 –

+0

现在,它的工作。抱歉。 –

回答

1

使用pandas.DataFrame()代替:

import pandas as pd 

df = pd.DataFrame() 
a=range(10) 
b=range(10,20) 
c=range(20,30) 
df['a']=a 
df['b']=b 
df['c']=c 

df 
    a b c 
0 0 10 20 
1 1 11 21 
2 2 12 22 
3 3 13 23 
4 4 14 24 
5 5 15 25 
6 6 16 26 
7 7 17 27 
8 8 18 28 
9 9 19 29 

或一次:

df = pd.DataFrame({'a': a, 'b': b, 'c': c}) 

df 
    a b c 
0 0 10 20 
1 1 11 21 
2 2 12 22 
3 3 13 23 
4 4 14 24 
5 5 15 25 
6 6 16 26 
7 7 17 27 
8 8 18 28 
9 9 19 29 
+0

如果左列的索引是0到9,中间列是否有2x2矩阵作为元素,右列是否再次包含整数,这是如何工作的? –

+0

旁注:请不要在您的代码中使用>>>,这是无用的,并且很难为您自己复制和运行 –