forked from jcchurch/PythonAndR
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import matplotlib.pyplot as plt | ||
import math | ||
|
||
def linreg(X, Y): | ||
assert len(X)==len(Y) | ||
n = len(X) | ||
EX = sum(X) | ||
EY = sum(Y) | ||
EX2 = sum([x * x for x in X]) | ||
EXY = sum([ X[i] * Y[i] for i in range(n)]) | ||
m = (n * EXY - EX * EY) / (n * EX2 - EX * EX) | ||
b = (EY - m * EX) / float(n) | ||
return (m, b) | ||
|
||
def corr(X, Y): | ||
mux = sum(X)/float(len(X)) | ||
muy = sum(Y)/float(len(Y)) | ||
SUMXX = sum([(x - mux) * (x - mux) for x in X]) | ||
SUMYY = sum([(y - muy) * (y - muy) for y in Y]) | ||
SUMXY = sum([(X[i] - mux) * (Y[i] - muy) for i in range(len(X))]) | ||
return SUMXY/(SUMXX * SUMYY)**0.5 | ||
|
||
def estimatedFit(X, m, b): | ||
return [x * m + b for x in X] | ||
|
||
def bivariate(X, Y): | ||
print "Linear Regression: " | ||
print "Plot color: blue" | ||
(m, b) = linreg(X, Y) | ||
fit = estimatedFit(X, m, b) | ||
r = corr(fit, Y) | ||
print "M: %s" %(m) | ||
print "B: %s" %(b) | ||
print "R-squared: ", r*r | ||
plt.plot(X, fit, "b-") | ||
|
||
# y = (a/x)+b -> 1/X | ||
print "\nInverse Regression: " | ||
print "Plot color: red" | ||
Xp = [1.0/float(x) for x in X] | ||
(m,b) = linreg(Xp, Y) | ||
fit = estimatedFit(Xp, m, b) | ||
r = corr(fit, Y) | ||
print "M: %s" %(m) | ||
print "B: %s" %(b) | ||
print "R-squared: ", r*r | ||
plt.plot(X, fit, "r-") | ||
|
||
# y = x^m + b | ||
print "\nPower Law: " | ||
print "Plot color: green" | ||
Xp = [math.log(x) for x in X] | ||
Yp = [math.log(y) for y in Y] | ||
(m,b) = linreg(Xp, Yp) | ||
fit = estimatedFit(Xp, m, b) | ||
r = corr(fit, Yp) | ||
print "M: %s" %(m) | ||
print "B: %s" %(b) | ||
print "R-squared: ", r*r | ||
plt.plot(X, [math.exp(y) for y in fit], "g-") | ||
|
||
# y = a*sqrt(x)+b -> X*X | ||
print "\nSquare root: " | ||
print "Plot color: magenta" | ||
Xp = [math.sqrt(x) for x in X] | ||
(m,b) = linreg(Xp, Y) | ||
fit = estimatedFit(Xp, m, b) | ||
r = corr(fit, Y) | ||
print "M: %s" %(m) | ||
print "B: %s" %(b) | ||
print "R-squared: ", r*r | ||
plt.plot(X, fit, 'm-') | ||
|
||
# y = a*log(x)+b -> e^X | ||
print "\nLogarithm: " | ||
print "Plot color: cyan" | ||
Xp = [math.log(x) for x in X] | ||
(m,b) = linreg(Xp, Y) | ||
fit = estimatedFit(Xp, m, b) | ||
r = corr(fit, Y) | ||
print "M: %s" %(m) | ||
print "B: %s" %(b) | ||
print "R-squared: ", r*r | ||
plt.plot(X, fit, 'c-') |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import bivariate | ||
import math | ||
|
||
carat = np.loadtxt('diamond.tab', delimiter='\t', skiprows=1,usecols=[0]) | ||
price = np.loadtxt('diamond.tab', delimiter='\t', skiprows=1,usecols=[1]) | ||
|
||
data = zip(carat, price) #sort | ||
data.sort() | ||
(carat, price) = zip(* data) # unzip | ||
bivariate.bivariate(carat,price) | ||
|
||
plt.plot(carat, price, 'k+') | ||
plt.xlabel("Carats") | ||
plt.ylabel("Price") | ||
plt.title("Price of Diamonds by Carat") | ||
plt.show() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
0.17 355 | ||
0.16 328 | ||
0.17 350 | ||
0.18 325 | ||
0.25 642 | ||
0.16 342 | ||
0.15 322 | ||
0.19 485 | ||
0.21 483 | ||
0.15 323 | ||
0.18 462 | ||
0.28 823 | ||
0.16 336 | ||
0.2 498 | ||
0.23 595 | ||
0.29 860 | ||
0.12 223 | ||
0.26 663 | ||
0.25 750 | ||
0.27 720 | ||
0.18 468 | ||
0.16 345 | ||
0.17 352 | ||
0.16 332 | ||
0.17 353 | ||
0.18 438 | ||
0.17 318 | ||
0.18 419 | ||
0.17 346 | ||
0.15 315 | ||
0.17 350 | ||
0.32 918 | ||
0.32 919 | ||
0.15 298 | ||
0.16 339 | ||
0.16 338 | ||
0.23 595 | ||
0.23 553 | ||
0.17 345 | ||
0.33 945 | ||
0.25 655 | ||
0.35 1086 | ||
0.18 443 | ||
0.25 678 | ||
0.25 675 | ||
0.15 287 | ||
0.26 693 | ||
0.15 316 |