Advertisement
korenizla

upsampling

Nov 1st, 2022
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. from sklearn.utils import shuffle
  2.  
  3. def upsample(features, target, repeat):
  4.     '''Данная функция создана для того, чтобы
  5.    увеличить выборку'''
  6.     features_zeros = features[target == 0]
  7.     features_ones = features[target == 1]
  8.     target_zeros = target[target == 0]
  9.     target_ones = target[target == 1]
  10.  
  11.     features_upsampled = pd.concat([features_zeros] + [features_ones] * repeat)
  12.     target_upsampled = pd.concat([target_zeros] + [target_ones] * repeat)
  13.    
  14.     features_upsampled, target_upsampled = shuffle(
  15.         features_upsampled, target_upsampled, random_state=12345)
  16.    
  17.     return features_upsampled, target_upsampled
  18.  
  19. features_upsampled, target_upsampled = upsample(features_train, target_train, 10)
  20.  
  21. model = RandomForestClassifier(random_state=12345, criterion='gini', max_depth=11,
  22.                                min_samples_leaf=2, n_estimators=76, class_weight='balanced')
  23. model.fit(features_upsampled, target_upsampled)
  24. predicted_valid = model.predict(features_valid)
  25.  
  26. print("F1:", f1_score(target_valid, predicted_valid))
  27.  
  28. #OUTPUT F1: 0.5956471935853379
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement