Advertisement
korenizla

downsampling

Nov 1st, 2022
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. def downsample(features, target, fraction):
  2.     features_zeros = features[target == 0]
  3.     features_ones = features[target == 1]
  4.     target_zeros = target[target == 0]
  5.     target_ones = target[target == 1]
  6.  
  7.     features_downsampled = pd.concat(
  8.         [features_zeros.sample(frac=fraction, random_state=12345)] + [features_ones])
  9.     target_downsampled = pd.concat(
  10.         [target_zeros.sample(frac=fraction, random_state=12345)] + [target_ones])
  11.    
  12.     features_downsampled, target_downsampled = shuffle(
  13.         features_downsampled, target_downsampled, random_state=12345)
  14.    
  15.     return features_downsampled, target_downsampled
  16.  
  17. features_downsampled, target_downsampled = downsample(features_train, target_train, 0.1)
  18.  
  19. model = RandomForestClassifier(random_state=12345, criterion='gini', max_depth=11,
  20.                                min_samples_leaf=2, n_estimators=76, class_weight='balanced')
  21.  
  22. model.fit(features_downsampled, target_downsampled)
  23. predicted_valid = model.predict(features_valid)
  24.  
  25. print("F1:", f1_score(target_valid, predicted_valid))
  26.  
  27. #OUTPUT F1: 0.5099236641221373
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement