Exemplo n.º 1
0
 def diagnostic_plot1(self, saveto=None):
     fig, axn = plt.subplots(len(self.alphas),
                             1,
                             figsize=(8, 60),
                             sharey=True)
     for i, ax in enumerate(axn.flat):
         plt.axes(ax)
         cvglmnetPlot(self.estimators[i])
         textstr = 'alpha=' + str(np.round_(self.alphas[i], 3))
         props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
         ax.text(0.05,
                 0.95,
                 textstr,
                 transform=ax.transAxes,
                 fontsize=14,
                 verticalalignment='top',
                 bbox=props)
         plt.tight_layout()
     if saveto != None:
         plt.savefig(saveto, dpi=300)
Exemplo n.º 2
0
# Part 3a

################## Do for latitude

print("-----------Latitude-------------------")
plt.figure(figsize=(1200 / 96, 1200 / 96), dpi=96)  # initialize a canvas
plt.subplot(2, 2, 1)
cvfit_ridge = cvglmnet(x=features.copy(),
                       y=latitude.copy(),
                       ptype='mse',
                       nfolds=20,
                       alpha=0)  # create a ridge regression

coff_count = sum(1 for x in cvglmnetCoef(cvfit_ridge)
                 if x != 0)  # get the no of non zero weights
cvglmnetPlot(cvfit_ridge)  # plot the ridge regression
plt.title("Ridge on latitude", y=1.10)
print("The value of lambda giving minimum mse for ridge for latitude is " +
      str((cvfit_ridge['lambda_min'])))  # min lambda
print("The value of lambda giving 1se mse for ridge for latitude is " + str(
    (cvfit_ridge['lambda_1se'])))  # 1se lambda
print("The cross validated error for ridge for latitude is " +
      str(np.mean(cvfit_ridge['cvm'])))  # cv error

pred_ridge = cvglmnetPredict(
    cvfit_ridge, newx=features.copy(),
    s='lambda_min').flatten()  # get the predicted values
