Created
June 4, 2019 17:47
-
-
Save DipenChawla/fb969345e88203de428f3d0e7629b792 to your computer and use it in GitHub Desktop.
ML Classifiers for DrugNeuroAnalytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_train, X_test, y_train, y_test,indices_train, indices_test = train_test_split(df_final, df_final_labels,reviews.index, test_size=0.2, random_state=42) | |
from sklearn.model_selection import cross_val_score | |
#Classifier 1 - Linear SVM with OnevsRest | |
from sklearn.svm import LinearSVC | |
svc = LinearSVC(dual=False, multi_class='ovr', class_weight='balanced') | |
scores = cross_val_score(svc, X_train, y_train, scoring='f1_weighted', n_jobs=-1, cv=10) | |
print('Cross-validation f1 score {0:.2f}, std {1:.2f}.'.format(np.mean(scores), np.std(scores) * 100)) | |
svc.fit(X_train, y_train) | |
pred = svc.predict(X_test) | |
#Classifier 2 - Logistic regression | |
from sklearn.linear_model import LogisticRegression | |
clf = LogisticRegression(C=30.0, solver='newton-cg', class_weight='balanced', | |
multi_class='multinomial', n_jobs=-1, random_state=42) | |
scores = cross_val_score(clf, X_train, y_train, scoring='f1_weighted', n_jobs=-1, cv=10) | |
print('Cross-validation f1 score {0:.2f}, std {1:.2f}.'.format(np.mean(scores), np.std(scores) * 100)) | |
clf.fit(X_train, y_train) | |
pred = clf.predict(X_test) | |
#Classifier 3 - Random forest | |
from sklearn.ensemble import RandomForestClassifier | |
random = RandomForestClassifier(n_estimators=1000, random_state=0) | |
scores = cross_val_score(random, X_train, y_train, scoring='f1_weighted', n_jobs=-1, cv=10) | |
print('Cross-validation f1 score {0:.2f}, std {1:.2f}.'.format(np.mean(scores), np.std(scores) * 100)) | |
random.fit(X_train, y_train) | |
pred = random.predict(X_test) | |
#Classifier 4 - Bagging Meta-Estimator with Logistic Regressor | |
from sklearn.ensemble import BaggingClassifier | |
bagging = BaggingClassifier(clf) | |
scores = cross_val_score(bagging, X_train, y_train, scoring='f1_weighted', n_jobs=-1, cv=10) | |
print('Cross-validation f1 score {0:.2f}, std {1:.2f}.'.format(np.mean(scores), np.std(scores) * 100)) | |
bagging.fit(X_train, y_train) | |
pred = bagging.predict(X_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment