Skip to content

Instantly share code, notes, and snippets.

View BenjaminFraser's full-sized avatar

Benjamin Fraser BenjaminFraser

View GitHub Profile
@BenjaminFraser
BenjaminFraser / activation_examples.ipynb
Created September 28, 2021 07:21
Activation_examples.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@BenjaminFraser
BenjaminFraser / random_forest.py
Last active April 12, 2020 13:45
Python implementation of a random forest model using Numpy
class RandomForest():
""" Python implementation of a random forest regressor """
def __init__(self, x, y, num_trees, sample_size, feature_proportion=1.0,
min_leaf=5, bootstrap=False, random_seed=12):
np.random.seed(random_seed)
self.x = x
self.y = y
self.num_trees = num_trees
self.sample_size = sample_size
self.feature_proportion = feature_proportion
@BenjaminFraser
BenjaminFraser / decision_tree.py
Last active August 17, 2022 15:21
Python implementation of a Decision Tree using numpy.
class DecisionTree():
""" Form a basic decision tree """
def __init__(self, x, y, idxs=None, oob_idxs=None,
min_leaf=5, feat_proportion=1.0):
if idxs is None:
idxs = np.arange(len(y))
self.x = x
self.y = y
self.idxs = idxs
self.oob_idxs = oob_idxs
@BenjaminFraser
BenjaminFraser / web_scrape_example_1.py
Last active January 4, 2020 10:42
Example of web scraping the URLs of applicable news articles in order to form a dataset for NLP.
from bs4 import BeautifulSoup
import requests
from time import sleep
# select Guardian website - Military news and obtain a set of URLs
crawl_url = 'https://www.theguardian.com/uk/military?page='
# form a set to store unique urls of all articles
guardian_urls = set()
@BenjaminFraser
BenjaminFraser / vadar_sentiment.py
Last active January 3, 2020 17:43
An example of using NLTK VADAR sentiment analyser to perform sentiment analysis on a Pandas dataframe.
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sent_i = SentimentIntensityAnalyzer()
def vadar_sentiment(text):
""" Calculate and return the nltk vadar (lexicon method) sentiment """
return sent_i.polarity_scores(text)['compound']
# create new column for vadar compound sentiment score
news_sentiments['vadar compound'] = news_sentiments['title'].apply(vadar_sentiment)
@BenjaminFraser
BenjaminFraser / Adaline_classifier.py
Created June 30, 2018 15:54
An Adaline binary classifier to illustrate the workings of basic neural units for the blog post.
class Adaline(object):
""" Adaline (Adaptive Linear Neuron) for binary classification.
Minimises the cost function using gradient descent. """
def __init__(self, learn_rate = 0.01, iterations = 100):
self.learn_rate = learn_rate
self.iterations = iterations
def fit(self, X, y, biased_X = False, standardised_X = False):
@BenjaminFraser
BenjaminFraser / Perceptron_classifier.py
Last active June 30, 2018 11:09
A Python perceptron classifier used to illustrate the concepts of basic binary classifiers.
class Perceptron(object):
""" Perceptron for demonstrating a binary classifier """
def __init__(self, learn_rate = 0.01, iterations = 100):
self.learn_rate = learn_rate
self.iterations = iterations
def fit(self, X, y, biased_X = False):
""" Fit training data to our model """
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# make up random data for bird wingspans and weights for golden eagles and horned owls
bird_wingspans = np.concatenate([np.random.randint(170, 230, size = 50)/100.0,
np.random.randint(60, 100, size = 50)/100.0])
bird_weights = np.concatenate([(11 - 10)*np.random.randn(50)+10, np.abs(np.random.randn(50))+1])
# combine X vectors into a 2-dimensional array with 100 rows and 2 columns
@BenjaminFraser
BenjaminFraser / get_dataset.py
Last active March 24, 2020 19:14 — forked from ardamavi/get_dataset.py
For reading datasets and converting to numpy files.
# Arda Mavi
import os
import numpy as np
from os import listdir
from scipy.misc import imread, imresize
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
# Settings:
img_size = 64
@BenjaminFraser
BenjaminFraser / nRF24l01_slave_ackpayload_multiple.cpp
Created June 27, 2017 21:33
A slave device program that can be installed on up to 6 remote slave devices using the nRF24l01+ transceiver and TMRh20 RF24 library.
/*************************************************************************
* Remote node - nRF24L01+ radio communications *
* A program to operate a remote-node slave device that sends *
* data to a command unit on a given request message. The radio *
* transceiver used is the nRF24L01+, and it operates using the *
* TMRh20 RF24 library. *
* *
* Author: B.D. Fraser *
* *
* Last modified: 27/06/2017 *