|
34 | 34 |
|
35 | 35 | # https://kknews.cc/code/lnnxmr9.html
|
36 | 36 |
|
| 37 | +# Training code |
| 38 | +# 新增網路硬碟 |
| 39 | +from google.colab import drive |
| 40 | +drive.mount("/gdrive", force_remount=True) |
| 41 | +#drive.mount('/gdrive') |
| 42 | +import os |
| 43 | +os.getcwd() |
| 44 | + |
| 45 | +#設定Google Drive路徑 |
| 46 | +root = "/gdrive" |
| 47 | +path = "/gdrive/My Drive/Colab Notebooks/Visualization" |
| 48 | +os.chdir(path) |
| 49 | +os.getcwd() |
| 50 | + |
| 51 | +### 利用 PANDAS 取得酒的品質資料 |
| 52 | +df_red = pd.read_csv("winequality_red.csv") |
| 53 | +df_white = pd.read_csv("winequality_white.csv") |
| 54 | + |
| 55 | +#資料整理 |
| 56 | +df_red["color"] = "R" |
| 57 | +df_white["color"] = "W" |
| 58 | + |
| 59 | +#整合紅酒與白酒的資料 |
| 60 | +df_all=pd.concat([df_red,df_white],axis=0) |
| 61 | + |
| 62 | +# 檢查合併後的資料集 |
| 63 | +df_all.head() |
| 64 | + |
| 65 | +df_all.rename(columns={'fixed acidity': 'fixed_acidity','citric acid':'citric_acid', |
| 66 | + 'volatile acidity':'volatile_acidity','residual sugar':'residual_sugar', |
| 67 | + 'free sulfur dioxide':'free_sulfur_dioxide', |
| 68 | + 'total sulfur dioxide':'total_sulfur_dioxide'}, inplace=True) |
| 69 | +# 檢查合併後的資料集 |
| 70 | +df_all.head() |
| 71 | + |
| 72 | +#處理缺失值 |
| 73 | +df = pd.get_dummies(df_all, columns=["color"]) |
| 74 | +df_all.isnull().sum() |
| 75 | + |
| 76 | +#可以使用 info() 方法瞭解有關資料集屬性的更多資訊。特別是行和列的數量、列名稱、它們的數據類型和空值數。 |
| 77 | +df_all.info() |
| 78 | + |
| 79 | +#要瞭解數據集的統計摘要,即記錄數、平均值、標準差、最小值和最大值,我們使用描述()。 |
| 80 | +df_all.describe() |
| 81 | + |
| 82 | +#可視化所有數值數據。在垂直軸上計數,在水平軸上使用值範圍。hist 函數通過將所有屬性繪製在一起使操作變得簡單。 |
| 83 | +df_all.hist(bins=10, color='lightblue',edgecolor='blue',linewidth=1.0, |
| 84 | + xlabelsize=8, ylabelsize=8, grid=False) |
| 85 | + |
| 86 | +plt.tight_layout(rect=(0, 0, 1.2, 1.2)) |
| 87 | + |
| 88 | +#Plotting heatmap |
| 89 | +f, ax = plt.subplots(figsize=(10, 6)) |
| 90 | +b = sns.heatmap(df_all.corr(), annot=True, linewidths=.05, ax=ax) |
| 91 | +f.subplots_adjust(top=0.93) |
| 92 | +bottom, top = ax.get_ylim() |
| 93 | +ax.set_ylim(bottom + 0.5, top - 0.5) |
| 94 | +title= f.suptitle('Correlation Heatmap for wine attributes', fontsize=12) |
| 95 | + |
| 96 | +#Plotting Jointplot, 使用 'reg'== regression 回歸線繪製關係圖 |
| 97 | +a = sns.jointplot("fixed_acidity","citric_acid",data = df_all,kind ='reg', color = None) |
| 98 | +b = sns.jointplot("alcohol", "citric_acid", data = df_all, kind = 'reg') |
| 99 | +c = sns.jointplot("volatile_acidity", "citric_acid", data = df_all, kind = 'reg') |
| 100 | + |
| 101 | +''' |
| 102 | +Swarm沿分類軸(質量)調整記錄。這種繪圖將記錄分別標記,而不會重疊。這就是為什麼它最適合小型數據集的原因。 |
| 103 | +在此圖表中,您可以看到硫酸鹽的數量,根據品質。品質值為 6 的硫酸鹽密度最高,品質等級為 9 和 3 的最低 |
| 104 | +''' |
| 105 | + |
| 106 | +#在此一區域寫下程式碼 |
| 107 | + |
| 108 | +f,ax = plt.subplots(figsize=(12, 4)) |
| 109 | +p = sns.catplot(x="quality", y="sulphates", kind = 'swarm', data= df_all, palette= 'GnBu_d', ax=ax) |
| 110 | + |
| 111 | +''' |
| 112 | +PairGrid 允許我們使用相同的繪圖類型繪製子圖網格來可視化數據。 |
| 113 | +與 FacetGrid 不同,它在每個子圖使用不同的變數對。它形成子圖的矩陣。它有時也被稱為"散點圖矩陣"。 |
| 114 | +對網格的用法與分面網格類似。首先初始化網格,然後傳遞繪圖函數。 |
| 115 | +''' |
| 116 | + |
| 117 | +#在此一區域寫下程式碼 |
| 118 | +#設定底圖樣式 |
| 119 | +sns.set(style="white") |
| 120 | + |
| 121 | +#利用PairGrid 繪製對角圖 |
| 122 | +g = sns.PairGrid(df_all, diag_sharey=False) |
| 123 | +g.map_upper(sns.scatterplot) |
| 124 | +g.map_lower(sns.kdeplot, colors="C0") |
| 125 | +g.map_diag(sns.kdeplot, lw=2) |
| 126 | + |
0 commit comments