text_plain_output_1.png
from sklearn import linear_model

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('../input/housedata/output.csv')

corr = df.corr()

x = df[['bathrooms', 'bedrooms', 'sqft_above', 'sqft_living']]
y = df['price']
regr = linear_model.LinearRegression()
regr.fit(x.values, y)

predicted = regr.predict([[2, 3, 1680, 2180]])
predicted = regr.predict([[2.75, 4.0, 1400, 2490]])
predicted = regr.predict([[8.0, 7.0, 9410, 13540]])
predicted = regr.predict([[1.0, 0.0, 0.0, 0.0]])
predicted = regr.predict([[0.0, 1.0, 0.0, 0.0]])
predicted = regr.predict([[1.0, 0.0, 1.0, 0.0]])
predicted = regr.predict([[0.0, 0.0, 0.0, 1.0]])

df.isnull().sum()

def function(x, a):
    f = a[2] * x * x + a[1] * x + a[0]
    return f
def grad(x, a):
    g = 2 * a[2] * x + a[1]
    return g
x = df[['bathrooms', 'bedrooms', 'sqft_above', 'sqft_living']]
y = df['price']
f = function(x, y)

x = df[['bedrooms']]
y = df['price']

import numpy as np

def find_theta(X, y):
    m = X.shape[0]
    X = np.append(X, np.ones((m,1)), axis=1)
    theta = np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(X.T, y))
    return theta
def predict(X):
    X = np.append(X, np.ones((x.shape[0],1)), axis=1)
    preds = np.dot(X, theta)
    return preds

theta = find_theta(x, y)
print(theta)

preds = predict(x)
fig = plt.figure()
plt.plot(x, y, 'b.')
plt.plot(x, preds, 'c-')
plt.xlabel('X - Input')
plt.ylabel('y - target/true')

x = df[['bedrooms']]
y = df['price']
regr2 = linear_model.LinearRegression()
regr2.fit(x.values, y)
arr = []
index = []
for i in range(0, 9, 1):
    predicted = regr2.predict([[i]])
    arr.append(predicted[0])
    index.append(i)
fig = plt.figure()
plt.plot(x, y, 'b.')
plt.plot(index, arr, 'c-')