Ejemplo n.º 1
0
def run_ELM(
    x,
    y,
    threshold,
    test_num,
    n_hidden,
    random_state=2018,
    kernel_type='MLP',
):
    #  split the data set into train/test
    x_train, x_test, y_train, y_test = cross_validation.train_test_split(
        x, y, test_size=0.3, random_state=random_state)
    # currently only support test_num <=100k
    assert test_num <= 100000

    def powtanh_xfer(activations, power=1.0):
        return pow(np.tanh(activations), power)

    model_count = 0
    result = []
    hidden_options = {
        'MLP': MLPRandomLayer,
        'RBF': RBFRandomLayer,
        'GRBF': GRBFRandomLayer
    }

    for i in range(0, test_num):
        tanh_rhl = hidden_options[kernel_type](n_hidden=n_hidden,
                                               random_state=i,
                                               activation_func=powtanh_xfer,
                                               activation_args={
                                                   'power': 3.0
                                               })
        elmc_tanh = GenELMClassifier(hidden_layer=tanh_rhl)
        # start Training
        elmc_tanh.fit(x_train, y_train)
        # calculate score
        train_acc = elmc_tanh.score(x_train, y_train)
        test_acc = elmc_tanh.score(x_test, y_test)
        if train_acc > threshold and test_acc > threshold:
            logging.info(
                'find model satisfiy threshold, train_acc: {}, test_acc: {}'.
                format(train_acc, test_acc))
            result.append(
                (train_acc, test_acc, tanh_rhl.components_['weights']))

            model_count += 1
    logging.info('fininsh training, get {} valid models'.format(model_count))

    result.sort(key=lambda x: x[1], reverse=True)
    return result
Ejemplo n.º 2
0
    fpr, tpr, thresholds = roc_curve(y_test, y_score)
    tprs.append(tpr)
    fprs.append(fpr)
    roc_auc = auc(fpr, tpr)
    tn, fp, fn, tp = confusion_matrix(y_test, y_pre).ravel()
    test_acc = (tn + tp) / (tn + fp + fn + tp)
    test_Sn = tp / (fn + tp)
    test_Sp = tn / (fp + tn)
    mcc = (tp * tn - fp * fn) / pow(
        ((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)), 0.5)
    final_test_acc.append(test_acc)
    final_test_Sn.append(test_Sn)
    final_test_Sp.append(test_Sp)
    final_mcc.append(mcc)
    final_auc.append(roc_auc)
    print('train_Accuracy: {:.3f}'.format(clf.score(x_train, y_train)))
    #print('test_Accuracy:%0.5f, test_Sn:%0.5f, test_Sp:%0.5f, mcc:%0.5f' % (test_acc, test_Sn, test_Sp, mcc))
    print(
        'test_Accuracy:%0.5f, test_Sn:%0.5f, test_Sp:%0.5f, mcc:%0.5f, roc_auc:%0.5f'
        % (test_acc, test_Sn, test_Sp, mcc, roc_auc))
    print("confusion matrix:\n" + str(confusion_matrix(y_test, y_pre)))
    print('---------------------------------------------------')

Final_test_acc = (sum(final_test_acc) / len(final_test_acc))
Final_test_Sn = (sum(final_test_Sn) / len(final_test_Sn))
Final_test_Sp = (sum(final_test_Sp) / len(final_test_Sp))
Final_mcc = (sum(final_mcc) / len(final_mcc))
Final_auc = (sum(final_auc) / len(final_auc))
print('Final_test_Accuracy:%0.5f' % (Final_test_acc))
print('Final_test_Sn:%0.5f' % (Final_test_Sn))
print('Final_test_Sp:%0.5f' % (Final_test_Sp))
unit_rs = np.ones(nh)

#rhl = RBFRandomLayer(n_hidden=nh, activation_func='inv_multiquadric')
#rhl = RBFRandomLayer(n_hidden=nh, centers=ctrs, radii=unit_rs)
rhl = GRBFRandomLayer(n_hidden=nh, grbf_lambda=.0001, centers=ctrs)
elmr = GenELMRegressor(hidden_layer=rhl)
elmr.fit(xtoy_train, ytoy_train)
print elmr.score(xtoy_train, ytoy_train), elmr.score(xtoy_test, ytoy_test)
plot(xtoy, ytoy, xtoy, elmr.predict(xtoy))

# <codecell>

rbf_rhl = RBFRandomLayer(n_hidden=100, random_state=0, rbf_width=0.01)
elmc_rbf = GenELMClassifier(hidden_layer=rbf_rhl)
elmc_rbf.fit(dgx_train, dgy_train)
print elmc_rbf.score(dgx_train, dgy_train), elmc_rbf.score(dgx_test, dgy_test)


def powtanh_xfer(activations, power=1.0):
    return pow(np.tanh(activations), power)


tanh_rhl = MLPRandomLayer(n_hidden=100,
                          activation_func=powtanh_xfer,
                          activation_args={'power': 3.0})
elmc_tanh = GenELMClassifier(hidden_layer=tanh_rhl)
elmc_tanh.fit(dgx_train, dgy_train)
print elmc_tanh.score(dgx_train,
                      dgy_train), elmc_tanh.score(dgx_test, dgy_test)

# <codecell>
Ejemplo n.º 4
0
metadata = pd.read_table(mapfile, sep='\t', index_col=0)
y = metadata[disease_col]
## Merge adenoma and normal in one-category called no-cancer, so we have binary classification
y = y.replace(to_replace=['normal', 'adenoma'],
              value=['no-cancer', 'no-cancer'])

encoder = LabelEncoder()
y = pd.Series(encoder.fit_transform(y), index=y.index, name=y.name)

A, P, Y, Q = train_test_split(X, y, test_size=0.15,
                              random_state=42)  # Can change to 0.2

srhl_rbf = RBFRandomLayer(n_hidden=50, rbf_width=0.1, random_state=0)
clf6 = GenELMClassifier(hidden_layer=srhl_rbf).fit(A, Y.values.ravel())
print("Accuracy of Extreme learning machine Classifier: " +
      str(clf6.score(P, Q)))

#==============================================
plt.figure()
cls = 0
# Set figure size and plot layout
figsize = (20, 15)
f, ax = plt.subplots(1, 1, figsize=figsize)

x = [clf6, 'purple', 'ELM']

#y_true = Q[Q.argsort().index]
y_score = x[0].decision_function(P)
#y_prob = x[0].predict_proba(P.ix[Q.argsort().index, :])
fpr, tpr, _ = roc_curve(Q, y_score)
roc_auc = auc(fpr, tpr)
Ejemplo n.º 5
0
unit_rs = np.ones(nh)

#rhl = RBFRandomLayer(n_hidden=nh, activation_func='inv_multiquadric')
#rhl = RBFRandomLayer(n_hidden=nh, centers=ctrs, radii=unit_rs)
rhl = GRBFRandomLayer(n_hidden=nh, grbf_lambda=.0001, centers=ctrs)
elmr = GenELMRegressor(hidden_layer=rhl)
elmr.fit(xtoy_train, ytoy_train)
print elmr.score(xtoy_train, ytoy_train), elmr.score(xtoy_test, ytoy_test)
plot(xtoy, ytoy, xtoy, elmr.predict(xtoy))

# <codecell>

rbf_rhl = RBFRandomLayer(n_hidden=100, random_state=0, rbf_width=0.01)
elmc_rbf = GenELMClassifier(hidden_layer=rbf_rhl)
elmc_rbf.fit(dgx_train, dgy_train)
print elmc_rbf.score(dgx_train, dgy_train), elmc_rbf.score(dgx_test, dgy_test)

def powtanh_xfer(activations, power=1.0):
    return pow(np.tanh(activations), power)

tanh_rhl = MLPRandomLayer(n_hidden=100, activation_func=powtanh_xfer, activation_args={'power':3.0})
elmc_tanh = GenELMClassifier(hidden_layer=tanh_rhl)
elmc_tanh.fit(dgx_train, dgy_train)
print elmc_tanh.score(dgx_train, dgy_train), elmc_tanh.score(dgx_test, dgy_test)

# <codecell>

rbf_rhl = RBFRandomLayer(n_hidden=100, rbf_width=0.01)
tr, ts = res_dist(dgx, dgy, GenELMClassifier(hidden_layer=rbf_rhl), n_runs=100, random_state=0)

# <codecell>
Ejemplo n.º 6
0
metadata = pd.read_table(mapfile,sep='\t',index_col=0)
y = metadata[disease_col]
## Merge adenoma and normal in one-category called no-cancer, so we have binary classification
y = y.replace(to_replace=['normal','adenoma'], value=['no-cancer','no-cancer'])

encoder = LabelEncoder()
y = pd.Series(encoder.fit_transform(y),
index=y.index, name=y.name)

A, P, Y, Q = train_test_split(
X, y, test_size=0.1, random_state=42)	# Can change to 0.2


srhl_rbf = RBFRandomLayer(n_hidden=50,rbf_width=0.1,random_state=0)
clf6 = GenELMClassifier(hidden_layer=srhl_rbf).fit(A, Y.values.ravel())
print ("Accuracy of Extreme learning machine Classifier: "+str(clf6.score(P,Q)))


#==============================================
#plt.figure()
cls = 0
# Set figure size and plot layout
figsize=(20,15)
f, ax = plt.subplots(1, 1, figsize=figsize)

x = [clf6,'purple','ELM']

#y_true = Q[Q.argsort().index]
y_score = x[0].decision_function(P)
#y_prob = x[0].predict_proba(P.ix[Q.argsort().index, :])
fpr, tpr, _ = roc_curve(Q, y_score)