Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.0 kB
0
Indexable
Never
def upsample(features, target, repeat):
    features_zeros = features[target == 0]
    features_ones = features[target == 1]
    target_zeros = target[target == 0]
    target_ones = target[target == 1]

    features_upsampled = pd.concat([features_zeros] + [features_ones] * repeat)
    target_upsampled = pd.concat([target_zeros] + [target_ones] * repeat)

    features_upsampled, target_upsampled = shuffle(
        features_upsampled, target_upsampled, random_state=1234
    )

    return features_upsampled, target_upsampled


features_upsampled, target_upsampled = upsample(
    features_train, target_train, 3
)

RFUPmodel = RandomForestClassifier(random_state=1234,n_estimators=100)
RFUPmodel.fit(features_upsampled,target_upsampled)
RFUPpredic_valid = RFmodel.predict(features_valid)
RFUPprob_valid = RFmodel.predict_proba(features_valid)

print('Precision:',precision_score(target_valid,RFpredic_valid))
print('Recall:',recall_score(target_valid,RFpredic_valid))
print('F1 Score:', f1_score(target_valid, RFpredic_valid))
Leave a Comment