Skip to content

Instantly share code, notes, and snippets.

%matplotlib inline
import matplotlib.pyplot as plt
# calculate the cost at -5
def f(w1):
return sum((w1*data['speed'] - data['dist'])**2)
w1 = -5
h = 0.1
x_tan = np.linspace(-10, 0, 15)
%matplotlib inline
import matplotlib.pyplot as plt
# calculate the cost for many different values
# of w1 using our cost function
costs = []
for i in range(-10,11,1):
w1 = i
y_actual = data['dist']
y_predict = w1*data['speed']
costs.append(np.mean((y_predict - y_actual)**2))
@yvan
yvan / calc_reg.py
Last active October 28, 2017 16:26
# performs the gradient descent for linear regression
def calc_regression_simple(w1, frames, x_data, y_data):
learn_rate = 0.001
ys = []
for i in range(frames):
# get the gradient and update the parameter
w1_gradient = 2*np.mean(x_data*(w1*x_data - y_data))
w1 = w1 - (w1_gradient*learn_rate)
# calculate the predictions from this new function
x = np.linspace(0,30)
import pandas as pd
# load the data
data = pd.read_csv('cars.csv')
data.head()
@yvan
yvan / tangent.py
Last active October 28, 2017 17:13
%matplotlib inline
import matplotlib.pyplot as plt
# calculate the cost at -5
def f(w1):
return np.mean((w1*data['speed'] - data['dist'])**2)
w1 = -5
h = 0.1
x_tan = np.linspace(-10, 0, 15)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
# set ffmpeg path to installed path (for anmation)
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
# set ffmpeg path to installed path (for anmation)
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
import random
import numpy as np
def display_cave(matrix):
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
char = "#" if matrix[i][j] == WALL else "."
print(char, end='')
print()
import random
import numpy as np
def display_cave(matrix):
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
char = "#" if matrix[i][j] == WALL else "."
print(char, end='')
print()
shape = (42,42)
WALL = 0
FLOOR = 1
# set the probability of filling
# a wall at 40% not 50%
fill_prob = 0.4
new_map = np.ones(shape)
for i in range(shape[0]):