1- # Activity 3: Generating predictions and evaluating performance of grid search SVC model
1+ # Activity 3: Multiple Plot Types using Subplots
22
3- # continuing from Exercise 9:
3+ # import Items_Sold_by_Week.csv
4+ import pandas as pd
5+ Items_by_Week = pd .read_csv ('Items_Sold_by_Week.csv' )
46
5- # generate predicted classes
6- predicted_class = model .predict (X_test_scaled )
7+ # For scatterplot
8+ # import Height_by_Weight.csv
9+ import pandas as pd
10+ Weight_by_Height = pd .read_csv ('Weight_by_Height.csv' )
711
8- # evaluate performance with confusion matrix
9- from sklearn . metrics import confusion_matrix
12+ # For histogram and Box-and-Whisker
13+ # Create an array of 100 normally distributed numbers
1014import numpy as np
11- cm = pd .DataFrame (confusion_matrix (y_test , predicted_class ))
12- cm ['Total' ] = np .sum (cm , axis = 1 )
13- cm = cm .append (np .sum (cm , axis = 0 ), ignore_index = True )
14- cm .columns = ['Predicted No' , 'Predicted Yes' , 'Total' ]
15- cm = cm .set_index ([['Actual No' , 'Actual Yes' , 'Total' ]])
16- print (cm )
17-
18- # generate a classification report
19- from sklearn .metrics import classification_report
20- print (classification_report (y_test , predicted_class ))
15+ y = np .random .normal (loc = 0 , scale = 0.1 , size = 100 ) # 100 numbers with mean of 0 and standard deviation of 0.1
16+
17+ # generate figure with 6 subplots organized in 3 rows and 2 columns that do not overlap
18+ import matplotlib .pyplot as plt
19+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
20+ plt .tight_layout () # prevent plot overlap
21+
22+ # Name the titles
23+ import matplotlib .pyplot as plt
24+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
25+ # line plot (top left)
26+ axes [0 ,0 ].set_title ('Line' )
27+ # Bar plot (top right)
28+ axes [0 ,1 ].set_title ('Bar' )
29+ # Horizontal bar plot (middle left)
30+ axes [1 ,0 ].set_title ('Horizontal Bar' )
31+ # Histogram (middle right)
32+ axes [1 ,1 ].set_title ('Histogram' )
33+ # Scatterplot (bottom left)
34+ axes [2 ,0 ].set_title ('Scatter' )
35+ # Box-and-Whisker
36+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
37+ plt .tight_layout () # prevent plot overlap
38+
39+ # in the ‘Line’, ‘Bar’, and ‘Horizontal Bar’ axes, plot ‘Items_Sold’ by ‘Week’ from the ‘Items_by_Week’
40+ # Horizontal bar
41+ import matplotlib .pyplot as plt
42+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
43+ # line plot (top left)
44+ axes [0 ,0 ].plot (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for line plot
45+ axes [0 ,0 ].set_title ('Line' )
46+ # Bar plot (top right)
47+ axes [0 ,1 ].bar (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for bar plot
48+ axes [0 ,1 ].set_title ('Bar' )
49+ # Horizontal bar plot (middle left)
50+ axes [1 ,0 ].barh (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for horizontal bar plot
51+ axes [1 ,0 ].set_title ('Horizontal Bar' )
52+ # Histogram (middle right)
53+ axes [1 ,1 ].set_title ('Histogram' )
54+ # Scatterplot (bottom left)
55+ axes [2 ,0 ].set_title ('Scatter' )
56+ # Box-and-Whisker
57+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
58+ plt .tight_layout () # prevent plot overlap
59+
60+ # in the 'Histogram' and 'Box-and-Whisker axes, plot ‘Items_Sold’ by ‘Week’ from the ‘Items_by_Week’
61+ # Horizontal bar
62+ import matplotlib .pyplot as plt
63+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
64+ # line plot (top left)
65+ axes [0 ,0 ].plot (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for line plot
66+ axes [0 ,0 ].set_title ('Line' )
67+ # Bar plot (top right)
68+ axes [0 ,1 ].bar (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for bar plot
69+ axes [0 ,1 ].set_title ('Bar' )
70+ # Horizontal bar plot (middle left)
71+ axes [1 ,0 ].barh (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for horizontal bar plot
72+ axes [1 ,0 ].set_title ('Horizontal Bar' )
73+ # Histogram (middle right)
74+ axes [1 ,1 ].hist (y , bins = 20 )
75+ axes [1 ,1 ].set_title ('Histogram' )
76+ # Scatterplot (bottom left)
77+ axes [2 ,1 ].boxplot (y )
78+ axes [2 ,0 ].set_title ('Scatter' )
79+ # Box-and-Whisker
80+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
81+ plt .tight_layout () # prevent plot overlap
82+
83+ # add scatterplot
84+ import matplotlib .pyplot as plt
85+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
86+ # line plot (top left)
87+ axes [0 ,0 ].plot (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for line plot
88+ axes [0 ,0 ].set_title ('Line' )
89+ # Bar plot (top right)
90+ axes [0 ,1 ].bar (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for bar plot
91+ axes [0 ,1 ].set_title ('Bar' )
92+ # Horizontal bar plot (middle left)
93+ axes [1 ,0 ].barh (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for horizontal bar plot
94+ axes [1 ,0 ].set_title ('Horizontal Bar' )
95+ # Histogram (middle right)
96+ axes [1 ,1 ].hist (y , bins = 20 ) # for histogram
97+ axes [1 ,1 ].set_title ('Histogram' )
98+ # Scatterplot (bottom left)
99+ axes [2 ,0 ].scatter (Weight_by_Height ['Height' ], Weight_by_Height ['Weight' ]) # for scatterplot
100+ axes [2 ,0 ].set_title ('Scatter' )
101+ # Box-and-Whisker
102+ axes [2 ,1 ].boxplot (y ) # for Box-and-Whisker
103+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
104+ plt .tight_layout () # prevent plot overlap
105+
106+ # Set x- and y-axis for each subplot
107+ import matplotlib .pyplot as plt
108+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 )
109+ # line plot (top left)
110+ axes [0 ,0 ].plot (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for line plot
111+ axes [0 ,0 ].set_xlabel ('Week' )
112+ axes [0 ,0 ].set_ylabel ('Items Sold' )
113+ axes [0 ,0 ].set_title ('Line' )
114+ # Bar plot (top right)
115+ axes [0 ,1 ].bar (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for bar plot
116+ axes [0 ,1 ].set_xlabel ('Week' )
117+ axes [0 ,1 ].set_ylabel ('Items Sold' )
118+ axes [0 ,1 ].set_title ('Bar' )
119+ # Horizontal bar plot (middle left)
120+ axes [1 ,0 ].barh (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for horizontal bar plot
121+ axes [1 ,0 ].set_xlabel ('Items Sold' )
122+ axes [1 ,0 ].set_ylabel ('Week' )
123+ axes [1 ,0 ].set_title ('Horizontal Bar' )
124+ # Histogram (middle right)
125+ axes [1 ,1 ].hist (y , bins = 20 ) # for histogram
126+ axes [1 ,1 ].set_xlabel ('y' )
127+ axes [1 ,1 ].set_ylabel ('Frequency' )
128+ axes [1 ,1 ].set_title ('Histogram' )
129+ # Scatterplot (bottom left)
130+ axes [2 ,0 ].scatter (Weight_by_Height ['Height' ], Weight_by_Height ['Weight' ]) # for scatterplot
131+ axes [2 ,0 ].set_xlabel ('Height' )
132+ axes [2 ,0 ].set_ylabel ('Weight' )
133+ axes [2 ,0 ].set_title ('Scatter' )
134+ # Box-and-Whisker
135+ axes [2 ,1 ].boxplot (y ) # for Box-and-Whisker
136+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
137+ plt .tight_layout () # prevent plot overlap
138+
139+ # Enlarge the figure size and Save the figure
140+ import matplotlib .pyplot as plt
141+ fig , axes = plt .subplots (nrows = 3 , ncols = 2 , figsize = (8 ,8 )) # for figure size
142+ # line plot (top left)
143+ axes [0 ,0 ].plot (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for line plot
144+ axes [0 ,0 ].set_xlabel ('Week' )
145+ axes [0 ,0 ].set_ylabel ('Items Sold' )
146+ axes [0 ,0 ].set_title ('Line' )
147+ # Bar plot (top right)
148+ axes [0 ,1 ].bar (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for bar plot
149+ axes [0 ,1 ].set_xlabel ('Week' )
150+ axes [0 ,1 ].set_ylabel ('Items Sold' )
151+ axes [0 ,1 ].set_title ('Bar' )
152+ # Horizontal bar plot (middle left)
153+ axes [1 ,0 ].barh (Items_by_Week ['Week' ], Items_by_Week ['Items_Sold' ]) # for horizontal bar plot
154+ axes [1 ,0 ].set_xlabel ('Items Sold' )
155+ axes [1 ,0 ].set_ylabel ('Week' )
156+ axes [1 ,0 ].set_title ('Horizontal Bar' )
157+ # Histogram (middle right)
158+ axes [1 ,1 ].hist (y , bins = 20 ) # for histogram
159+ axes [1 ,1 ].set_xlabel ('y' )
160+ axes [1 ,1 ].set_ylabel ('Frequency' )
161+ axes [1 ,1 ].set_title ('Histogram' )
162+ # Scatterplot (bottom left)
163+ axes [2 ,0 ].scatter (Weight_by_Height ['Height' ], Weight_by_Height ['Weight' ]) # for scatterplot
164+ axes [2 ,0 ].set_xlabel ('Height' )
165+ axes [2 ,0 ].set_ylabel ('Weight' )
166+ axes [2 ,0 ].set_title ('Scatter' )
167+ # Box-and-Whisker
168+ axes [2 ,1 ].boxplot (y ) # for Box-and-Whisker
169+ axes [2 ,1 ].set_title ('Box-and-Whisker' )
170+ plt .tight_layout () # prevent plot overlap
171+ fig .savefig ('Six_Subplots' ) # save figure
0 commit comments