## 基于神经网络的降雨预测

? ## ? 基于神经网络的降雨预测

python代码：

``````import numpy as np
import pandas as pd
import sklearn

filename = r'tianjin.xlsx'
pd.set_option('display.max_columns',None)

# data = data.drop('ET0',axis=1)#去除数据集中的ET0
len(data)

len(data.columns)
print(len(data.columns))
data = data.drop(['PMax','ET0', 'Rn', 'Wind', 'PMean', 'TMean', 'Delta','Gama'],axis = 1)#去除不需要的参数
print(data)
#data = data.drop(['PMax', 'Wind', 'PMin', 'TMax', 'TMin', 'PMean', 'Rs', 'Rn', 'Delta','Gama'],axis = 1)
len(data.columns)
ex = data.Date[10]
# print(len(data.Data[10])
# print(data)
def func_change_data_type(row):#修改数据的时间类型
row = pd.to_datetime(row)
return row
func_change_data_type(ex)>pd.to_datetime('19700101')
data['Date'] = data['Date'].apply(func_change_data_type)

#保留近五十年的数据
data = data[data['Date']>pd.to_datetime('19691231')]
len(data)
data['y'] = data.Rain
data = data.drop('Rain',axis =1)

data_copy = data.copy()
len(data)

columns = data_copy.columns.tolist()
columns.pop(0)

columns

for i in columns:#数据中不存在空值
if data[i].isnull().any():
print(i)

#处理异常数据
for i in columns:
if (data_copy[i]>3000).any():
data_copy = data_copy[data_copy[i]<=3000]
len(data_copy)

#训练集验证机测试集划分
data_train = data_copy[data_copy.Date<pd.to_datetime('20000101')].drop('Date',axis =1)
data_train_y = data_copy[data_copy.Date<pd.to_datetime('20000101')].y

data_s = data_copy[data_copy.Date<pd.to_datetime('20100101')]
data_validation = data_s[data_s.Date>pd.to_datetime('19991231')].drop('Date',axis=1)
data_validation_y = data_s[data_s.Date>pd.to_datetime('19991231')].y

data_test = data_copy[data_copy.Date>pd.to_datetime('20091231')].drop('Date',axis=1)
data_test_y = data_copy[data_copy.Date>pd.to_datetime('20091231')].y

len(data_train)+len(data_validation) + len(data_test) == len(data_copy)

from sklearn.neural_network import MLPRegressor
len(data_train.columns)

from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing
scaler_train = preprocessing.StandardScaler().fit(data_train)
data_train = scaler_train.transform(data_train)
train_x = data_train[:,:15]
train_y = data_train[:,-1]
#验证集标准化
scaler_valid = preprocessing.StandardScaler().fit(data_validation)
data_validation = scaler_valid.transform(data_validation)
valid_x = data_validation[:,:15]
valid_y = data_validation[:,-1]
#测试集标准化
scaler_test = preprocessing.StandardScaler().fit(data_test)
data_test = scaler_test.transform(data_test)
test_x = data_test[:,:15]
test_y = data_test[:,-1]
print("训练集的规模：{0}".format(train_x.shape,train_y.shape))
print("验证集的规模：{0}".format(valid_x.shape,valid_y.shape))
print("测试集的规模：{0}".format(test_x.shape,test_y.shape))

from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
model_mlp = MLPRegressor(
learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=5000, shuffle=True,
random_state=1, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True,
early_stopping=False,beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model_mlp.fit(train_x, train_y)

result = model_mlp.predict(train_x)
#mlp_score=model_mlp.score(x1,train_y)
#print('sklearn多层感知器-回归模型得分: ',mlp_score)#预测正确/总数
pred = model_mlp.predict(valid_x)
print('MSE是：{0:.2f}'.format(mean_squared_error(valid_y,pred)*100))
print('MAE是：{0:.2f}'.format(mean_absolute_error(valid_y,pred)*100))
print('R^2是：{0:.2f}%'.format(r2_score(valid_y,pred)*100))
#x2 = valid_x.reshape(-1,1)
#score_test = model_mlp.score(x2,valid_y)
#print("测试集上的得分是：{0:.2f}%".format(score*100))
# inp = [[ele] for ele in X_train]
# pre = clf.predict(inp)
# #print(pre)
plt.plot(train_x, train_y, 'bo')
plt.plot(train_x, result, 'ro')
plt.show()

pred_test = model_mlp.predict(test_x)

print('MSE是：{0:.2f}'.format(mean_squared_error(test_y,pred_test)*100))
print('MAE是：{0:.2f}'.format(mean_absolute_error(test_y,pred_test)*100))
print('R^2是：{0:.2f}%'.format(r2_score(test_y,pred_test)*100))
pred_test[pred_test < 0] = 0
pred_test = pred_test * 10
np.savetxt('b.txt', pred_test, fmt="%s", delimiter= ',')
``````

评论
0 评论