Created
August 7, 2018 15:07
-
-
Save marciok/2836bcaf22485b279e1eb4bedd1fc358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import csv | |
import matplotlib.pyplot as plt | |
# 1. Extract Bitcoin prices and number of Google searches. | |
bitcoin_interest = {} | |
with open('bitcoin-interest.csv') as f: | |
reader = csv.reader(f) | |
for row in reader: | |
bitcoin_interest[row[0]] = row[1] | |
price_and_interest = [] | |
with open('market-price.csv') as f: | |
reader = csv.reader(f) | |
for row in reader: | |
# 2. Match the price with search date. | |
if bitcoin_interest.get(row[0], None): | |
price_and_interest.append([int(bitcoin_interest.get(row[0])), float(row[1])]) | |
def calculate_error(points, m, b): | |
# Error is calculated by the average distance from the points to the line | |
error = 0 | |
for i in range(0, len(points)): | |
x = points[i, 0] | |
y = points[i, 1] | |
# Moving y to the other side of the equation | |
# y = mx + b -> = mx + b - y | |
error += (y - (m*x + b))**2 | |
# Calculating average | |
return error / float(len(points)) | |
def gradient_descent(points, b, m, learning_rate): | |
m_gradient = 0 | |
b_gradient = 0 | |
N = float(len(points)) | |
for i in range(0, len(points)): | |
x = points[i, 0] | |
y = points[i, 1] | |
b_gradient += -(2/N) * (y - ((m * x) + b)) | |
m_gradient += -(2/N) * x * (y - ((m * x) + b)) | |
m_updated = m - (m_gradient * learning_rate) | |
b_updated = b - (b_gradient * learning_rate) | |
return b_updated, m_updated | |
# 3.Set our hyper paremeters: epoch, learning rate, m and b. | |
learning_rate = 0.0001 | |
epochs = 1000 | |
start_m = 0 | |
start_b = 0 | |
for i in range(epochs): | |
# 4. Calculate the error so we can see our algorirthm learning. | |
error = calculate_error(data, start_m, start_b) | |
if (i % 100): | |
print("Error rate: " + str(error)) | |
# 5. Calculate the gradient: | |
start_m, start_b = gradient_descent(data, start_m, start_b, learning_rate) | |
print("Final m: " + str(start_m)) | |
print("Final b: " + str(start_b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment