Skip to content

Commit

Permalink
add restructured fat_learn
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed May 11, 2023
1 parent f64bf3f commit ec20d79
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 015-machine-learning/fat_learn/.github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Upload Python Package

on:
tags:
- 'v*'

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Set up Docker Context for Buildx
id: buildx-context
run: |
docker context create builders
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
version: latest
endpoint: builders
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: |
python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: build/wheels/
4 changes: 4 additions & 0 deletions 015-machine-learning/fat_learn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
*env*
output.html
*.egg-info
16 changes: 16 additions & 0 deletions 015-machine-learning/fat_learn/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
All code is MIT licensed.

Belfast City Council's dataset of car parks is included under the UK Open Government License: http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/
With thanks to OpenDataNI for supplying this dataset: https://opendatani.gov.uk


MIT LICENSE (for code)
===========

Copyright 2016 Phil Weir

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions 015-machine-learning/fat_learn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a companion repository to the

https://github.com/flaxandteal/python-course

Python course.
31 changes: 31 additions & 0 deletions 015-machine-learning/fat_learn/data/car-park-locations-data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"NAME","ADDRESS","POSTCODE","NON-DESIGNATED BAYS","X CO-ORDINATE","Y CO-ORDINATE","LATITUDE","LONGITUDE","BLUE BADGE BAYS","FAMILY FRIENDLY BAYS","E-CAR BAYS","TARIFF","CHARGED HOURS"
"Ashdale Street Car Park","Ashdale Street - Off Bloomfield Ave.","BT5 5AX","87","336410","374115","54.596667","-5.890236","","","","TL3","Mon- Sat 0800-1800"
"Bankmore Street Car Park","Bankmore Street/Maryville Street","BT7 1AQ","46","333872","373522","54.592300","-5.929871","","","","TM12","Mon- Sat 0800-1800"
"Charlotte Street Car Park","Ormeau Avenue","BT7 1BG","90","334066","373514","54.592281","-5.926693","","","","TM7","Mon- Sat 0800-1800"
"Corporation Square Car Park","Corporation Sqaure/Tomb Street","BT1 3AJ","198","334270","374888","54.604340","-5.922644","","","","TL4","Mon- Sat 0800-1800"
"Corporation Street Car Park","Corporation Street","BT1 3DG","124","334214","374958","54.605046","-5.923358","","","","TL4","Mon- Sat 0800-1800"
"Cromac Street Car Park","Cromac Street","BT2 8JN","99","334232","373792","54.593924","-5.924265","1","1","2","TL6","Mon- Sat 0800-1800"
"Dunbar Street Car Park","Talbot Street/Dunbar Street","BT2 1LH","32","334090","374713","54.603248","-5.925944","2","1","","TM10","Mon- Sat 0800-1800"
"Dundela Crescent Car Park","Dundela Crescent","BT4 3BN ","26","337106","374514","54.600309","-5.878777","","","","Free","Free"
"Exchange Street Car Park","Exchange Street off Corporation Street","BT1 2LJ","46","334135","374749","54.600396","-5.925242","2","1","","TM8","Mon- Sat 0800-1800"
"Frederick Street Car Park","Frederick Street","BT1 2LW","134","333821","375030","54.605616","-5.930339","1","","","TL4","Mon- Sat 0800-1800"
"Glenburn Road Car Park","Glenburn Road-Dunmurry","BT17 9AG","123","329158","368703","54.550223","-6.005425","3","","2","Free","Free"
"Grampian Avenue Car Park","Grampian Avenue","BT4 3AG","41","336658","374237","54.598169","-5.886109","1","1","","Free","Free"
"Hope Street North Car Park","Hope Street","BT12 5EE","41","333469","373666","54.593413","-5.935841","","","2","TM12","Mon- Sat 0800-2100"
"Kent Street Car Park","Kent Street","BT1 2JA","66","333547","374736","54.603463","-5.933922","2","","","TM10","Mon- Sat 0800-1800"
"Little Donegall Street Car Park","Little Donegal Street/Stephen Street","BT1 2JD","105","333607","374853","54.604610","-5.932752","2","2","2","TL6","Mon- Sat 0800-1800"
"Little Victoria Street Car Park","Little Victoria Street/Bruce Street","BT2 7JH","84","333650","373570","54.592701","-5.933385","2","1","2","TM12","Mon- Sat 0800-2100"
"Marlborough Avenue Car Park","Marlborough Avenue","BT9 7HB","31","332265","371684","54.575889","-5.955479","2","2","","TL2","Mon- Sat 0800-1800"
"Northumberland Street Car Park","Northumberland Street/Kendal Street","BT13 2JF","87","332750","374714","54.602989","-5.946541","2","2","","Free","Free"
"Parkgate Avenue Car Park","Parkgate Avenue","BT4 1JA","36","336658","374375","54.599127","-5.886814","1","1","","Free","Free"
"Ravenscroft Avenue Car Park","Ravenscroft Avenue","BT5 5BA","53","336608","374148","54.597197","-5.887805","3","2","","Free","Free"
"Sandown Road Car Park","Sandown Road","BT5 6GT","65","337804","373839","54.594108","-5.868433","2","2","","Free","Free"
"Shaw's Bridge Car Park","Malone Road/Shaws Bridge","BT9 5YN","57","332379","369116","54.553536","-5.954924","4","3","","Free","Free"
"Smithfield Car Park","Wine Tavern Street","BT1 1JQ","107","333543","374569","54.601901","-5.933594","2","","","TM12","Mon- Sat 0800-1800 Thurs until 2100"
"Station Street Car Park","Station Street","BT5 1AS","114","334561","374431","54.600366","-5.918011","","","","TL6","Mon- Sat 0800-1800"
"Wandsworth Road Car Park","Wandsworth Road","BT4 3LS","24","338755","373976","54.595118","-5.854048","","","","Free","Free"
"Westminster Avenue (East) Car Park","Westminster Avenue ","BT4 1QQ","10","336522","374275","54.598300","-5.888232","","","","Free","Free"
"Westminster Avenue (North) Car Park","Westminster Avenue North","BT4 1QQ","70","336460","374286","54.598303","-5.889029","","","","Free","Free"
"Westminster Avenue (West) Car Park","Westminster Avenue ","BT4 1QQ","10","336543","374286","54.598300","-5.888165","","","","Free","Free"
"Whitla Street Car Park","Whitla Street","BT15 1JP","50","334266","375596","54.611024","-5.922635","","","","Free","Free"
"Lancaster Street - York Street","York Street","BT15 1AS","53","333947","375082","54.606033","-5.927695","","","","TL4","Mon- Sat 0800-1800"
150 changes: 150 additions & 0 deletions 015-machine-learning/fat_learn/data/iris.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica
1 change: 1 addition & 0 deletions 015-machine-learning/fat_learn/fat_learn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
56 changes: 56 additions & 0 deletions 015-machine-learning/fat_learn/fat_learn/carpark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Find the closest carparks to the Computer Science building
@author: Phil Weir <[email protected]>
@license: MIT
"""

import pandas
from sklearn.neighbors import NearestNeighbors

from . import utils

def closest_carpark(gdf, target):
X = []
def extract_points(row):
X.append([row.geometry.y, row.geometry.x])

gdf.apply(extract_points, axis=1)
nbrs = NearestNeighbors(n_neighbors=4).fit(X)

Y = [target]
# This maps multiple targets at once efficiently, but we just need one
distances_per_Y_row, row_numbers_per_Y_row = nbrs.kneighbors(Y)

distances = distances_per_Y_row[0]
row_numbers = row_numbers_per_Y_row[0]

# Report distance as a comparison
reference_distance = distances[0]
# Why are there two zeroes below? What happens if Y has multiple entries?
for distance, row_i in zip(distances, row_numbers):
relative_distance = 100 * (distance / reference_distance - 1)
row = gdf.iloc[row_i]
print(row['NAME'], " is ", relative_distance, "percent further than the closest")


def run():
# Load data into variable called "df"
df = pandas.read_csv('car-park-locations-data.csv', encoding='latin1')

# Because why not?
# print(df.describe())

# Get a geopandas dataframe, from a normal pandas one
# gdf = utils.to_geo_dataframe(df)

# Create a plot
# utils.plot_gdf("output.html", gdf)

# CompSci Building 54.5817428 -5.9374874
closest_carpark(gdf, [54.5817428, -5.9374874])

if __name__ == "__main__":
run()
78 changes: 78 additions & 0 deletions 015-machine-learning/fat_learn/fat_learn/iris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test learning algorithms on the classic Iris dataset.
@author: Phil Weir <[email protected]>
@license: MIT
"""

import os
import numpy as np
import pandas

from sklearn import svm

from sklearn import datasets
from pandas.plotting import scatter_matrix

def run():
# Why is this if-statement here? Why don't we just use lines 21-22?
if not os.path.exists('iris.data'):
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
df = pandas.read_csv(url, header=None)
df.to_csv('iris.data', header=False, index=False)

df = pandas.read_csv('./iris.data', header=None)

# When you are happy with what this looks like, remove it!
print(df)

# Why is this important?
reordering = np.random.permutation(df.index)
df = df.reindex(reordering)

# This is the set of inputs
X_training = df.iloc[:-30, :-1]
# This is the known classifiers
y_training_target = df.iloc[:-30, -1]

# This set we will use for testing the model
X_testing = df.iloc[-30:, :-1]
# We will measure our predictions against these
y_testing_target = df.iloc[-30:, -1]

# Make a model
classifier = svm.SVC(kernel='linear', C=1).fit(X_training, y_training_target)

# Evaluate it
score = classifier.score(X_testing, y_testing_target)
print(score)

# What type of flower is an iris with:
# Sepal length: 4.7cm
# Sepal width: 3.4cm
# Petal length: 1.1cm
# Petal width: 0.2cm
# ?
samples = [
[4.7, 3.4, 1.1, 0.2]
]
print(classifier.predict(samples))

# TASKS
#------
# 0 - What, in words, is each slicing operation doing?
#
# 1 - Download the CSV and open it from the same folder as script (still with read_csv)

# 2 - Add plots of sepal length, sepal width, petal length and petal width using Pandas'
# - scatter_matrix (hint: create a dataframe with those four columns)
# - RadViz (familiar looking dataset on the Pandas manual page!)

# 3 - Switch to using "test_train_split" instead of using "-30" and slicing

# 4 - Instead of using "score", use "cross_val_score": http://scikit-learn.org/stable/modules/cross_validation.html#cross-validation

if __name__ == "__main__":
run()
Loading

0 comments on commit ec20d79

Please sign in to comment.