在 Numpy 中获取多项式回归统计
Obtaining Polynomial Regression Stats in Numpy
我希望在 Numpy 中使用多项式回归 (polyfit) 来获取系数和截距,但我不确定如何编写脚本来获取多项式函数。
我已经将代码作为线性回归的代码,附在下面:
import matplotlib.pyplot as plt
import sys
from numpy import *
import numpy as np
import numpy.polynomial.polynomial as poly
import pylab
from scipy import stats
from scipy.interpolate import *
from datetime import datetime, timedelta
#Open dataset1,dataset2 data
data1 = np.loadtxt('/home/script/2_columns', delimiter=',', skiprows=0)
data2 = np.loadtxt('/home/script/2_columns_a', delimiter=',', skiprows=0)
#Define first column as dataset1
#Define second column as dataset2
x = data1[:,0]
y = data1[:,1]
#The stuff...
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
您需要统计数据吗?如果没有,你只需要系数,使用 numpy.polyfit
:
真的很简单
import numpy as np
# from your code
data1 = np.loadtxt('/home/script/2_columns', delimiter=',', skiprows=0)
x = data1[:,0]
y = data1[:,1]
degree = 3
coeffs = np.polyfit(x, y, degree)
# now, coeffs is an array which contains the polynomial coefficients
# in ascending order, i.e. x^0, x^1, x^2
intercept, linear, quadratic, cubic = coeffs
如果您确实需要其他值,请说明您需要什么,因为例如 r_value
是 x
和 y
之间的相关系数,这不是很当您不希望数据是线性的时很有用。
我希望在 Numpy 中使用多项式回归 (polyfit) 来获取系数和截距,但我不确定如何编写脚本来获取多项式函数。
我已经将代码作为线性回归的代码,附在下面:
import matplotlib.pyplot as plt
import sys
from numpy import *
import numpy as np
import numpy.polynomial.polynomial as poly
import pylab
from scipy import stats
from scipy.interpolate import *
from datetime import datetime, timedelta
#Open dataset1,dataset2 data
data1 = np.loadtxt('/home/script/2_columns', delimiter=',', skiprows=0)
data2 = np.loadtxt('/home/script/2_columns_a', delimiter=',', skiprows=0)
#Define first column as dataset1
#Define second column as dataset2
x = data1[:,0]
y = data1[:,1]
#The stuff...
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
您需要统计数据吗?如果没有,你只需要系数,使用 numpy.polyfit
:
import numpy as np
# from your code
data1 = np.loadtxt('/home/script/2_columns', delimiter=',', skiprows=0)
x = data1[:,0]
y = data1[:,1]
degree = 3
coeffs = np.polyfit(x, y, degree)
# now, coeffs is an array which contains the polynomial coefficients
# in ascending order, i.e. x^0, x^1, x^2
intercept, linear, quadratic, cubic = coeffs
如果您确实需要其他值,请说明您需要什么,因为例如 r_value
是 x
和 y
之间的相关系数,这不是很当您不希望数据是线性的时很有用。