|
5 | 5 | import matplotlib.pyplot as plt |
6 | 6 | from sklearn.datasets import load_wine |
7 | 7 |
|
| 8 | + |
| 9 | +# ------------------------------------------------------------------------------------------------ |
| 10 | + |
8 | 11 | # Read in the data |
9 | 12 | # NOTE that this loads as a dictionairy |
10 | 13 | wine_data = load_wine() |
|
19 | 22 |
|
20 | 23 | print("The wine dataset has " + str(num_features) + " features") |
21 | 24 | print(wine_data.feature_names) |
22 | | -print("The wine dataset has " + str(num_classes) + " classes") |
| 25 | +print("The wine dataset has " + str(num_classes) + " categoryes") |
23 | 26 | print(wine_data.target_names) |
24 | 27 |
|
25 | 28 |
|
26 | 29 | # Put everything into a Pandas DataFrame |
27 | | -data = pd.DataFrame(data=np.c_[train_data, train_labels], columns=wine_data.feature_names + ['class']) |
| 30 | +data = pd.DataFrame(data=np.c_[train_data, train_labels], columns=wine_data.feature_names + ['category']) |
28 | 31 | # print(tabulate(data, headers='keys', tablefmt='psql')) |
29 | 32 |
|
| 33 | +# ------------------------------------------------------------------------------------------------ |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
30 | 38 |
|
| 39 | +# ------------------------------------------------------------------------------------------------ |
31 | 40 |
|
32 | 41 | # Create histogram |
33 | 42 | hist_feature_name='color_intensity' |
|
38 | 47 | plt.xlabel(hist_feature_name) |
39 | 48 | plt.show() |
40 | 49 |
|
| 50 | +# ------------------------------------------------------------------------------------------------ |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +# ------------------------------------------------------------------------------------------------ |
| 59 | + |
| 60 | +# Create grouped bar plot |
| 61 | + |
| 62 | + |
| 63 | +var_name_1 = 'alcohol' |
| 64 | +var_name_2 = 'color_intensity' |
| 65 | + |
| 66 | + |
| 67 | +# Setting the positions and width for the bars |
| 68 | +pos = list(range(num_classes)) |
| 69 | +width = 0.1 |
| 70 | + |
| 71 | +# Plotting the bars |
| 72 | +fig, ax = plt.subplots(figsize=(10,5)) |
| 73 | + |
| 74 | +# Set the position of the x ticks |
| 75 | +ax.set_xticks([p + 1.5 * width for p in pos]) |
| 76 | +ax.set_xticklabels(list(range(num_classes))) |
| 77 | + |
| 78 | +class_0_data = data[data.category==0] |
| 79 | +alcohol_values_0 = class_0_data[var_name_1].values |
| 80 | +mean_alcohol_0 = np.mean(alcohol_values_0) |
| 81 | +color_values_0 = class_0_data[var_name_2].values |
| 82 | +mean_color_0 = np.mean(color_values_0) |
| 83 | + |
| 84 | +class_1_data = data[data.category==1] |
| 85 | +alcohol_values_1 = class_1_data[var_name_1].values |
| 86 | +mean_alcohol_1 = np.mean(alcohol_values_1) |
| 87 | +color_values_1 = class_1_data[var_name_2].values |
| 88 | +mean_color_1 = np.mean(color_values_1) |
41 | 89 |
|
| 90 | +class_2_data = data[data.category==2] |
| 91 | +alcohol_values_2 = class_2_data[var_name_1].values |
| 92 | +mean_alcohol_2 = np.mean(alcohol_values_2) |
| 93 | +color_values_2 = class_2_data[var_name_2].values |
| 94 | +mean_color_2 = np.mean(color_values_2) |
| 95 | + |
| 96 | +plt.bar(pos, [mean_alcohol_0, mean_alcohol_1, mean_alcohol_2], width, alpha=1.0, color='#EE3224', label='alcohol') |
| 97 | +plt.bar([p + width for p in pos], [mean_color_0, mean_color_1, mean_color_2], width, alpha=1.0, color='#F78F1E', label='color_intensity') |
| 98 | + |
| 99 | + |
| 100 | +plt.legend([var_name_1, 'color_intensity'], loc='upper left') |
| 101 | + |
| 102 | +plt.show() |
| 103 | + |
| 104 | + |
| 105 | +# ------------------------------------------------------------------------------------------------ |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +# ------------------------------------------------------------------------------------------------ |
42 | 116 |
|
43 | 117 | # Create scatterplot |
44 | 118 | scatter_feature_name_1='color_intensity' |
|
52 | 126 |
|
53 | 127 |
|
54 | 128 | # Create scatterplot matrix |
55 | | -fig = sns.pairplot(data=data[['alcohol', 'color_intensity', 'malic_acid', 'magnesium', 'class']], hue='class') |
| 129 | +fig = sns.pairplot(data=data[['alcohol', 'color_intensity', 'malic_acid', 'magnesium', 'category']], hue='category') |
56 | 130 |
|
57 | 131 | plt.show() |
58 | 132 |
|
| 133 | +# ------------------------------------------------------------------------------------------------ |
59 | 134 |
|
60 | 135 |
|
| 136 | + |
| 137 | +# ------------------------------------------------------------------------------------------------ |
| 138 | + |
61 | 139 | # Create bee swarm plot |
62 | | -sns.swarmplot(x='class', y='total_phenols', data=data) |
| 140 | +sns.swarmplot(x='category', y='total_phenols', data=data) |
63 | 141 | plt.show() |
64 | 142 |
|
| 143 | +# ------------------------------------------------------------------------------------------------ |
| 144 | + |
| 145 | + |
| 146 | + |
65 | 147 |
|
66 | 148 |
|
| 149 | +# ------------------------------------------------------------------------------------------------ |
67 | 150 |
|
68 | 151 | # Cumulative Distribution Function Plots |
69 | 152 |
|
|
0 commit comments