Exemplo n.º 1
0
labels = data[['diagnosis']].copy()

features = data.drop(columns='diagnosis')
feature_list = list(features)

train_features, test_features = np.split(features, [int(.9 * len(features))])
train_labels, test_labels = np.split(labels, [int(.9 * len(labels))])

print('Training Features Shape:', train_features.shape)
print('Training Labels Shape:', train_labels.shape)
print('Testing Features Shape:', test_features.shape)
print('Testing Labels Shape:', test_labels.shape)

# Define pca object
pca = PCA(n_components=2)
train_features = pca.fit_transform(train_features)

test_features = pca.fit_transform(test_features)
rfr = Ridge(type='classifier')
print('Train started............')
rfr.fit(train_features, train_labels)
print('Train completed..........')
# Use the forest's predict method on the test data
print('Prediction started............')
predictions = rfr.predict(test_features)
print('Prediction completed..........')

correct = 0

df = pd.DataFrame(data=predictions.flatten())
Exemplo n.º 2
0
def build_model(model_type,
                prediction_type,
                configs,
                feature_headers,
                label_headers,
                pca=None):
    if model_type == 'Ridge':
        model = Ridge(label_headers=label_headers,
                      alpha=configs['alpha'],
                      type=prediction_type,
                      cfg=args.cfg)
    elif model_type == 'DecisionTree':
        model = DecisionTree(label_headers=label_headers,
                             max_depth=configs['max_depth'],
                             type=prediction_type,
                             cfg=args.cfg)
    elif model_type == 'AdaBoost':
        model = AdaBoost(label_headers=label_headers,
                         n_estimators=configs['n_estimators'],
                         type=prediction_type,
                         cfg=args.cfg)
    elif model_type == 'GradientBoost':
        model = GradientBoost(label_headers=label_headers,
                              n_estimators=configs['n_estimators'],
                              type=prediction_type,
                              cfg=args.cfg)
    elif model_type == 'RandomForest':
        model = RandomForest(label_headers=label_headers,
                             n_estimators=configs['n_estimators'],
                             type=prediction_type,
                             cfg=args.cfg)
    elif model_type == 'KernelSVM':
        model = KernelSVM(kernel=configs['kernel'],
                          degree=configs['degree'],
                          cfg=args.cfg)
    elif model_type == 'NeuralNetwork':
        model = NeuralNetwork(feature_headers,
                              label_headers,
                              epochs=configs['epochs'],
                              batch_size=configs['batch_size'],
                              type=prediction_type,
                              cfg=args.cfg,
                              pca=pca)
    elif model_type == 'ConvolutionalNeuralNetwork':
        model = ConvolutionalNeuralNetwork(height=configs['height'],
                                           width=configs['width'],
                                           dimension=configs['dimension'],
                                           classes=configs['classes'],
                                           epochs=configs['epochs'],
                                           batch_size=configs['batch_size'],
                                           cfg=args.cfg)
    elif model_type == 'LSTM':
        model = LSTMModel(feature_headers,
                          label_headers,
                          epochs=configs['epochs'],
                          batch_size=configs['batch_size'],
                          type=prediction_type,
                          lookback=configs['lookback'],
                          num_of_cells=configs['num_of_cells'],
                          cfg=args.cfg,
                          pca=pca)
    elif model_type == 'PCA':
        model = PCA(n_components=configs['n_components'],
                    type=prediction_type,
                    cfg=args.cfg)
    elif model_type == 'PLS':
        model = PLS(n_components=configs['n_components'],
                    type=prediction_type,
                    cfg=args.cfg)
    elif model_type == 'Lasso':
        model = Lasso(label_headers=label_headers,
                      alpha=configs['alpha'],
                      type=prediction_type,
                      cfg=args.cfg)
    elif model_type == 'AdaptiveLasso':
        model = AdaptiveLasso(label_headers=label_headers,
                              alpha=configs['alpha'],
                              n_itr=configs['n_itr'],
                              type=prediction_type,
                              cfg=args.cfg)
    elif model_type == 'GroupLasso':
        model = GLasso(feature_headers=feature_headers,
                       label_headers=label_headers,
                       groups=configs['groups'],
                       type=prediction_type,
                       cfg=args.cfg)
    elif model_type == 'ElasticNet':
        model = ElasticNet(label_headers=label_headers,
                           l1_ratio=configs['l1_ratio'],
                           type=prediction_type,
                           cfg=args.cfg)
    else:
        print(model_type, ' is not implemented yet')
        model = None

    return model
labels = data[['diagnosis']].copy()

features = data.drop(columns='diagnosis')
feature_list = list(features)

train_features, test_features = np.split(features, [int(.9 * len(features))])
train_labels, test_labels = np.split(labels, [int(.9 * len(labels))])

print('Training Features Shape:', train_features.shape)
print('Training Labels Shape:', train_labels.shape)
print('Testing Features Shape:', test_features.shape)
print('Testing Labels Shape:', test_labels.shape)

# Define pca object
pca = PCA(n_components=2)
train_features = pca.fit_transform(train_features)

test_features = pca.fit_transform(test_features)

rfr = Ridge(type='classifier')
print('Train started............')
rfr.fit(train_features, train_labels)
print('Train completed..........')
# Use the forest's predict method on the test data
print('Prediction started............')
predictions = rfr.predict(test_features)
print('Prediction completed..........')

predict = Evaluation(evaluation=predictions, test_labels=test_labels)
r2s = predict.getAccuracy()
Exemplo n.º 4
0
    labels = data[label_headers].copy()
    features = data[feature_headers].copy()

    model = None
    if '[' in model_type:
        model_type = model_type.replace('[','')
        model_type = model_type.replace(']','')
        sub_models = model_type.split(',')
        sub_models_list = []
        index = 0
        for sub_model_type in sub_models:
            sub_model_type = sub_model_type.lstrip()
            sub_model_type = sub_model_type.rstrip()
            if index == 0:
                tmp_features = PCA.fit(features)
            else:
                model = load_model(modelpath, model_type, type, feature_headers, label_headers)
            index = 1
    else:
        model = load_model(modelpath, model_type, type, feature_headers, label_headers)

    predictions = model.predict(features)
    #print(predictions)
    dict = {}

    index = 0
    for label_header in label_headers:
        dict[label_header] = predictions
        index += 1