This tutorial is designed to show the students of PTYS 595B how ti install and play with basic python libraries.
The data for the GOT example is in: https://github.com/leonpalafox/MLClass/tree/master/Chapter1Introduction/data
Python Programming Language.
Packages (Canopy data manager or PIP (https://pip.pypa.io/en/stable/))
import numpy as np #Means that we will import all the functions from the numpy package
import pylab as plt
import pandas as pd
%matplotlib inline #Don't use this command in canopy or in python
a = 1
b = 2
c = a + b
print c
3
m = np.zeros((10,10))
print m
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
m = np.ones((10,10))
print m
[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
t = np.arange(0,3.15,0.1)
print t
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. 3.1]
x =np.sin(2*np.pi*t)
plt.plot(t, x)
[<matplotlib.lines.Line2D at 0xbd84fd0>]
t = np.arange(0,3.15,0.01)
x =np.sin(2*np.pi*t)
plt.plot(t, x)
#plt.show() Use this command in canopy
[<matplotlib.lines.Line2D at 0xbf71278>]
df = pd.read_csv('data/5kings_battles_v1.csv') #windows or mac, be careful \/
df.head()
name | year | battle_number | attacker_king | defender_king | attacker_1 | attacker_2 | attacker_3 | attacker_4 | defender_1 | ... | major_death | major_capture | attacker_size | defender_size | attacker_commander | defender_commander | summer | location | region | note | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Battle of the Golden Tooth | 298 | 1 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Tully | ... | 1.0 | 0.0 | 15000.0 | 4000.0 | Jaime Lannister | Clement Piper, Vance | 1.0 | Golden Tooth | The Westerlands | NaN |
1 | Battle at the Mummer's Ford | 298 | 2 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Baratheon | ... | 1.0 | 0.0 | NaN | 120.0 | Gregor Clegane | Beric Dondarrion | 1.0 | Mummer's Ford | The Riverlands | NaN |
2 | Battle of Riverrun | 298 | 3 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Tully | ... | 0.0 | 1.0 | 15000.0 | 10000.0 | Jaime Lannister, Andros Brax | Edmure Tully, Tytos Blackwood | 1.0 | Riverrun | The Riverlands | NaN |
3 | Battle of the Green Fork | 298 | 4 | Robb Stark | Joffrey/Tommen Baratheon | Stark | NaN | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 18000.0 | 20000.0 | Roose Bolton, Wylis Manderly, Medger Cerwyn, H... | Tywin Lannister, Gregor Clegane, Kevan Lannist... | 1.0 | Green Fork | The Riverlands | NaN |
4 | Battle of the Whispering Wood | 298 | 5 | Robb Stark | Joffrey/Tommen Baratheon | Stark | Tully | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 1875.0 | 6000.0 | Robb Stark, Brynden Tully | Jaime Lannister | 1.0 | Whispering Wood | The Riverlands | NaN |
5 rows × 25 columns
If we want to search only for elements that had two attackers
df['attacker_2']
0 NaN 1 NaN 2 NaN 3 NaN 4 Tully 5 Tully 6 NaN 7 NaN 8 NaN 9 NaN 10 NaN 11 NaN 12 NaN 13 Greyjoy 14 Tully 15 NaN 16 NaN 17 NaN 18 NaN 19 NaN 20 NaN 21 NaN 22 NaN 23 NaN 24 NaN 25 Bolton 26 NaN 27 Thenns 28 NaN 29 NaN 30 Karstark 31 NaN 32 NaN 33 NaN 34 NaN 35 Frey 36 Lannister 37 Karstark Name: attacker_2, dtype: object
df['attacker_2'].dropna()
4 Tully 5 Tully 13 Greyjoy 14 Tully 25 Bolton 27 Thenns 30 Karstark 35 Frey 36 Lannister 37 Karstark Name: attacker_2, dtype: object
pd.notnull(df['attacker_2'])
0 False 1 False 2 False 3 False 4 True 5 True 6 False 7 False 8 False 9 False 10 False 11 False 12 False 13 True 14 True 15 False 16 False 17 False 18 False 19 False 20 False 21 False 22 False 23 False 24 False 25 True 26 False 27 True 28 False 29 False 30 True 31 False 32 False 33 False 34 False 35 True 36 True 37 True Name: attacker_2, dtype: bool
df[pd.notnull(df['attacker_2'])]
name | year | battle_number | attacker_king | defender_king | attacker_1 | attacker_2 | attacker_3 | attacker_4 | defender_1 | ... | major_death | major_capture | attacker_size | defender_size | attacker_commander | defender_commander | summer | location | region | note | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4 | Battle of the Whispering Wood | 298 | 5 | Robb Stark | Joffrey/Tommen Baratheon | Stark | Tully | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 1875.0 | 6000.0 | Robb Stark, Brynden Tully | Jaime Lannister | 1.0 | Whispering Wood | The Riverlands | NaN |
5 | Battle of the Camps | 298 | 6 | Robb Stark | Joffrey/Tommen Baratheon | Stark | Tully | NaN | NaN | Lannister | ... | 0.0 | 0.0 | 6000.0 | 12625.0 | Robb Stark, Tytos Blackwood, Brynden Tully | Lord Andros Brax, Forley Prester | 1.0 | Riverrun | The Riverlands | NaN |
13 | Sack of Winterfell | 299 | 14 | Joffrey/Tommen Baratheon | Robb Stark | Bolton | Greyjoy | NaN | NaN | Stark | ... | 1.0 | 0.0 | 618.0 | 2000.0 | Ramsay Snow, Theon Greyjoy | Rodrik Cassel, Cley Cerwyn, Leobald Tallhart | 1.0 | Winterfell | The North | Since House Bolton betrays the Starks for Hous... |
14 | Battle of Oxcross | 299 | 15 | Robb Stark | Joffrey/Tommen Baratheon | Stark | Tully | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 6000.0 | 10000.0 | Robb Stark, Brynden Tully | Stafford Lannister, Roland Crakehall, Antario ... | 1.0 | Oxcross | The Westerlands | NaN |
25 | The Red Wedding | 299 | 26 | Joffrey/Tommen Baratheon | Robb Stark | Frey | Bolton | NaN | NaN | Stark | ... | 1.0 | 1.0 | 3500.0 | 3500.0 | Walder Frey, Roose Bolton, Walder Rivers | Robb Stark | 1.0 | The Twins | The Riverlands | This observation refers to the battle against ... |
27 | Battle of Castle Black | 300 | 28 | Stannis Baratheon | Mance Rayder | Free folk | Thenns | Giants | NaN | Night's Watch | ... | 1.0 | 1.0 | 100000.0 | 1240.0 | Mance Rayder, Tormund Giantsbane, Harma Dogshe... | Stannis Baratheon, Jon Snow, Donal Noye, Cotte... | 0.0 | Castle Black | Beyond the Wall | NaN |
30 | Retaking of Deepwood Motte | 300 | 31 | Stannis Baratheon | Balon/Euron Greyjoy | Baratheon | Karstark | Mormont | Glover | Greyjoy | ... | 0.0 | 0.0 | 4500.0 | 200.0 | Stannis Baratheon, Alysane Mormot | Asha Greyjoy | 0.0 | Deepwood Motte | The North | NaN |
35 | Siege of Riverrun | 300 | 36 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | Frey | NaN | NaN | Tully | ... | 0.0 | 0.0 | 3000.0 | NaN | Daven Lannister, Ryman Fey, Jaime Lannister | Brynden Tully | 0.0 | Riverrun | The Riverlands | NaN |
36 | Siege of Raventree | 300 | 37 | Joffrey/Tommen Baratheon | Robb Stark | Bracken | Lannister | NaN | NaN | Blackwood | ... | 0.0 | 1.0 | 1500.0 | NaN | Jonos Bracken, Jaime Lannister | Tytos Blackwood | 0.0 | Raventree | The Riverlands | NaN |
37 | Siege of Winterfell | 300 | 38 | Stannis Baratheon | Joffrey/Tommen Baratheon | Baratheon | Karstark | Mormont | Glover | Bolton | ... | NaN | NaN | 5000.0 | 8000.0 | Stannis Baratheon | Roose Bolton | 0.0 | Winterfell | The North | NaN |
10 rows × 25 columns
df.head()
name | year | battle_number | attacker_king | defender_king | attacker_1 | attacker_2 | attacker_3 | attacker_4 | defender_1 | ... | major_death | major_capture | attacker_size | defender_size | attacker_commander | defender_commander | summer | location | region | note | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Battle of the Golden Tooth | 298 | 1 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Tully | ... | 1.0 | 0.0 | 15000.0 | 4000.0 | Jaime Lannister | Clement Piper, Vance | 1.0 | Golden Tooth | The Westerlands | NaN |
1 | Battle at the Mummer's Ford | 298 | 2 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Baratheon | ... | 1.0 | 0.0 | NaN | 120.0 | Gregor Clegane | Beric Dondarrion | 1.0 | Mummer's Ford | The Riverlands | NaN |
2 | Battle of Riverrun | 298 | 3 | Joffrey/Tommen Baratheon | Robb Stark | Lannister | NaN | NaN | NaN | Tully | ... | 0.0 | 1.0 | 15000.0 | 10000.0 | Jaime Lannister, Andros Brax | Edmure Tully, Tytos Blackwood | 1.0 | Riverrun | The Riverlands | NaN |
3 | Battle of the Green Fork | 298 | 4 | Robb Stark | Joffrey/Tommen Baratheon | Stark | NaN | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 18000.0 | 20000.0 | Roose Bolton, Wylis Manderly, Medger Cerwyn, H... | Tywin Lannister, Gregor Clegane, Kevan Lannist... | 1.0 | Green Fork | The Riverlands | NaN |
4 | Battle of the Whispering Wood | 298 | 5 | Robb Stark | Joffrey/Tommen Baratheon | Stark | Tully | NaN | NaN | Lannister | ... | 1.0 | 1.0 | 1875.0 | 6000.0 | Robb Stark, Brynden Tully | Jaime Lannister | 1.0 | Whispering Wood | The Riverlands | NaN |
5 rows × 25 columns
df = df[pd.notnull(df['attacker_2'])]
df['region'].value_counts().plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0xbc18320>