python - 在线性回归中处理 NaN——scipy?
问题描述
我有一个数据集,其中NaN
s 分散在数据中。我正在使用 pandas 从文件中提取数据,并使用 numpy 来处理它。这是我读取数据的代码:
import pandas as pd
import numpy as np
def makeArray(band):
"""
Takes as argument a string as the name of a wavelength band.
Converts the list of magnitudes in that band into a numpy array,
replacing invalid values (where invalid == -999) with NaNs.
Returns the array.
"""
array_name = band + '_mag'
array = np.array(df[array_name])
array[array==-999]=np.nan
return array
# Read data file
fields = ['no', 'NED', 'z', 'obj_type','S_21', 'power', 'SI_flag',
'U_mag', 'B_mag', 'V_mag', 'R_mag', 'K_mag', 'W1_mag',
'W2_mag', 'W3_mag', 'W4_mag', 'L_UV', 'Q', 'flag_uv']
magnitudes = ['U_mag', 'B_mag', 'V_mag', 'R_mag', 'K_mag', 'W1_mag',
'W2_mag', 'W3_mag', 'W4_mag']
df = pd.read_csv('todo.dat', sep = ' ',
names = fields, index_col = False)
# Define axes for processing
redshifts = np.array(df['z'])
y = np.log(makeArray('K'))
mask = np.isnan(y)
我想一个最小的工作示例是:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
randomNumberGenerator = np.random.RandomState(1000)
x = 4 * randomNumberGenerator.rand(100)
y = 4 * x - 1+ randomNumberGenerator.randn(100)
y[50] = np.nan
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
fit = slope*x + intercept
plt.scatter(x, y)
plt.plot(x, fit)
plt.show()
注释掉y[50] = np.nan
MWE 中的行会生成一个漂亮的图表,但包含它会产生与我的实际数据相同的错误消息:
C:\Users\Jeremy\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater
return (self.a < x) & (x < self.b)
C:\Users\Jeremy\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less
return (self.a < x) & (x < self.b)
C:\Users\Jeremy\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1818: RuntimeWarning: invalid value encountered in less_equal
cond2 = cond0 & (x <= self.a)
实际数据框的片段:
no NED z obj_type S_21 power SI_flag U_mag B_mag V_mag R_mag K_mag W1_mag W2_mag W3_mag W4_mag L_UV Q flag_uv
1 SDSSJ000005.95+145310.1 2.499 * 0.0 0.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 0.0 0.0 NONE
4 SDSSJ000009.27+020621.9 1.432 UvS 0.0 0.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 0.0 0.0 NONE
5 SDSSJ000009.38+135618.4 2.239 QSO 0.0 0.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 0.0 0.0 NONE
6 SDSSJ000011.37+150335.7 2.18 * 0.0 0.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 0.0 0.0 NONE
11 SDSSJ000030.64-064100.0 2.606 QSO 0.0 0.0 -999.0 -999.0 -999.0 -999.0 15.46 -999.0 -999.0 -999.0 -999.0 23.342 56.211000000000006 UV
15 SDSSJ000033.05+114049.6 0.73 UvS 0.0 0.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 0.0 0.0 NONE
27 LBQS2358+0038 0.95 QSO 0.0 0.0 17.342 18.483 18.203 17.825 -999.0 -999.0 -999.0 -999.0 -999.0 23.301 56.571999999999996 UV
我正在_mag
针对 绘制每一列z
,并且我正在尝试计算和绘制线性回归,不包括NaN
s。
我已经尝试过numpy.linalg
, numpy.poly
,scipy.stats.linregress
和statsmodels.api
,但似乎它们中的任何一个都不能轻松处理NaN
s。我在 SE 上发现的其他问题正在引导我转圈。
如何在我的数据顶部绘制 OLS 回归拟合,如 MWE 所示?
解决方案
您可以使用df.dropna()
以下链接:pandas.DataFrame.dropna
推荐阅读
- url - “!”的用例是什么 "$" "(" ")" "+" " ," URL 中的字符?
- angular - 如何在不使用 ngFor angular 的情况下通过 json 结果
- ffmpeg - ffmpeg - 用虚拟替换所有损坏的帧
- node.js - 在nodeJS中创建主题时将领导者分配给kafka主题的分区
- r - 随机森林回归:在每棵树的终端节点中提取训练样本
- node.js - 在 posman 表单数据中发布对象数组以及图像字段
- php - phpMyAdmin 无法上传数据库
- java - Camera2 以编程方式录制视频
- sql - SQL Server存储过程将错误的值插入位列
- google-sheets - 一次突出显示多个单元格