示例#1
0
def predict_LME(model_path, outdir, X, y, dv, partition_name, model_name=''):
    stderr('Retrieving saved model %s...\n' % m)
    with open(model_path, 'rb') as m_file:
        lme = pickle.load(m_file)

    summary = '=' * 50 + '\n'
    summary += 'LME regression\n\n'
    summary += 'Model name: %s\n\n' % m
    summary += 'Formula:\n'
    summary += '  ' + formula + '\n'
    summary += str(lme.summary()) + '\n'

    if args.mode in [None, 'response']:
        lme_preds = lme.predict(X)

        with open(outdir + '/%spreds_%s.txt' % ('' if model_name=='' else model_name + '_', partition_name), 'w') as p_file:
            for i in range(len(lme_preds)):
                p_file.write(str(lme_preds[i]) + '\n')
        losses = np.array(y[dv] - lme_preds) ** 2
        with open(outdir + '/%slosses_mse_%s.txt' % ('' if model_name=='' else model_name + '_', partition_name), 'w') as p_file:
            for i in range(len(losses)):
                p_file.write(str(losses[i]) + '\n')
        lme_mse = mse(y[dv], lme_preds)
        lme_mae = mae(y[dv], lme_preds)

        summary += 'Loss (%s set):\n' % partition_name
        summary += '  MSE: %.4f\n' % lme_mse
        summary += '  MAE: %.4f\n' % lme_mae

    summary += '=' * 50 + '\n'
    with open(outdir + '/%seval_%s.txt' % ('' if model_name=='' else model_name + '_', partition_name), 'w') as f_out:
        f_out.write(summary)
    stderr(summary)
示例#2
0
文件: lmer.py 项目: mindis/cdr
                        'Retrieving saved L(ME) regression of CDR model %s...\n'
                        % m)
                    with open(model_path, 'rb') as m_file:
                        lm = pickle.load(m_file)
                else:
                    stderr('Fitting L(ME) regression of CDR model %s...\n' % m)
                    if is_lme:
                        lm = LME(model_form, df_r)
                    else:
                        lm = LM(model_form, df_r)
                    with open(model_path, 'wb') as m_file:
                        pickle.dump(lm, m_file)

                lm_preds = lm.predict(df_r)
                lm_mse = mse(df[dv], lm_preds)
                lm_mae = mae(df[dv], lm_preds)
                summary = '=' * 50 + '\n'
                summary += '%s regression\n\n' % ('LME'
                                                  if is_lme else 'Linear')
                summary += 'Model name: %s\n\n' % m
                summary += 'Formula:\n'
                summary += '  ' + model_form + '\n'
                summary += str(lm.summary()) + '\n'
                summary += 'Training set loss:\n'
                summary += '  MSE: %.4f\n' % lm_mse
                summary += '  MAE: %.4f\n' % lm_mae
                summary += '=' * 50 + '\n'
                with open(model_summary_path, 'w') as f_out:
                    f_out.write(summary)
                stderr(summary)
                stderr('\n\n')
示例#3
0
文件: train.py 项目: mindis/cdr
            dv = formula.strip().split('~')[0].strip().replace('.', '')

            if os.path.exists(p.outdir + '/' + m + '/m.obj'):
                stderr('Retrieving saved model %s...\n' % m)
                with open(p.outdir + '/' + m + '/m.obj', 'rb') as m_file:
                    lme = pickle.load(m_file)
            else:
                stderr('Fitting model %s...\n' % m)
                lme = LME(formula, X_baseline)
                with open(p.outdir + '/' + m + '/m.obj', 'wb') as m_file:
                    pickle.dump(lme, m_file)

            lme_preds = lme.predict(X_baseline)
            lme_mse = mse(y[dv], lme_preds)
            lme_mae = mae(y[dv], lme_preds)
            summary = '=' * 50 + '\n'
            summary += 'LME regression\n\n'
            summary += 'Model name: %s\n\n' % m
            summary += 'Formula:\n'
            summary += '  ' + formula + '\n'
            summary += str(lme.summary()) + '\n'
            summary += 'Training set loss:\n'
            summary += '  MSE: %.4f\n' % lme_mse
            summary += '  MAE: %.4f\n' % lme_mae
            summary += '=' * 50 + '\n'
            with open(p.outdir + '/' + m + '/summary.txt', 'w') as f_out:
                f_out.write(summary)
            stderr(summary)
            stderr('\n\n')
示例#4
0
                stderr('Retrieving saved model %s...\n' % m)
                with open(p.outdir + '/' + m + '/m.obj', 'rb') as m_file:
                    gam = pickle.load(m_file)
                gam_preds = gam.predict(X_baseline)
                with open(p.outdir + '/' + m + '/preds_%s.txt' % partition_str,
                          'w') as p_file:
                    for i in range(len(gam_preds)):
                        p_file.write(str(gam_preds[i]) + '\n')
                losses = np.array(y[dv] - gam_preds)**2
                with open(
                        p.outdir + '/' + m +
                        '/losses_mse_%s.txt' % partition_str, 'w') as p_file:
                    for i in range(len(losses)):
                        p_file.write(str(losses[i]) + '\n')
                gam_mse = mse(y[dv], gam_preds)
                gam_mae = mae(y[dv], gam_preds)
                summary = '=' * 50 + '\n'
                summary += 'GAM regression\n\n'
                summary += 'Model name: %s\n\n' % m
                summary += 'Formula:\n'
                summary += '  ' + formula + '\n'
                summary += str(gam.summary()) + '\n'
                summary += 'Loss (%s set):\n' % partition_str
                summary += '  MSE: %.4f\n' % gam_mse
                summary += '  MAE: %.4f\n' % gam_mae
                summary += '=' * 50 + '\n'
                with open(p.outdir + '/' + m + '/eval_%s.txt' % partition_str,
                          'w') as f_out:
                    f_out.write(summary)
                stderr(summary)