【发布时间】:2021-11-02 07:19:14
【问题描述】:
我正在尝试在 python 中执行减法。在 excel 中执行时这是一项简单的任务,但我想在 jupyter notebook 中执行此操作。
下面是我的代码:
import pandas as pd
from sklearn import linear_model
import numpy as np
#Read X1 anomaly
X1= pd.read_csv (r'file\X1.csv')
X1 = pd.DataFrame(X1,columns=['Year','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'])
X1= X1[X1['Year'].between(1984,2020, inclusive="both")]
#X1 = X1["Mar"].describe()
#print (X1)
#Read X2 anomaly
X2= pd.read_csv (r'file\X2.csv')
X2 = pd.DataFrame(X2,columns=['Year','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'])
X2= X2[X2['Year'].between(1984,2020, inclusive="both")]
#X2 = X2["Mar"].describe()
#print (X2)
X1 = X1["Mar"]
X2 = X2["Mar"]
#### my goal is to remove transform X2 by removing their line of fit
regr = linear_model.LinearRegression()
regr.fit(X1.values.reshape(-1,1), X2)
Trend=regr.coef_*X1+regr.intercept_
X3=np.subtract(X2,Trend)
print (X3)
这里是数据link。我想消除 X1 和 X2 之间的线性关系,所以我对 X1 和 X2 进行了回归,然后我想从 X2 中减去趋势线使其成为 X3。但是,X3 中有很多 NaN。请帮我看看我应该怎么做。
【问题讨论】:
标签: numpy jupyter-notebook regression subtraction