导入
数据集与探索性数据分析
学习如何使用乳腺癌数据集进行探索性数据分析。
1import numpy as np 2import pandas as pd 3import seaborn as sns 4sns.set(font_scale=1.3) # setting font size for the whole notebook 5sns.set_style("whitegrid") # setting the style 6import matplotlib.pyplot as plt
1df=pd.read_csv('Breast_Cancer_Diagnostic_Wisconsin.csv') 2print(df.info())
1print(df.describe())
1print(df.target.value_counts())
1# figure and the axes 2fig1,ax=plt.subplots(nrows=1,ncols=2,figsize=(15,6),sharey=True) 3# groupby 4grouped_data=df.groupby("target") 5# plots 6grouped_data['mean radius'].hist(alpha=0.6,ax=ax[0]) 7grouped_data['mean texture'].hist(alpha=0.6,ax=ax[1]) 8# setting labels, title, and so 9ax[0].set_title("Mean radius distribution by class") 10ax[0].set_xlabel("Mean radius") 11ax[0].set_ylabel("Frequency") 12ax[1].set_title("Mean texture distribution by class") 13ax[1].set_xlabel("Mean texture") 14fig1.legend(["Malignant","Benign"],bbox_to_anchor=(0.57,0.92))
1import math 2 3nrows=6 4ncols=5 5fig2, ax = plt.subplots(nrows=nrows,ncols=ncols,figsize=(36,18)) 6for i,col_ in enumerate(df.columns[:-1]): # Omitting target column 7 ax_ = ax[math.floor(i/ncols),i%ncols] # grabbing axis -- A nice trick to avoid nested loops! 8 df.groupby("target")[col_].hist(ax=ax_, alpha=.6) # plot 9 ax_.set_xlabel(col_) 10 ax_.set_ylabel("Frequency") 11 ax_.legend(["Malignant","Benign"])