Skip to content

Instantly share code, notes, and snippets.

@VictorSaenger
Last active September 7, 2019 11:10
Show Gist options
  • Select an option

  • Save VictorSaenger/9b5712b14c2674fd645845bd5237e523 to your computer and use it in GitHub Desktop.

Select an option

Save VictorSaenger/9b5712b14c2674fd645845bd5237e523 to your computer and use it in GitHub Desktop.
#This pipeline allow us to change the type of classifier:
def class_pipeline(features, class_ground, clf):
#Call Shuffle splitter:
ss = StratifiedShuffleSplit(n_splits=50)
#sm = EditedNearestNeighbours()
#[features, class_ground] = sm.fit_resample(features, class_ground)
#Alocate variables:
class_pred_all = []
class_test_all = []
f1_score_all = []
#K fold cross validation shuffled:
for train, test in ss.split(features,class_ground):
#train/test split embedings:
features_train = features[train,:]
class_train = class_ground[train]
features_test = features[test,:]
class_test = class_ground[test]
#Fit the model (clf is a typically used word to call any classifier):
clf.fit(features_train,class_train)
#Predict literature class from word embeddings:
class_predicted = clf.predict(features_test)
# Calculate F1 score and store all predicted and test classes:
f1_score_all = np.append(f1_score_all,f1_score(class_test,class_predicted,average='weighted'))
class_pred_all = np.append(class_pred_all,class_predicted)
class_test_all = np.append(class_test_all,class_test)
return class_pred_all, class_test_all, f1_score_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment