示例#1
0
def fit_neural_network(y_train=None,
                       X_train=None,
                       data=None,
                       layers=None,
                       n_epochs=100,
                       n_batch_size=20):
    """Fit a neural network regressor.

    Args:
        layers (str): str specifying the number of hidden layers and hidden nodes in the
            neural network. Example: "100-100-100".
        n_epochs (int): Number of epochs used for model fitting.
        n_batch_size (int): Batch size used for model fitting.

    Returns:
        nnet: The fitted neural network.

    """
    if data is not None:
        y_train = data["y_train"]
        X_train = data["X_train"]

    build_model = _get_build_model_func(input_dim=X_train.shape[1],
                                        layers=layers)

    nnet = KerasClassifier(build_fn=build_model,
                           batch_size=n_batch_size,
                           epochs=n_epochs)
    nnet._estimator_type = "classifier"
    nnet.fit(X_train, y_train, verbose=False)
    return nnet
示例#2
0
def pima_models(x_train_p, y_train_p):
    """
    3 ML models for PIMA (scaled) data
    :param x_train_p:
    :param Y_train:
    :return:
    """
    def create_mlp():
        mlp = Sequential()
        mlp.add(Dense(60, input_dim=len(x_train_p.columns), activation='relu'))
        mlp.add(Dropout(0.2))
        mlp.add(Dense(30, input_dim=60, activation='relu'))
        mlp.add(Dropout(0.2))
        mlp.add(Dense(1, activation='sigmoid'))
        mlp.compile(loss='binary_crossentropy',
                    optimizer='adam',
                    metrics=['accuracy'])
        return mlp

    mlp = KerasClassifier(build_fn=create_mlp,
                          epochs=100,
                          batch_size=64,
                          verbose=0)
    mlp._estimator_type = "classifier"
    mlp.fit(x_train_p, y_train_p)

    rf = RandomForestClassifier(n_estimators=1000)
    rf.fit(x_train_p, y_train_p)
    sigmoidRF = CalibratedClassifierCV(
        RandomForestClassifier(n_estimators=1000), cv=5, method='sigmoid')
    sigmoidRF.fit(x_train_p, y_train_p)

    # lr = LogisticRegression(C=1.)
    # lr.fit(x_train_p, y_train_p)

    svm = SVC(kernel='linear', probability=True)
    svm.fit(x_train_p, y_train_p)

    # create a dictionary of our models
    estimators = [('rf', sigmoidRF), ('mlp', mlp), ('svm', svm)]
    # create our voting classifier, inputting our models
    ensemble = VotingClassifier(estimators, voting='soft')
    ensemble._estimator_type = "classifier"
    ensemble.fit(x_train_p, y_train_p)

    return {'mlp': mlp, 'rf': sigmoidRF, 'svm': svm, 'ensemble': ensemble}
def build_model3():
    model = Sequential()
    model.add(Dense(32, activation='relu', input_shape=(784, )))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    model.compile(optimizer='Adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model


if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = load_data()

    model1 = KerasClassifier(build_fn=build_model1, epochs=20, batch_size=64)
    model1._estimator_type = "classifier"
    model2 = KerasClassifier(build_fn=build_model2, epochs=20, batch_size=64)
    model2._estimator_type = "classifier"
    model3 = KerasClassifier(build_fn=build_model3, epochs=20, batch_size=64)
    model3._estimator_type = "classifier"

    # if ‘hard’, uses predicted class labels for majority rule voting.
    # if ‘soft’, predicts the class label based on the argmax of the
    # sums of the predicted probabilities,
    # which is recommended for an ensemble of well-calibrated classifiers.
    cls = VotingClassifier(estimators=(['model1', model1],
                                       ['model2', model2],
                                       ['model3', model3]),
                           voting='hard')
    cls.fit(x_train, y_train)