# 2022 华为杯数模研赛E题原创python代码

import xlrd
import decimal
import pandas as pd

# 获取植被生物量数据

f_name = ‘fujian15.xlsx’
data = xlrd.open_workbook(f_name)
table = data.sheets()[0]

# 放牧规模

S = [str(table.cell_value(i, 0)) for i in range(1, table.nrows)]

# 日期

date = [str(table.cell_value(i, 1)) for i in range(1, table.nrows)]

# 植被生物量

W = [str(table.cell_value(i, 3)) for i in range(1, table.nrows)]

s0 = S[0]
d0 = date[0]
w = 0
w_dir = []

for i in range(len(S)):
s = S[i]
d = date[i]
if s == s0 and d == d0:
w += float(W[i])
else:
w_dir.append({‘s’: str(s0), ‘d’: str(d0), ‘w’: w})
w = 0
s0 = s
d0 = d

# 本代码用的回归模型是随机森林

import xlrd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
import warnings
warnings.filterwarnings(‘ignore’)

f_name = ‘q2.xlsx’
data = xlrd.open_workbook(f_name)
table = data.sheets()[0]
df = pd.DataFrame(columns=[‘10cm湿度’, ‘40cm湿度’, ‘100cm湿度’, ‘200cm湿度’]) # 存储最终结果

for mon in [0, 1, 2]: # 1~3月

# 蒸发量、降水量

x1 = [table.cell_value(i, 2) for i in range(mon * 11 + 1, mon * 11 + 12)]
x2 = [table.cell_value(i, 3) for i in range(mon * 11 + 1, mon * 11 + 12)]
X = np.array([x1, x2]).transpose()

# 不同深度的湿度

y1 = np.array([table.cell_value(i, 4) for i in range(mon * 11 + 1, mon * 11 + 12)]).flatten()
y2 = np.array([table.cell_value(i, 5) for i in range(mon * 11 + 1, mon * 11 + 12)]).flatten()
y3 = np.array([table.cell_value(i, 6) for i in range(mon * 11 + 1, mon * 11 + 12)]).flatten()
y4 = np.array([table.cell_value(i, 7) for i in range(mon * 11 + 1, mon * 11 + 12)]).flatten()

# 时间

t = np.array(range(len(x1))).reshape(-1, 1)
t_test = np.array(range(12)).reshape(-1, 1)

# 利用随机森林进行预测

regressor1 = RandomForestRegressor()
regressor1.fit(t, x1) # 蒸发量与时间关系模型
regressor2 = RandomForestRegressor()
regressor2.fit(t, x2) # 降水量与时间关系模型
regressor3 = RandomForestRegressor()
regressor3.fit(X, y1) # 10cm湿度预测模型
regressor4 = RandomForestRegressor()
regressor4.fit(X, y2) # 40cm湿度预测模型
regressor5 = RandomForestRegressor()
regressor5.fit(X, y3) # 100cm湿度预测模型
regressor6 = RandomForestRegressor()
regressor6.fit(X, y4) # 200cm湿度预测模型

# 预测未来蒸发量与降水量

import xlrd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
import warnings
warnings.filterwarnings(‘ignore’)

f_name = ‘q3.xlsx’
data = xlrd.open_workbook(f_name)
table = data.sheets()[0]
df = pd.DataFrame(columns=[‘plot’, ‘SOC’, ‘SIC’, ‘STC’, ‘N’, ‘C/N’]) # 存储最终结果

plots = np.array([table.cell_value(i, 1) for i in range(1, table.nrows)]).flatten()
plot = []
[plot.append(i) for i in plots if i not in plot]

# print(plot)

for y in range(12): # 12个分区

# 时间

t = np.array(range(11)).reshape(-1, 1)

THE END