Added explained variance

This commit is contained in:
Arnar Flatberg 2007-08-14 16:12:28 +00:00
parent 004cfe0a9f
commit 8d4848d5fa
2 changed files with 65 additions and 16 deletions

View File

@ -328,11 +328,11 @@ class PLS(Model):
**options.make_model_options())
# variance captured
var_x, exp_var_x = variances(self.model['E0'], self.model['T'], self.model['P'])
self.model['var_x'] = var_x
self.model['evx'] = var_x
self.model['exp_var_x'] = exp_var_x
var_y, exp_var_y = variances(self.model['F0'], self.model['T'], self.model['Q'])
self.model['var_y'] = var_y
self.model['evy'] = var_y
self.model['exp_var_y'] = exp_var_y
if options['calc_conf']:
@ -478,6 +478,14 @@ class LPLS(Model):
self._data['Z'] = c.asarray()
self.validation(options)
self.make_model(options)
print self.model['evx']
evx_str = [str(i)[:3] for i in self.model['evx']]
logger.log('notice', 'Explained variance:X\n\t: ' + str(evx_str))
evy_str = [str(i)[:3] for i in self.model['evy']]
logger.log('notice', 'Explained variance:Y\n\t: ' + str(evy_str))
evz_str = [str(i)[:3] for i in self.model['evz']]
logger.log('notice', 'Explained variance:Z\n\t: ' + str(evz_str))
if options['calc_conf']:
self.confidence(options)
@ -592,7 +600,7 @@ class PlsOptions(Options):
opt['center_mth'] = mat_center
opt['scale'] = 'scores'
opt['calc_conf'] = False
opt['calc_conf'] = True
opt['n_sets'] = 7
opt['strict'] = True
opt['p_center'] = 'med'
@ -607,7 +615,7 @@ class PlsOptions(Options):
opt['all_data'] = [('T', 'scores', True),
('P', 'loadings', True),
('E','residuals', False),
('p_tsq', 't2', False),
('w_tsq', 't2', True),
('rmsep', 'RMSEP', False)
]
@ -620,10 +628,10 @@ class PlsOptions(Options):
(blmplots.PlsCorrelationLoadingPlot, 'Corr. loadings', False)
]
opt['out_data'] = ['T','P', 'p_tsq']
opt['out_data'] = ['T','P', 'w_tsq']
opt['out_plots'] = [blmplots.PlsScorePlot,blmplots.PlsLoadingPlot,blmplots.LineViewXc]
opt['out_data'] = None
#opt['out_data'] = None
opt['pack'] = True
opt['calc_qvals'] = False
@ -664,17 +672,17 @@ class LplsOptions(Options):
opt['engine'] = nipals_lpls
opt['mode'] = 'normal' # how much info to calculate
opt['amax'] = 10
opt['aopt'] = 4
opt['xz_alpha'] = 0.5
opt['aopt'] = 3
opt['xz_alpha'] = 0.4
opt['auto_aopt'] = False
opt['center'] = True
opt['center_mth'] = [2, 2, 1]
opt['scale'] = 'scores'
opt['calc_conf'] = False
opt['calc_conf'] = True
opt['n_sets'] = 75
opt['strict'] = False
opt['p_center'] = 'med'
opt['alpha'] = .2
opt['alpha'] = .3
opt['cov_center'] = 'med'
opt['crot'] = True
@ -701,13 +709,14 @@ class LplsOptions(Options):
(blmplots.LplsZCorrelationPlot, 'Z corr.', True)
]
opt['out_data'] = ['T','P','L','K']
opt['out_plots'] = [blmplots.PlsScorePlot,
opt['out_data'] = ['T','P','L','K', 'tsqx', 'tsqz']
opt['out_plots'] = [blmplots.LplsScorePlot,
blmplots.LplsXLoadingPlot,
blmplots.LplsZLoadingPlot,
blmplots.LplsXCorrelationPlot,
blmplots.LplsZCorrelationPlot,
blmplots.LineViewXc]
blmplots.LineViewXc,
blmplots.LplsExplainedVariancePlot]
#opt['out_data'] = None

View File

@ -43,6 +43,7 @@ class BlmScatterPlot(plots.ScatterPlot):
"""Scatter plot used for scores and loadings in bilinear models."""
def __init__(self, title, model, absi=0, ordi=1, part_name='T', color_by=None):
self.model = model
if model.model.has_key(part_name)!=True:
raise ValueError("Model part: %s not found in model" %mod_param)
self._T = model.model[part_name]
@ -52,6 +53,7 @@ class BlmScatterPlot(plots.ScatterPlot):
self._absi = absi
self._ordi = ordi
self._cmap = cm.summer
dataset_1 = model.as_dataset(part_name)
id_dim = dataset_1.get_dim_name(0)
sel_dim = dataset_1.get_dim_name(1)
@ -64,7 +66,7 @@ class BlmScatterPlot(plots.ScatterPlot):
self._mappable.set_cmap(self._cmap)
self.sc = self._mappable
self.add_pc_spin_buttons(self._T.shape[1], absi, ordi)
def set_facecolor(self, colors):
"""Set patch facecolors.
"""
@ -77,6 +79,18 @@ class BlmScatterPlot(plots.ScatterPlot):
def set_sizes(self, sizes):
"""Set patch sizes."""
pass
def set_expvar_axlabels(self, param="evx"):
if not self.model.model.has_key(param):
self.model.model[param] = None
if self.model.model[param]==None:
pass #fixme: do expvar calc here if not present
else:
expvar = self.model.model[param]
xstr = "Comp: %s , %.1f " %(self._absi, expvar[self._absi+1])
ystr = "Comp: %s , %.1f " %(self._ordi, expvar[self._ordi+1])
self.axes.set_xlabel(xstr)
self.axes.set_ylabel(ystr)
def add_pc_spin_buttons(self, amax, absi, ordi):
sb_a = gtk.SpinButton(climb_rate=1)
@ -115,6 +129,7 @@ class BlmScatterPlot(plots.ScatterPlot):
pad = abs(self.xaxis_data.min()-self.xaxis_data.max())*0.05
new_lims = (self.xaxis_data.min()+pad, self.xaxis_data.max()+pad)
self.axes.set_xlim(new_lims, emit=True)
self.set_expvar_axlabels()
self.canvas.draw_idle()
def set_ordinate(self, sb):
@ -127,6 +142,7 @@ class BlmScatterPlot(plots.ScatterPlot):
pad = abs(self.yaxis_data.min()-self.yaxis_data.max())*0.05
new_lims = (self.yaxis_data.min()+pad, self.yaxis_data.max()+pad)
self.axes.set_ylim(new_lims, emit=True)
self.set_expvar_axlabels()
self.canvas.draw_idle()
def show_labels(self, index=None):
@ -173,22 +189,34 @@ class PlsLoadingPlot(BlmScatterPlot):
def __init__(self, model, absi=0, ordi=1):
title = "Pls loadings (%s)" %model._dataset['X'].get_name()
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='P', color_by='w_tsq')
#self.set_expvar_axlabels(self, param="expvarx")
class PlsCorrelationLoadingPlot(BlmScatterPlot):
def __init__(self, model, absi=0, ordi=1):
title = "Pls correlation loadings (%s)" %model._dataset['X'].get_name()
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='CP')
class LplsScorePlot(BlmScatterPlot):
def __init__(self, model, absi=0, ordi=1):
title = "L-pls scores (%s)" %model._dataset['X'].get_name()
BlmScatterPlot.__init__(self, title, model, absi, ordi, 'T')
self.set_expvar_axlabels("evx")
class LplsXLoadingPlot(BlmScatterPlot):
def __init__(self, model, absi=0, ordi=1):
title = "Lpls x-loadings (%s)" %model._dataset['X'].get_name()
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='P', color_by='tsqx')
self.set_expvar_axlabels("evx")
class LplsZLoadingPlot(BlmScatterPlot, plots.PlotThresholder):
def __init__(self, model, absi=0, ordi=1):
title = "Lpls z-loadings (%s)" %model._dataset['Z'].get_name()
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='L', color_by='tsqz')
self.set_expvar_axlabels(param="evz")
plots.PlotThresholder.__init__(self, "IC")
@ -204,6 +232,7 @@ class LplsXCorrelationPlot(BlmScatterPlot):
R = correlation_loadings(model._data['X'], model.model['T'])
model.model['Rx'] = R
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='Rx')
self.set_expvar_axlabels("evx")
radius = 1
center = (0,0)
c100 = patches.Circle(center,radius=radius,
@ -229,6 +258,7 @@ class LplsZCorrelationPlot(BlmScatterPlot):
R = correlation_loadings(model._data['Z'].T, model.model['W'])
model.model['Rz'] = R
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='Rz')
self.set_expvar_axlabels("evz")
radius = 1
center = (0,0)
c100 = patches.Circle(center,radius=radius,
@ -254,6 +284,16 @@ class LplsHypoidCorrelationPlot(BlmScatterPlot):
BlmScatterPlot.__init__(self, title, model, absi, ordi, part_name='W')
class LplsExplainedVariancePlot(plots.Plot):
def __init__(self, model):
self.model = model
plots.Plot.__init__(self, "Explained variance")
xax = scipy.arange(model.model['evx'].shape[0])
self.axes.plot(xax, model.model['evx'], 'b-', label='X', linewidth=1.5)
self.axes.plot(xax, model.model['evy'], 'k-', label='Y', linewidth=1.5)
self.axes.plot(xax, model.model['evz'], 'g-', label='Z', linewidth=1.5)
self.canvas.draw()
class LineViewXc(plots.LineViewPlot):
"""A line view of centered raw data
"""