import pandas as pd import numpy as np import matplotlib.pyplot as plt #1.加载并查看数据基本信息 def read_dataset(file_root): dataframe=pd.read_csv(file_root) print("数据的基本信息:") print(dataframe.info()) print("数据集有%i行,%i列"%(dataframe.shape[0],dataframe.shape[1])) print("数据预览:") print(dataframe.head()) return dataframe #2.处理缺失数据 def processing_missing_data(dataframe): if (dataframe.isnull().values.any()): dataframe.fillna(0.) #dataframe.dropna() return dataframe #3.可视化数据 def visualize_data(dataframe,save_fig=True): fig=plt.figure() ax1=fig.add_subplot(2,2,1) ax2=fig.add_subplot(2,2,2) ax3=fig.add_subplot(2,2,3) ax4=fig.add_subplot(2,2,4) #解决matplotlib显示中文的问题 plt.rcParams["font.sans-serif"]=["SimHei"]#指定默认字体 plt.rcParams["axes.unicode_minus"]=False#解决保存图像是负号显示为方块问题 fig.suptitle("战队属性") ax1.scatter(dataframe["LeagueIndex"],dataframe["Age"]) ax1.set_xlabel("战队") ax1.set_ylabel("年龄") ax2.scatter(dataframe["LeagueIndex"],dataframe["HoursPerWeek"]) ax2.set_xlabel("战队") ax2.set_ylabel("每周游戏时间") ax3.scatter(dataframe["LeagueIndex"], dataframe["APM"]) ax3.set_xlabel("战队") ax3.set_ylabel("APM") ax4.scatter(dataframe["LeagueIndex"], dataframe["WorkersMade"]) ax4.set_xlabel("战队") ax4.set_ylabel("单位时间建造数") if save_fig: plt.savefig("H:/pythonfigure/league.png") plt.show() #4.可视化数据统计 def visualize_league_data(dataframe,attribution,save_fig=True): min_data=[] max_data=[] mean_data=[] for i in range(1,9): filter_data=dataframe.ix[dataframe["LeagueIndex"]==i,attribution] min_data.append(filter_data.min()) max_data.append(filter_data.max()) mean_data.append(filter_data.mean()) league_series=pd.Series(range(1,9),name="LeagueIndex") min_series=pd.Series(min_data,name="min") max_series=pd.Series(max_data,name="max") mean_series=pd.Series(mean_data,name="mean") dataframe_rst=pd.concat([league_series,min_series,max_series,mean_series],axis=1) if save_fig: dataframe_rst.to_csv("H:/pythonfigure/min_max_mean.csv") #统计值可视化 fig=plt.figure() ax1=fig.add_subplot(1,1,1) #解决matplotlib显示中文问题 plt.rcParams["font.sans-serif"]=["SimHei"]#指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 ax1.plot(dataframe_rst["LeagueIndex"],dataframe_rst["min"],color='b') ax1.plot(dataframe_rst["LeagueIndex"],dataframe_rst["max"],color='g') ax1.plot(dataframe_rst["LeagueIndex"],dataframe_rst["mean"],color="r") ax1.set_xlabel("战队") ax1.set_ylabel("attribution") ax1.legend() if save_fig: plt.savefig("H:/pythonfigure/min_max_mean.png") plt.show() #加载数据 dataframe=read_dataset("H:/pythonfigure/starcraft.csv") #处理缺失数据 df_data=processing_missing_data(dataframe) #可视化数据 visualize_data(df_data,save_fig=True) #可视化统计指标 visualize_league_data(df_data,"Age",save_fig=True) visualize_league_data(df_data,"HoursPerWeek",save_fig=True) visualize_league_data(df_data,"APM",save_fig=True) visualize_league_data(df_data,"WorkersMade",save_fig=True)
星际争霸游戏战队案例分析
最新推荐文章于 2021-01-18 13:26:47 发布