In the print dialog, choose "Save as PDF" as the destination.
Machine Learning, Week 2
Look at the data before you model it: the charts every notebook of the course uses, and when each one fits.
df.plot.scatter and df.plot.histboxplot, violinplot, regplot, lmplotWeek 2 of the plan
| Part | What we do | Time |
|---|---|---|
| 1 | Matplotlib on the clinic data, a histogram by hand | 30 min |
| 2 | The poll data: line plot, a boxplot by hand | 20 min |
| 3 | Pandas and Seaborn on the wine data | 20 min |
| 4 | Correlation, the heatmap, choosing a chart | 15 min |
| 5 | Practice with answers, then takeaways | 35 min |
https://colab.research.google.com/github/
tirthajyoti/Machine-Learning-with-Python/blob/master/
Pandas%20and%20Numpy/Matplotlib_Seaborn_basics.ipynbPart 1
One figure, built call by call
Twelve patients: age in years, weight in kg, height in cm
people = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6',
'P7', 'P8', 'P9', 'P10', 'P11', 'P12']
age = [21, 12, 32, 45, 37, 18,
28, 52, 5, 40, 48, 15]
weight = [55, 35, 77, 68, 70, 60,
72, 69, 18, 65, 82, 48]
height = [160, 135, 170, 165, 173, 168,
175, 159, 105, 171, 155, 158]import matplotlib.pyplot as plt
plt.scatter(age, height)
plt.show()
Each extra call adds one element to the same figure
| Call | Adds |
|---|---|
plt.figure(figsize=(8, 6)) | figure size in inches |
plt.title, plt.xlabel, plt.ylabel | the texts around the plot |
plt.ylim(100, 200) | the visible y range |
plt.xticks(list) | where x tick marks go |
| Call | Adds |
|---|---|
plt.scatter(c=, s=, edgecolors=) | dot colour, size, outline |
plt.text(x, y, s) | text at a data point |
plt.vlines, plt.hlines | vertical, horizontal lines |
plt.legend(list, loc=2) | a legend, upper left |
plt.xticks([i * 5 for i in range(12)],
fontsize=15)[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]range(12) gives i = 0 to 11, times 5: twelve ticks up to 55.
One number across named items; every bar starts at zero
plt.figure(figsize=(12, 4))
plt.bar(x=people, height=weight,
width=0.6, color='orange',
edgecolor='k', alpha=0.6)
plt.xticks(fontsize=14, rotation=30)
plt.show()width=0.6: each bar fills 60% of its slotalpha=0.6: partly transparentrotation=30: tilted labels
Step 1 of 3
plt.hist(weight, color='red', edgecolor='k', alpha=0.75, bins=5)Step 2 of 3
A bin holds its left edge, not its right one; the last bin also holds 82
| Bin | Weights inside | Count |
|---|---|---|
| 18 to 30.8 | 18 | 1 |
| 30.8 to 43.6 | 35 | 1 |
| 43.6 to 56.4 | 55, 48 | 2 |
| 56.4 to 69.2 | 68, 60, 69, 65 | 4 |
| 69.2 to 82 | 77, 70, 72, 82 | 4 |
Step 3 of 3
1, 1, 2, 4, 4 add up to 12, one per patientplt.hist returns the same counts and edges
Part 2
A line through time, and a box around a spread
np.random.seed(0) # added
days = np.arange(1, 31)
candidate_A = 50 + days * 0.07 \
+ 2 * np.random.randn(30)
candidate_B = 50 - days * 0.1 \
+ 3 * np.random.randn(30)
plt.style.use('fivethirtyeight')
plt.plot(days, candidate_A, 'o-', c='blue')
plt.plot(days, candidate_B, '^-', c='green')plt.style.use stays on for every later plot
Step 1 of 5
Sort, then find the median
sorted: 105 135 155 158 159 160 165 168 170 171 173 175
position: 0 1 2 3 4 5 6 7 8 9 10 11Step 2 of 5
Step 3 of 5
Step 4 of 5
Fences: 137.75 and 189.75
| Part | Value | Why |
|---|---|---|
| Lower whisker | 155 | smallest height at or above 137.75 |
| Upper whisker | 175 | largest height at or below 189.75 |
| Outliers | 105, 135 | below the lower fence: P9 and P2, the children |
Step 5 of 5
plt.boxplot(x=[height], showmeans=True)
plt.style.use('ggplot')
plt.boxplot(x=[candidate_A, candidate_B],
showmeans=True)
plt.xticks([1, 2], ['Candidate A', 'Candidate B'])| A | B | |
|---|---|---|
| Median | 51.947 | 47.389 |
| Q1 to Q3 | 50.87 to 53.53 | 45.35 to 48.89 |
| Outlier | 46.364 | 54.452 |
| Mean | 51.971 | 47.581 |

