Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 17, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/15ec500401fe623052428f8a679cda30 to your computer and use it in GitHub Desktop.
Save velotiotech/15ec500401fe623052428f8a679cda30 to your computer and use it in GitHub Desktop.
""" Method to read the csv file using Pandas and later use this data for linear regression. """
""" Better run with Python 3+. """
# Library to read csv file effectively
import pandas
import matplotlib.pyplot as plt
import numpy as np
# Method to read the csv file
def load_data(file_name):
column_names = ['area', 'price']
# To read columns
io = pandas.read_csv(file_name,names=column_names, header=None)
x_val = (io.values[1:, 0])
y_val = (io.values[1:, 1])
size_array = len(y_val)
for i in range(size_array):
x_val[i] = float(x_val[i])
y_val[i] = float(y_val[i])
return x_val, y_val
# Call the method for a specific file
x_raw, y_raw = load_data('area-price.csv')
x_raw = x_raw.astype(np.float)
y_raw = y_raw.astype(np.float)
y = y_raw
# Modeling
w, b = 0.1, 0.1
num_epoch = 100
converge_rate = np.zeros([num_epoch , 1], dtype=float)
learning_rate = 1e-3
for e in range(num_epoch):
# Calculate the gradient of the loss function with respect to arguments (model parameters) manually.
y_predicted = w * x_raw + b
grad_w, grad_b = (y_predicted - y).dot(x_raw), (y_predicted - y).sum()
# Update parameters.
w, b = w - learning_rate * grad_w, b - learning_rate * grad_b
converge_rate[e] = np.mean(np.square(y_predicted-y))
print(w, b)
print(f"predicted function f(x) = x * {w} + {b}" )
calculatedprice = (10 * w) + b
print(f"price of plot with area 10 sqmtr = 10 * {w} + {b} = {calculatedprice}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment