Exemplo n.º 1
0
    # coxnet caller
    x = np.loadtxt(baseDataDir + 'CoxExampleX.dat', dtype = np.float64, delimiter = ',')
    y = np.loadtxt(baseDataDir + 'CoxExampleY.dat', dtype = np.float64, delimiter = ',')
    fit = glmnet.glmnet(x = x, y = y, family = 'cox')
    print('fit:')
    pprint.pprint(fit)

if testType == 'mgaussian':
    # mgaussian caller
    x = np.loadtxt(baseDataDir + 'MultiGaussianExampleX.dat', dtype = np.float64, delimiter = ',')
    y = np.loadtxt(baseDataDir + 'MultiGaussianExampleY.dat', dtype = np.float64, delimiter = ',')
    fit = glmnet.glmnet(x = x, y = y, family = 'mgaussian')
    print('fit:')
    pprint.pprint(fit)    
    
if testType == 'poisson':
    # poisson caller
    x = np.loadtxt(baseDataDir + 'PoissonExampleX.dat', dtype = np.float64, delimiter = ',')
    y = np.loadtxt(baseDataDir + 'PoissonExampleY.dat', dtype = np.float64, delimiter = ',')
    fit = glmnet.glmnet(x = x, y = y, family = 'poisson')
    print('fit:')
    pprint.pprint(fit)


glmnetPlot.glmnetPlot(fit, label = True)
# glmnetPlot.glmnetPlot(fit, xvar = 'lambda', label = True)

f = glmnetPredict.glmnetPredict(fit, x[0:1,:])


Exemplo n.º 2
0
importlib.reload(cvglmnetPlot)
importlib.reload(cvglmnetPredict)

# parameters
baseDataDir = '../data/'

# load data
x = np.loadtxt(baseDataDir + 'BinomialExampleX.dat',
               dtype=np.float64,
               delimiter=',')
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']
Exemplo n.º 3
0
importlib.reload(cvglmnetPlot)
importlib.reload(cvglmnetPredict)

# parameters
baseDataDir = '../data/'

# load data
x = np.loadtxt(baseDataDir + 'PoissonExampleX.dat',
               dtype=np.float64,
               delimiter=',')
y = np.loadtxt(baseDataDir + 'PoissonExampleY.dat',
               dtype=np.float64,
               delimiter=',')

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

glmnetPlot.glmnetPlot(fit)

glmnetCoef.glmnetCoef(fit, s=np.float64([1.0]))

f = glmnetPredict.glmnetPredict(fit,
                                x[0:5, :],
                                ptype='response',
                                s=np.float64([0.1, 0.01]))
print(f)

cvfit = cvglmnet.cvglmnet(x.copy(), y.copy(), family='poisson')
optlam = np.array([cvfit['lambda_min'], cvfit['lambda_1se']]).reshape(2, )
cvglmnetCoef.cvglmnetCoef(cvfit, s=optlam)
Exemplo n.º 4
0
# load data
x = scipy.loadtxt(baseDataDir + 'QuickStartExampleX.dat', dtype=scipy.float64)
y = scipy.loadtxt(baseDataDir + 'QuickStartExampleY.dat', dtype=scipy.float64)

# create weights
t = scipy.ones((50, 1), dtype=scipy.float64)
wts = scipy.row_stack((t, 2 * t))

# call glmnet
fit = glmnet.glmnet(x = x.copy(), y = y.copy(), family = 'gaussian', \
                    weights = wts, \
                    alpha = 0.2, nlambda = 20
                    )

glmnetPrint.glmnetPrint(fit)
glmnetPlot.glmnetPlot(fit, xvar='lambda', label=True)
glmnetPlot.glmnetPlot(fit, xvar='dev', label=True)
#
any(fit['lambdau'] == 0.5)
#
coefApprx = glmnetCoef.glmnetCoef(fit, s=scipy.float64([0.5]), exact=False)
print(coefApprx)
#
fc = glmnetPredict.glmnetPredict(fit, x[0:5,:], ptype = 'response', \
                                s = scipy.float64([0.05]))
print(fc)
#
cvfit = cvglmnet.cvglmnet(x=x.copy(), y=y.copy(), ptype='mse', nfolds=20)
cvfit['lambda_min']
cvglmnetCoef.cvglmnetCoef(cvfit, s='lambda_min')
#%%
Exemplo n.º 5
0
# parameters
baseDataDir = '../data/'

# load data
x = scipy.loadtxt(baseDataDir + 'MultiGaussianExampleX.dat',
                  dtype=scipy.float64,
                  delimiter=',')