Part 3
Name the columns, let the library do the rest
df = pd.read_csv("https://raw.githubusercontent.com/tirthajyoti/"
"Stats_data_science_ValleyML/master/Notebooks/Data/wine.data.csv")
df.head()df.shape is (178, 14): 178 wines, 14 columnsClass takes the values 1, 2, 3 (59, 71 and 48 wines)Alcohol, Flavanoids, Color intensity, Prolinedf.plot.scatter('Alcohol', 'Color intensity')
plt.show()plt.xlabel and friends still work
df['Alcohol'].plot.hist(bins=20, figsize=(5, 5),
edgecolor='k')
import seaborn as sns
sns.boxplot(x='Class', y='Alcohol', data=df)
sns.violinplot(x='Class', y='Alcohol', data=df)
sns.regplot(x='Alcohol', y='Color intensity',
data=df)
sns.lmplot(x='Alcohol', y='Color intensity',
hue='Class', data=df)hue='Class' colours the dots by class and fits one line per class| Class | Slope |
|---|---|
| 1 | 1.094 |
| 2 | 0.464 |
| 3 | 1.527 |

Part 4
Every pair of columns in one picture
Step 1 of 2
P1, P4, P6, P10: heights 160, 165, 168, 171 and weights 55, 68, 60, 65
| Patient | Deviations | Product | Squares |
|---|---|---|---|
| P1 | -6, -7 | 42 | 36, 49 |
| P4 | -1, 6 | -6 | 1, 36 |
| P6 | 2, -2 | -4 | 4, 4 |
| P10 | 5, 3 | 15 | 25, 9 |
| Sum | 47 | 66, 98 |
Step 2 of 2
corr_mat = np.corrcoef(df, rowvar=False)
corr_mat.shape # (14, 14)
corr_df = pd.DataFrame(corr_mat,
columns=df.columns, index=df.columns)
sns.heatmap(corr_df, linewidth=1,
cmap='plasma')rowvar=False: the variables are the columns
| Question | Answer | r |
|---|---|---|
Most related to Class | Flavanoids | -0.847 |
| Next two | OD280/OD315, Total phenols | -0.788, -0.719 |
| Most related pair | Total phenols and Flavanoids | 0.865 |
| Least related pair | Ash and OD280/OD315 | 0.004 |
| Question | Chart |
|---|---|
| Do two numbers move together? | scatter, regplot |
| How does a number change over time? | line |
| How do named items compare? | bar |
| Question | Chart |
|---|---|
| What shape does one column have? | histogram |
| How do groups differ in spread? | box, violin |
| Which of many columns are related? | heatmap |
plt.style.use stays on for later plotsPractice
About 30 minutes
About 7 minutes
| Bin | Values | Count |
|---|---|---|
| 12 to 20 | 12, 15 | 2 |
| 20 to 28 | 21, 22, 26 | 3 |
| 28 to 36 | 30, 31, 33 | 3 |
| 36 to 44 | 38, 44 | 2 |
About 8 minutes
| Part | Position | Value |
|---|---|---|
| Q1 | 0.25 x 9 = 2.25 | 55 + 0.25 x 2 = 55.5 |
| Median | 0.5 x 9 = 4.5 | 58 + 0.5 x 2 = 59 |
| Q3 | 0.75 x 9 = 6.75 | 61 + 0.75 x 2 = 62.5 |
About 5 minutes
About 10 minutes
sns.boxplot(x='Class', y='Flavanoids', data=df): which class has the highest median? Which boxes show outliers?df['Proline'].plot.hist(bins=10): how many wines in the tallest bin, between which edges? (np.histogram gives exact numbers)sns.regplot(x='Total phenols', y='Flavanoids', data=df): slope sign? r with np.corrcoef?['Class', 'Alcohol', 'Flavanoids', 'Proline'] only: which pair has the largest |r|?| Task | Answer |
|---|---|
| 1 | Medians 2.98, 2.03, 0.685: class 1. Outliers: class 2 (5.08), class 3 (1.57) |
| 2 | 41 wines between 558.4 and 698.6 |
| 3 | Positive: slope 1.380, r = 0.865 |
| 4 | Class and Flavanoids, r = -0.847, the same as in the full heatmap |
x= or hue= splits by groupMahmoud Abas|Data Visualization with Matplotlib and Seaborn