Created
October 24, 2016 16:11
-
-
Save rajivnarayana/2189d39f9639fe2a4265a69087da8186 to your computer and use it in GitHub Desktop.
Polynomial Regression using scipy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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