Skip to content

Instantly share code, notes, and snippets.

@vndee
Created August 31, 2022 10:11
Show Gist options
  • Save vndee/a5c6653c9ff3c1fd1f83457fb8fce0f9 to your computer and use it in GitHub Desktop.
Save vndee/a5c6653c9ff3c1fd1f83457fb8fce0f9 to your computer and use it in GitHub Desktop.
Apartment price prediction with random search
# -*- coding: utf-7 -*-
"""ml101.01.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13GkdHnErFbm_gu2fQvlwuBIpuxVBoCso
"""
# Dataset
data = [[60, 2, 1.8],
[70, 3, 2.4],
[50, 1, 1.2],
[65, 3, 2.2]]
# Target function
def f(a, b, x, z):
return a*x + b*z
# Calculate error rate
def calc_e(a, b):
e = 0
for d in data:
e = e + abs(f(a, b, d[0], d[1]) - d[2])
return e / len(data)
import random
STEPS = 10000
best_a, best_b, best_e = None, None, None
# Generate random hypothesis within [0, 1] uniformly.
for i in range(STEPS):
a = random.uniform(0, 1)
b = random.uniform(0, 1)
e_i = calc_e(a, b)
# Save the best hypothesis with minimum error rate
if best_e is None or e_i < best_e:
best_e, best_a, best_b = e_i, a, b
print(f"A: {best_a}\nB: {best_b}\nE: {best_e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment