2

How to print a table with 3 columns (Index, Covariance Matrix, Mean Square Error)?

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])

The matrix table contains everything that is necessary. But how do I align it so that it is understandable? No shiny LaTeX is needed, but can be used.

2
  • Hi there. I suggest you to create a minimal working example, because your question doesn't concern with the data or algorithm you have posted and it is a waste of time to make it work (it currently doesn't). Commented Mar 12, 2017 at 18:16
  • Now it works. Sorry. Commented Mar 12, 2017 at 18:33

1 Answer 1

2

Use a pandas.DataFrame() instead:

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

Or all at once:

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
Sign up to request clarification or add additional context in comments.

2 Comments

How does that work if the left column is indices from 0 to 9, the middle column has 2x2 matrices as elements, and the right column has integers again?
sidenote: please don't use >>> in your code, it is useless and makes it hard to copy and run for yourself

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.