y = scipy.loadtxt(baseDataDir + 'MultiGaussianExampleY.dat',
                  dtype=scipy.float64,
                  delimiter=',')

# call glmnet
mfit = glmnet.glmnet(x=x.copy(), y=y.copy(), family='mgaussian')

plt.figure()
glmnetPlot(mfit, xvar='lambda', label=True, ptype='2norm')

f = glmnetPredict.glmnetPredict(mfit, x[0:5, :], s=scipy.float64([0.1, 0.01]))
print(f[:, :, 0])
print(f[:, :, 1])

plt.figure()
t = time.time()
cvmfit = cvglmnet.cvglmnet(x=x.copy(),
                           y=y.copy(),
                           family="mgaussian",
                           parallel=True)
e = time.time() - t
print('time elapsed = ', e)

cvglmnetPlot.cvglmnetPlot(cvmfit)
# ln -s /r/a/p/usr/lib64/python3.5/site-packages/glmnet_python /r/a/p/usr/lib64/python3.5/site-packages/glmnet
# import os
# import sys
# sys.path.append(os.path.join(os.path.dirname(
#     '/r/a/p/usr/lib64/python3.5/site-packages/glmnet_python/__init__.py')))
import glmnet_python
from glmnet import glmnet
from glmnetPlot import glmnetPlot

import scipy
wts = scipy.ones((df_numeric_data.shape[0], 1), dtype=scipy.float64)
fit = glmnet(x=x.copy(), y=y.copy(), family='gaussian',
             weights=wts,
             alpha=0.2, nlambda=20
             )
glmnetPlot(fit, xvar='lambda', label=True)

plt.savefig('glmnet_price1.png', dpi=200)
plt.cla()
plt.clf()
plt.close()


from patsy import dmatrices
df_categorical = superclean_data[['price', 'yearOfRegistration',
                                  'powerPS', 'kilometer', 'vehicleType', 'gearbox', 'fuelType', 'notRepairedDamage', 'brand', 'model']].dropna()

df_categorical['yearOfRegistration'] = 2017 - \
    df_categorical['yearOfRegistration']

y, X = dmatrices('price ~' + 'yearOfRegistration+powerPS+kilometer+C(notRepairedDamage)+C(fuelType)+C(gearbox)+C(vehicleType)+C(brand)+C(model)', df_categorical,
Exemplo n.º 7
0
    y = scipy.random.normal(size=[N, 1])
    y[y > 0] = 1.0
    y[y < 0] = 0.0
    st = time.time()
    fit = cvglmnet.cvglmnet(x=xs, y=y, family=family)
    en = time.time()
    print("time elapsed (sparse) = ", en - st)

elif section == 5:
    import matplotlib.pyplot as plt
    scipy.random.seed(1)
    x = scipy.random.normal(size=(100, 20))
    y = scipy.random.normal(size=(100, 1))
    g4 = scipy.random.choice(4, size=(100, 1)) * 1.0
    fit1 = glmnet.glmnet(x=x.copy(), y=y.copy())
    glmnetPlot(fit1)
    plt.figure()
    glmnetPlot(fit1, 'lambda', True)
    fit3 = glmnet.glmnet(x=x.copy(), y=g4.copy(), family='multinomial')
    plt.figure()
    glmnetPlot(fit3)

elif section == 6:
    x = scipy.random.rand(100, 10)
    y = scipy.random.rand(100, 1)
    fit = glmnet.glmnet(x=x, y=y)
    f = glmnetPredict(fit, x[0:5, :], scipy.array([0.0866, 0.2323]))
    print(f)

elif section == 7:
    x = scipy.random.normal(size=[100, 20])
Exemplo n.º 8
0
from cvglmnetPlot import cvglmnetPlot
from cvglmnetPredict import cvglmnetPredict

# データロード
df = pd.read_csv('QuickStartExample.csv', index_col=0)
x = df.iloc[:, 0:20].values
y = df.iloc[:, 20].values

# Correlation
pd.set_option('display.max_columns', 21)
pd.set_option('display.max_rows', 21)
df.corr().style.background_gradient(cmap='cool')

# 解パス図
fit = glmnet(x=x, y=y)
glmnetPlot(fit, xvar='lambda', label=True)

# RMSE
np.random.seed(0)

k = 10  # K-fold cross-validation
foldid = np.random.choice(k, size=y.shape[0], replace=True)
cvfit = cvglmnet(x=x, y=y, foldid=foldid)

fig = plt.figure()
ax = fig.add_subplot(111)
cvglmnetPlot(cvfit)
plt.show()

# predict
y_hat = cvglmnetPredict(cvfit, newx=x, s='lambda_min')