Skip to content

Instantly share code, notes, and snippets.

@pbowyer
Last active November 9, 2015 14:18
Show Gist options
  • Save pbowyer/2e257c7567d8ac8d0201 to your computer and use it in GitHub Desktop.
Save pbowyer/2e257c7567d8ac8d0201 to your computer and use it in GitHub Desktop.
This gives different results on each platform we test
"""
Please paste your output into a comment at
https://gist.github.com/pbowyer/2e257c7567d8ac8d0201
"""
import scipy # Imported for version number only
from scipy.odr import Model, Data, ODR
from scipy.stats import linregress
import numpy as np
def orthoregress(x, y):
"""Perform an Orthogonal Distance Regression on the given data,
using the same interface as the standard scipy.stats.linregress function.
Arguments:
x: x data
y: y data
Returns:
[m, c, nan, nan, nan]
Uses standard ordinary least squares to estimate the starting parameters
then uses the scipy.odr interface to the ODRPACK Fortran code to do the
orthogonal distance calculations.
"""
linreg = linregress(x, y)
print(linreg)
mod = Model(f)
dat = Data(x, y)
od = ODR(dat, mod, beta0=linreg[0:2])
out = od.run()
return list(out.beta) + [np.nan, np.nan, np.nan]
def f(p, x):
"""Basic linear regression 'model' for use with ODR"""
return (p[0] * x) + p[1]
######################################################
# Start useful debugging code
######################################################
import platform
import sys
def linux_distribution():
try:
return platform.linux_distribution()
except:
return "N/A"
print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_distribution(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
))
print("Numpy version: " + np.__version__)
print("Scipy version: " + scipy.__version__)
print("")
######################################################
# End useful debugging code
######################################################
x = np.arange(5).astype(np.float64)
y = np.array([3.5, 2.7, 6.8, 5.6, 9.7]).astype(np.float64)
res = orthoregress(x, y)
print(res)
@pbowyer
Copy link
Author

pbowyer commented Nov 9, 2015

Another from the community:

Python version: ['2.7.6 (default, Jun 22 2015, 17:58:13) ', '[GCC 4.8.2]']
dist: ('Ubuntu', '14.04', 'trusty')
linux_distribution: ('Ubuntu', '14.04', 'trusty')
system: Linux
machine: x86_64
platform: Linux-3.13.0-65-generic-x86_64-with-Ubuntu-14.04-trusty
uname: ('Linux', 'ropey', '3.13.0-65-generic', '#106-Ubuntu SMP Fri Oct 2 22:08:27 UTC 2015', 'x86_64', 'x86_64')
version: #106-Ubuntu SMP Fri Oct 2 22:08:27 UTC 2015
mac_ver: ('', ('', '', ''), '')
Numpy version: 1.8.2
Scipy version: 0.13.3
(1.5299999999999998, 2.6000000000000005, 0.86825369874211522, 0.056255802911426386, 0.50474415961620245)
[1.9017647664279498, 1.8564703727657534, nan, nan, nan]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment