title: Python去线性趋势 date: 2021-08-21 23:45:52 tags:
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
import pandas as pd
def f_1(x, A, B):
return A * x + B
data=pd.read_excel('total.xlsx',sheet_name='TSA')
x0 = data.iloc[:,0]
y0 = data.iloc[:,1]
A1, B1 = optimize.curve_fit(f_1, x0, y0)[0]
y1 = A1 * x0 + B1
temp=pd.DataFrame(y0-y1).to_excel('temp.xlsx')
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
import pandas as pd
def f_1(x, A, B):
return A * x + B
data=pd.read_excel('gentoo_and_index_20210821.xlsx')
data['de-penguin_count']=0
site_list=list(data['site_name'].value_counts().index)
for site in site_list:
print(site)
index=data[data['site_name']==site].index
index=list(index)
print(index)
x0 = data.iloc[index,1]
y0 = data.iloc[index,2]
A1, B1 = optimize.curve_fit(f_1, x0, y0)[0]
y1 = A1 * x0 + B1
anomaly=y0-y1
data.iloc[index,-1]=anomaly
data.to_excel('gentoo_detrend_and_index.xlsx')
|