# Scipy ODR

ODR代表正交距離迴歸，用於迴歸研究。 基本線性迴歸通常用於通過在圖上繪製最佳擬合線來估計兩個變量`y``x`之間的關係。

``````import numpy as np
import matplotlib.pyplot as plt
from scipy.odr import *
import random

# Initiate some data, giving some randomness using random.random().
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([i**2 + random.random() for i in x])

# Define a function (quadratic in our case) to fit the data with.
def linear_func(p, x):
m, c = p
return m*x + c

# Create a model for fitting.
linear_model = Model(linear_func)

# Create a RealData object using our initiated data from above.
data = RealData(x, y)

# Set up ODR with the model and data.
odr = ODR(data, linear_model, beta0=[0., 1.])

# Run the regression.
out = odr.run()

# Use the in-built pprint method to give us results.
out.pprint()``````

``````Beta: [ 5.50355382 -3.88825011]
Beta Std Error: [ 0.77904626  2.33231797]
Beta Covariance: [[  1.92223609  -4.80559051]
[ -4.80559051  17.22882877]]
Residual Variance: 0.31573284521355344
Inverse Condition #: 0.1465848083469268
Reason(s) for Halting:
Sum of squares convergence``````
0 條評論，你可以發表評論，我們會進行改進