Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import itertools
- from sklearn.model_selection import train_test_split
- from sklearn import tree
- from sklearn.metrics import classification_report
- from sklearn.metrics import confusion_matrix
- from dtreeplt import dtreeplt
- def plot_confusion_matrix(cm, classes,
- normalize=False,
- title='Confusion matrix',
- cmap=plt.cm.Blues):
- """
- This function prints and plots the confusion matrix.
- Normalization can be applied by setting `normalize=True`.
- """
- plt.figure(figsize = (5, 5))
- plt.imshow(cm, interpolation='nearest', cmap=cmap)
- plt.title(title)
- plt.colorbar()
- tick_marks = np.arange(len(classes))
- plt.xticks(tick_marks, classes, rotation=45)
- plt.yticks(tick_marks, classes)
- if normalize:
- cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
- print("Normalized confusion matrix")
- else:
- print('Confusion matrix, without normalization')
- print(cm)
- thresh = cm.max() / 2.
- for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
- plt.text(j, i, cm[i, j],
- horizontalalignment="center",
- color="white" if cm[i, j] > thresh else "black")
- plt.tight_layout()
- plt.ylabel('True')
- plt.xlabel('Predicted')
- #讀入資料
- AllData = pd.read_csv('all_data.csv')
- CleanData = AllData.dropna()
- CleanData = CleanData.reset_index(drop=True)
- CleanData = CleanData.drop('mdate', axis = 1)
- CleanData = CleanData.drop('pmkt', axis = 1)
- X = CleanData.drop('Y', axis = 1)
- y = CleanData['Y']
- y = y.astype(int)
- #切分資料(70%訓練、30%測試)
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3,
- random_state=12345)
- #建立分類器
- clf = tree.DecisionTreeClassifier(criterion = 'gini', max_depth = 5)
- clf.fit(X_train, y_train)
- #混淆矩陣
- cnf_matrix = confusion_matrix(y_test, clf.predict(X_test))
- target_names=['0', '1']
- plot_confusion_matrix(cnf_matrix, classes =target_names,
- title='Confusion matrix')
- plt.show()
- print(confusion_matrix(y_test,clf.predict(X_test)))
- #精準度、召回率
- print(classification_report(y_test, clf.predict(X_test)))
- #印出正確率
- from sklearn import metrics
- accuracy = metrics.accuracy_score(y_test, clf.predict(X_test))
- print(accuracy)
- #特徵重要性
- feature_names= X.keys().tolist()
- clf.feature_importances_.tolist()
- a=clf.feature_importances_.tolist()
- df = pd.DataFrame({'feature':feature_names,
- 'feature_importance':clf.feature_importances_.tolist()})
- df = df.sort_values(by=['feature_importance'], ascending=False).reset_index(drop=True)
- def autolabel(rects):
- for rect in rects:
- height = rect.get_height()
- plt.text(rect.get_x()+rect.get_width()/2., 1.02*height, '%f'%float(height),
- ha='center', va='bottom')
- plt.style.use('ggplot')
- fig = plt.figure(figsize=(8, 4))
- plt.rcParams['font.family'] = 'DFKai-sb' #標楷體
- gini = plt.bar(df.index, df['feature_importance'], align='center')
- plt.xlabel('Feature') #X軸名稱
- plt.ylabel('Feature Importance') #Y軸名稱
- plt.xticks(df.index, df['feature']) #X軸項目名稱
- autolabel(gini)
- plt.show()
- dtree = dtreeplt(model = clf,feature_names = X.keys().tolist(), target_names = y.keys().tolist())
- fig = dtree.view()
- fig.savefig('output.png')
Add Comment
Please, Sign In to add comment