【发布时间】:2021-02-11 08:26:10
【问题描述】:
我正在尝试对两个掩码数组进行线性回归。不幸的是,线性回归忽略了掩码并回归了所有变量。我的数据有一些-9999 值,其中我们的仪器没有测量任何数据。这些 -9999 值会产生一条根本不适合数据的线。
我的代码是这样的:
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
x = np.array( [ 2.019, 1.908, 1.902, 1.924, 1.891, 1.882, 1.873, 1.875, 1.904,
1.886, 1.891, 2.0, 1.902, 1.947,2.0280, 1.95, 2.342, 2.029,
2.086, 2.132, 2.365, 2.169, 2.121, 2.192,2.23, -9999, -9999, -9999, -9999,
1.888, 1.882, 2.367 ] ).reshape((-1,1))
y = np.array( [ 0.221, 0.377, 0.367, 0.375, 0.258, 0.16 , 0.2 , 0.811,
0.330, 0.407, 0.421, -9999, 0.605, 0.509, 1.126, 0.821,
0.759, 0.812, 0.686, 0.666, 1.035, 0.436, 0.753, 0.611,
0.657, 0.335, 0.231, 0.185, 0.219, 0.268, 0.332, 0.729 ] )
model = LinearRegression().fit(x, y )
r_sq = model.score( x, y )
print( 'coefficient of determination:', r_sq)
print( 'intercept:', model.intercept_)
print( 'slope:', model.coef_)
x_line = np.linspace (x.min(), x.max(), 11000)
y_line = (model.coef_* x_line) + model.intercept_
fig, ax1 = plt.subplots( figsize = ( 10, 10) )
plt.scatter( x, y )
plt.plot( x_line, y_line )
plt.show()
这给了我们 this scatter plot with the regression plotted。注意:大多数值都在右上角...它们靠得太近而无法区分。
有没有办法在忽略被屏蔽的-9999 值的同时运行回归?
【问题讨论】:
标签: python matplotlib scikit-learn linear-regression