1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #!/usr/bin/env python # coding: utf-8 # In[28]: from __future__ import division import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker import numpy as np def plot_confusion_matrix(cm, classes, normalize = True , title = 'Confusion matrix' , cmap = plt.cm.hot): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype( 'float' ) / cm. sum (axis = 1 )[:, np.newaxis] print ( "Normalized confusion matrix" ) else : print ( 'Confusion matrix, without normalization' ) print (cm) plt.imshow(cm, interpolation = 'nearest' ) # plt.title(title) plt.colorbar() tick_marks = np.arange( len (classes)) plt.xticks(tick_marks, classes, rotation = 90 ) plt.yticks(tick_marks, classes) # fmt = '.2f' if normalize else 'd' # thresh = cm.max() / 2. # for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): # plt.text(j, i, format(cm[i, j], fmt), # horizontalalignment="center", # color="white" if cm[i, j] > thresh else "black") data = pd.read_csv(r "experiment\61476-2000\confusion_matrix.csv" ) all_categories = data.iloc[:, 0 ] confusion = np.array(data.iloc[:, 1 :]) # print(confusion) # Normalize by dividing every row by its sum # for i in range(len(all_categories)): # for j in range(len(all_categories)): # confusion[i][j] = confusion[i][j] / confusion[i].sum() #Set up plot fig = plt.figure() ax = fig.add_subplot( 111 ) cax = ax.matshow(confusion) # fig.colorbar(cax) # Set up axes ax.set_xticklabels([''] + all_categories, rotation = 90 ) ax.set_yticklabels([''] + all_categories) # Force label at every tick ax.xaxis.set_major_locator(ticker.MultipleLocator( 1 )) ax.yaxis.set_major_locator(ticker.MultipleLocator( 1 )) plot_confusion_matrix(confusion, classes = list (all_categories), title = "Confusion matrix" ) # sphinx_gallery_thumbnail_number = 2 plt.show() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2016-05-13 MATLAB时间序列预测Prediction of time series with NAR neural network