【项目】斯里兰卡ESV计算

按照某一指标进行排序

1
yield_c[yield_c["Year"]>2015].groupby('Item').mean()

torch存储和读取

1
2
3
4
torch.save(model.state_dict(),path) # 存储

checkpoint=torch.load(path)
model.load_state_dict(checkpoint) # 加载参数

Pyechart制图参数

1
2
l.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
l.set_global_opts(title_opts=opts.TitleOpts(title='1D CNN'))

Series转列表

1
pp_c[pp_c['Item']=="Rice"]['Value'].tolist()

添加行数据

1
pp_c=pp_c.append({"Unnamed: 0":91+i,"Year":2006+i,"Value":l[5+i],"Item":"Sorghum",'Unit':"卢比/kg"},ignore_index=True)

注意需要列对应上,并且需要忽略索引,这里以字典形式做匹配。

判断数据类型

1
2
3
for i in e.columns:
if e[i].dtype==np.float64:
e[i]*=16111

关于enumerate

1
2
3
4
5
6
7
8
9
def func(tb,path):
newT=defaultdict(int)
for i in range(len(tb)):
if (k:=mapTable[tb["Value"][i]]) in v["Unnamed: 0"].unique(): # unique()才能用 in,否则数据类型不是list
_=tb['Count'][i]/100
for n,g in enumerate(v.columns[2:].tolist(),2): # enumerate(list,idx) idx表示起始下标
newT[g]+=_/1e10*v[v["Unnamed: 0"]==k].iloc[0,n]
s=pd.DataFrame(newT,range(1))
s.to_csv(path)

很酷的运算1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AT=[]
tab=os.listdir(path)
tables=[i for i in tab if i.endswith("t.csv")]
# ['2017t.csv', '2018t.csv', '2019t.csv', '2020t.csv', '2021t.csv', '2022t.csv']
for i in range(len(tables)):
newT=defaultdict(int)
year=tables[i].split('.')[0]
tb=pd.read_csv(os.path.join(path,tables[i]))
for i in range(len(tb)):
if (k:=mapTable[tb["Value"][i]]) in v["Unnamed: 0"].unique():
_=tb['Count'][i]/100
for n,g in enumerate(v.columns[2:].tolist(),2):
# 每一项都等于 每种类型的像元乘以该像元在该项之下的价值之和
newT[g]+=_/1e10*v[v["Unnamed: 0"]==k].iloc[0,n]
AT.append(newT)
d=pd.DataFrame(AT,index=["%s"%i for i in range(2017,2023)])

很酷的运算2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def getValue(year):
def f(tem):
Sum=0
for i in range(len(tem)):
# 若映射存在
if(k:=mapTable[tem['Value'][i]]) in v['Unnamed: 0'].unique():
_=tem['Count'][i]/100 # 换算成公顷
Sum+=v[v['Unnamed: 0']==k].iloc[0,1:].sum()*_ # 总价值=\sum c_i*v_i
return Sum
dicT=defaultdict(int)
tabs=os.listdir(path+r"\%sTabs"%year)
total_esv=0
for i in tabs:
tb=pd.read_excel(path+r"\%sTabs\\"%year+i)
total_esv+=(k:=f(tb))
dicT[i.split("_")[0]]=k/1e10
return total_esv/1e10,dicT

Stable=[getValue(i) for i in range(2017,2023)]
DataShow=pd.DataFrame([],columns=[str(i) for i in range(2017,2023)])
# 实际上,DF可以直接传入一个Dict,Key将作为Index,value将作为列值
for i in range(2017,2023):DataShow[str(i)]=Stable[i-2017][1]

关于PyEchart图层的叠加

1
a.overlap(l).render_notebook()

关于在训练过程中的新形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class MyDataSet(object):
def __init__(self,seq,ws=6):
# ws是滑动窗口大小
self.ori=[i for i in seq[:ws]]
self.label=[i for i in seq[ws:]]
self.reset()
self.ws=ws

def set(self,dpi):
# 添加数据
self.x.append(dpi)

def reset(self):
# 初始化
self.x=self.ori[:]

def get(self,idx):
return self.x[idx:idx+self.ws],self.label[idx]

def __len__(self):
return len(self.x)

# 数据迭代器不一定非得集成自DataLoader


for i in range(len(x)-ws):
# 每次更新参数前都梯度归零和初始化
seq,y_train=train_data.get(i) # 从我们的数据集中拿出数据
seq,y_train=torch.FloatTensor(seq),torch.FloatTensor([y_train])
seq=seq.unsqueeze(dim=0)
seq,y_train=seq.to(device),y_train.to(device)

optimizer.zero_grad()
# 注意这里要对样本进行reshape,
# 转换成conv1d的input size(batch size, channel, series length)
y_pred = model(seq)
loss = criterion(y_pred, y_train)
loss.backward()
train_data.set(y_pred.to("cpu").item()) # 再放入预测数据

# 需要注意的是,一定要先构建计算图处理梯度后再放入,要不会出bug


获取DataFrame中的数值

1
np.matmul(data.values,np.array(EWM(data)).T)