machine learning classification algorithms code example
Example 1: best algorithm for classification
for clf in classifiers: clf.fit(X_train, y_train) y_pred= clf.predict(X_test) acc = accuracy_score(y_test, y_pred) print("Accuracy of %s is %s"%(clf, acc)) cm = confusion_matrix(y_test, y_pred) print("Confusion Matrix of %s is %s"%(clf, cm)
Example 2: best algorithm for classification
Accuracy of XGBClassifier is 95.55Confusion Matrix of XGBClassifier is [[ 9 0 0] [ 0 15 1] [ 0 1 19]]Accuracy of SVC is 95.55Confusion Matrix of SVC is [[ 9 0 0] [ 0 16 0] [ 0 2 18]]Accuracy of DecisionTreeClassifier is 88.88Confusion Matrix of DecisionTreeClassifier is [[ 9 0 0] [ 0 15 1] [ 0 4 16]]Accuracy of RandomForestClassifier is 84.44Confusion Matrix of RandomForestClassifier is [[ 9 0 0] [ 0 15 1] [ 0 6 14]]
Example 3: best algorithm for classification
model1 = xgboost.XGBClassifier()classifiers.append(model1)model2 = svm.SVC()classifiers.append(model2)model3 = tree.DecisionTreeClassifier()classifiers.append(model3)model4 = RandomForestClassifier()classifiers.append(model4)