2017-03-12 35 views
1

Wie drucke ich eine Tabelle mit 3 Spalten (Index, Kovarianzmatrix, mittlerer quadratischer Fehler)?Drucktisch in 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]) 

Die Matrix table enthält alles, was notwendig ist. Aber wie richte ich es so aus, dass es verständlich ist? Kein glänzendes LaTeX wird benötigt, kann aber verwendet werden.

+0

Hallo dort. Ich empfehle Ihnen, ein minimales Arbeitsbeispiel zu erstellen, da Ihre Frage sich nicht auf die von Ihnen geposteten Daten oder Algorithmen bezieht und es Zeitverschwendung ist, sie zum Laufen zu bringen (derzeit nicht). –

+0

Jetzt funktioniert es. Es tut uns leid. –

Antwort

1

ein pandas.DataFrame verwenden() statt:

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 

oder alle auf einmal:

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

Wie funktioniert das, wenn die linke Spalte Indizes von 0 bis 9 hat, die mittlere Spalte 2x2 Matrizen als Elemente hat und die rechte Spalte wieder ganze Zahlen hat? –

+0

sidente: Bitte verwenden Sie nicht >>> in Ihrem Code, es ist nutzlos und macht es schwierig zu kopieren und für sich selbst zu laufen –