r2Score = r2_score(latitude.copy(), pred_ridge)  # get r2
print("r2 score for ridge regularisation for latitude is " + str(r2Score))
print(
    "Mean square error for regression model to predict latitude with ridge regularisation is "
Exemplo n.º 3
0
y = np.loadtxt(baseDataDir + 'BinomialExampleY.dat', dtype=np.float64)

# call glmnet
fit = glmnet.glmnet(x=x.copy(), y=y.copy(), family='binomial')

glmnetPlot.glmnetPlot(fit, xvar='dev', label=True)

glmnetPredict.glmnetPredict(fit,
                            newx=x[0:5, ],
                            ptype='class',
                            s=np.array([0.05, 0.01]))

cvfit = cvglmnet.cvglmnet(x=x.copy(),
                          y=y.copy(),
                          family='binomial',
                          ptype='class')

plt.figure()
cvglmnetPlot.cvglmnetPlot(cvfit)

cvfit['lambda_min']

cvfit['lambda_1se']

cvglmnetCoef.cvglmnetCoef(cvfit, s='lambda_min')

cvglmnetPredict.cvglmnetPredict(cvfit,
                                newx=x[0:10, ],
                                s='lambda_min',
                                ptype='class')
Exemplo n.º 4
0
cvfit['lambda_min']
cvglmnetCoef.cvglmnetCoef(cvfit, s='lambda_min')
#%%
cvglmnetPredict.cvglmnetPredict(cvfit, newx=x[0:5, ], s='lambda_min')

#%%
foldid = scipy.random.choice(10, size=y.shape[0], replace=True)

cv1 = cvglmnet.cvglmnet(x=x.copy(), y=y.copy(), foldid=foldid, alpha=1)
cv0p5 = cvglmnet.cvglmnet(x=x.copy(), y=y.copy(), foldid=foldid, alpha=0.5)
cv0 = cvglmnet.cvglmnet(x=x.copy(), y=y.copy(), foldid=foldid, alpha=0)

#%%
f = plt.figure()
f.add_subplot(2, 2, 1)
cvglmnetPlot.cvglmnetPlot(cv1)
f.add_subplot(2, 2, 2)
cvglmnetPlot.cvglmnetPlot(cv0p5)
f.add_subplot(2, 2, 3)
cvglmnetPlot.cvglmnetPlot(cv0)
f.add_subplot(2, 2, 4)
plt.plot(scipy.log(cv1['lambdau']), cv1['cvm'], 'r.')
#plt.hold(True)
plt.plot(scipy.log(cv0p5['lambdau']), cv0p5['cvm'], 'g.')
plt.plot(scipy.log(cv0['lambdau']), cv0['cvm'], 'b.')
plt.xlabel('log(Lambda)')
plt.ylabel(cv1['name'])
plt.xlim(-6, 4)
plt.ylim(0, 9)
plt.legend(('alpha = 1', 'alpha = 0.5', 'alpha = 0'),
           loc='upper left',
Exemplo n.º 5
0
##############################################################################
##############################################################################

# precompute with alpha=1.0 (default, lasso) and keep foldids in order to
# find optimal value of alpha.

warnings.filterwarnings('ignore')
elasticCV = cvglmnet(x=z_train_param.copy(),
                     y=train_group.copy(),
                     family="multinomial",
                     mtype="grouped",
                     parallel=-1,
                     keep=True)

warnings.filterwarnings('default')
cvglmnetPlot(elasticCV)
pred_elastic = cvglmnetPredict(elasticCV,
                               newx=z_test_param,
                               s='lambda_min',
                               ptype="class")
acc_elastic = accuracy_score(test_group, pred_elastic, normalize=True)
prec_elastic = precision_score(test_group, pred_elastic, average=None)

print("The accuracy of CV Elastic Net Regression (a=1.0): " + str(acc_elastic))
for i, j in zip(np.nditer(prec_LogL1), groups):
    print(
        "The precision of CV Elastic Net Regression (a=1.0) for ALL subtype " +
        j + ": " + str(round(float(i), 3)))

print("\n\n\n")
print(
Exemplo n.º 6
0
r2Score = r2_score(measurement_data_with_gender_encoded.ix[:, -1],
                   lm.predict(measurement_data_with_gender_encoded.ix[:, 0:8]))
print(
    "For problem 7.11d with gender and log of dependent variable the r2 score is "
    + str(r2Score))

# Part 6

x = np.array(measurement_data_without_gender).astype(
    scipy.float64)  # change the data type to float64
cvfit_lasso = cvglmnet(x=x[:, :7].copy(),
                       y=x[:, -1].copy(),
                       ptype='mse',
                       nfolds=10,
                       alpha=1)  # create a lasso regression
cvglmnetPlot(cvfit_lasso)  # plot the lasso regression
plt.title("Lasso regularisation", y=1.14)
plt.tight_layout()
plt.savefig('problem7.11f_lasso.png')  # save the plot
plt.close()  # close the plot
print("The value of lambda giving minimum mse for lasso is " +
      str(cvfit_lasso['lambda_min']))

pred_lasso = cvglmnetPredict(
    cvfit_lasso,
    newx=measurement_data_without_gender.ix[:, 0:7].copy(),
    s='lambda_min').flatten()  # get the predicted values
r2Score = r2_score(measurement_data_without_gender.ix[:, -1], pred_lasso)
print("For problem 7.11f with gender and lasso regression the r2 score is " +
      str(r2Score))
Exemplo n.º 7
0
    print(glmnetPredict(fit2, scipy.empty([0]), scipy.empty([0]), 'nonzero'))

    fit3 = glmnet.glmnet(x=x.copy(), y=g4.copy(), family='multinomial')
    print(glmnetPredict(fit3, x[0:3, :], scipy.array([0.01]), 'response'))
    print(glmnetPredict(fit3, x[0:3, :], scipy.array([0.01, 0.5]), 'response'))

elif section == 8:
    x = scipy.random.rand(100, 20)
    y = scipy.random.rand(100, 1)
    fit = glmnet.glmnet(x=x.copy(), y=y.copy())
    ncoef = glmnetCoef(fit, scipy.array([0.01, 0.001]))

elif section == 9:
    scipy.random.seed(1)
    x = scipy.random.normal(size=(100, 20))
    y = scipy.random.normal(size=(100, 1))
    g2 = scipy.random.choice(2, size=(100, 1)) * 1.0
    g4 = scipy.random.choice(4, size=(100, 1)) * 1.0

    plt.figure()
    fit1 = cvglmnet(x=x.copy(), y=y.copy())
    cvglmnetPlot(fit1)

    plt.figure()
    fit2 = cvglmnet(x=x.copy(), y=g2.copy(), family='binomial')
    cvglmnetPlot(fit2)

    plt.figure()
    fit3 = cvglmnet(x=x.copy(), y=g2.copy(), family='binomial', ptype='class')
    cvglmnetPlot(fit3)