Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created October 24, 2016 16:11
Show Gist options
  • Save rajivnarayana/2189d39f9639fe2a4265a69087da8186 to your computer and use it in GitHub Desktop.
Save rajivnarayana/2189d39f9639fe2a4265a69087da8186 to your computer and use it in GitHub Desktop.
Polynomial Regression using scipy
import csv
datafile = open('home_data.csv', 'r')
datareader = csv.reader(datafile,delimiter=',')
data = []
for row in datareader:
data.append(row)
print data[0]
print data[0][5]
print data[0][3]
print data[0][2]
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn import datasets, linear_model
features = []
label = []
xfeatures = []
data.pop(0)
for row in data:
features.append([float(row[5])/1000, row[3]])
label.append(row[2])
xfeatures.append(float(row[5])/1000)
x = np.array(features)
y = np.array(label)
xf = np.array(xfeatures)
x = np.array(x,dtype=float)
y = np.array(y,dtype=float)
xf = np.array(xf,dtype=float)
poly = PolynomialFeatures(3)
X_ = poly.fit_transform(x)
import statsmodels.formula.api as sm
import scipy, scipy.stats
result = sm.OLS( y, X_ ).fit()
print "Regression summary"
print result.summary()
print "Regression coefficients"
print result.params
print "Regression pvalues"
print result.pvalues
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(xf,y)
print "For linear regression between sq_ft living and price"
print "slope: ", slope
print "intercept: ", intercept
print "p_value: ", p_value
print "r_value: ", r_value
print "std_err: ", std_err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment