<jupyter_start><jupyter_text>Vectorize with count and tf-idfKeep words appearing in 10% to 70 % of the posts.<jupyter_code>from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer

# Posts to a matrix of token counts
cntizer = CountVectorizer(analyzer="word", 
                             max_features=1500, 
                             tokenizer=None,    
                             preprocessor=None, 
                             stop_words=None,  
                             max_df=0.7,
                             min_df=0.1) 

# Learn the vocabulary dictionary and return term-document matrix
print("CountVectorizer...")
X_train = cntizer.fit_transform(X_train)
X_test = cntizer.transform(X_test)
# Transform the count matrix to a normalized tf or tf-idf representation
tfizer = TfidfTransformer()

print("Tf-idf...")
# Learn the idf vector (fit) and transform a count matrix to a tf-idf representation
X_train =  tfizer.fit_transform(X_train).toarray()
X_test =  tfizer.transform(X_test).toarray()
print("MBTI 1st row: %s" % translate_back(y_train[0,:]))
print("Y: Binarized MBTI 1st row: %s" % y_train[0,:])
feature_names = list(enumerate(cntizer.get_feature_names()))
feature_names[:10]<jupyter_output>CountVectorizer...
Tf-idf...
MBTI 1st row: ISFP
Y: Binarized MBTI 1st row: [0 1 0 1]<jupyter_text>Linear SVM - One vs Rest<jupyter_code>from sklearn.svm import LinearSVC

classif = OneVsRestClassifier(LinearSVC())
classif.fit(X_train, y_train)
y = classif.predict(X_test)
print('accuracy_score', accuracy_score(y_test, y), '\n',
      'roc_auc_score', roc_auc_score(y_test, y), '\n',
      'hamming_loss', hamming_loss(y_test, y))<jupyter_output>accuracy_score 0.3319884726224784 
 roc_auc_score 0.6007018964536487 
 hamming_loss 0.2404899135446686<jupyter_text>Chained Linear SVM<jupyter_code>from sklearn.svm import LinearSVC

classif = ClassifierChain(LinearSVC())
classif.fit(X_train, y_train)
y = classif.predict(X_test)

print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, y.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.3285302593659942 
 roc_auc_score 0.6273825065533728 
 hamming_loss 0.2489913544668588<jupyter_text>SVM, Gaussian kernel - One vs Rest<jupyter_code>from sklearn.svm import SVC

classif = OneVsRestClassifier(SVC(gamma='scale'))
classif.fit(X_train, y_train)
y = classif.predict(X_test)

print('accuracy_score', accuracy_score(y_test, y), '\n',
      'roc_auc_score', roc_auc_score(y_test, y), '\n',
      'hamming_loss', hamming_loss(y_test, y))<jupyter_output>accuracy_score 0.2904899135446686 
 roc_auc_score 0.5665363295727185 
 hamming_loss 0.24927953890489912<jupyter_text>Chained SVM, Gaussian kernel<jupyter_code>from sklearn.svm import SVC

classif = ClassifierChain(SVC(gamma='scale'))
classif.fit(X_train, y_train)
y = classif.predict(X_test)

print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, y.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.2881844380403458 
 roc_auc_score 0.5611140850486074 
 hamming_loss 0.2524495677233429<jupyter_text>MLKNN<jupyter_code>from skmultilearn.adapt import MLkNN


# Note that this classifier can throw up errors when handling sparse matrices.
X_train_mlknn = lil_matrix(X_train).toarray()
y_train_mlknn = lil_matrix(y_train).toarray()
X_test_mlknn = lil_matrix(X_test).toarray()

classif = MLkNN()
classif.fit(X_train_mlknn, y_train_mlknn)

y = classif.predict(X_test_mlknn)
yp = classif.predict_proba(X_test_mlknn)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.2622478386167147 
 roc_auc_score 0.5658352239225045 
 hamming_loss 0.28285302593659944<jupyter_text>Chained GaussianNB<jupyter_code>from sklearn.naive_bayes import GaussianNB
# initialize classifier chains multi-label classifier
classif = ClassifierChain(GaussianNB())
classif.fit(X_train, y_train)
y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.20922190201729107 
 roc_auc_score 0.6912461019993441 
 hamming_loss 0.33645533141210376<jupyter_text>Chained BernoulliNB<jupyter_code>from sklearn.naive_bayes import BernoulliNB

classif = ClassifierChain(BernoulliNB(alpha=1.0, binarize=None, class_prior=None, fit_prior=True))
classif.fit(X_train, y_train)
y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.2904899135446686 
 roc_auc_score 0.7198294564753879 
 hamming_loss 0.25994236311239194<jupyter_text>Chained BaggedBernoulliNB<jupyter_code>from sklearn.ensemble import BaggingClassifier

classif = ClassifierChain(BaggingClassifier(base_estimator=BernoulliNB()))
classif.fit(X_train, y_train)
y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.2685878962536023 
 roc_auc_score 0.7004657857359968 
 hamming_loss 0.2889048991354467<jupyter_text>Chained Adaboost with Decision Trees<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
classif = ClassifierChain(AdaBoostClassifier(base_estimator=DecisionTreeClassifier()))
classif.fit(X_train, y_train)
y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.1642651296829971 
 roc_auc_score 0.5412128136372868 
 hamming_loss 0.3612391930835735<jupyter_text>Chained LogisticRegression<jupyter_code>from sklearn.linear_model import LogisticRegression

classif = ClassifierChain(LogisticRegression(solver='lbfgs'))
classif.fit(X_train, y_train)
y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y.toarray()), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp.toarray()), '\n',
      'hamming_loss', hamming_loss(y_test, y.toarray()))<jupyter_output>accuracy_score 0.3504322766570605 
 roc_auc_score 0.7446379886968562 
 hamming_loss 0.23631123919308358<jupyter_text>RandomForest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
classif = RandomForestClassifier(n_estimators=100, class_weight=None)
classif.fit(X_train, y_train)
y = classif.predict(X_test)

print('accuracy_score', accuracy_score(y_test, y), '\n',
      'roc_auc_score', roc_auc_score(y_test, y), '\n',
      'hamming_loss', hamming_loss(y_test, y))<jupyter_output>accuracy_score 0.30086455331412104 
 roc_auc_score 0.6340715137209887 
 hamming_loss 0.2695965417867435<jupyter_text>FCNN (MLP)<jupyter_code>from sklearn.neural_network import MLPClassifier

classif = MLPClassifier()
classif.fit(X_train, y_train)


y = classif.predict(X_test)
yp = classif.predict_proba(X_test)
print('accuracy_score', accuracy_score(y_test, y), '\n',
      'roc_auc_score', roc_auc_score(y_test, yp), '\n',
      'hamming_loss', hamming_loss(y_test, y))<jupyter_output>accuracy_score 0.30086455331412104 
 roc_auc_score 0.7124481967804894 
 hamming_loss 0.2695965417867435
StarcoderdataJupyterStructuredCleanDedup