Python深度學習筆記(一):使用TensorFlow建立神經網路進行PM2.5預測
5 min readMar 28, 2019
神經網路
引入模組與資料
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.metrics import explained_variance_score,mean_absolute_error,median_absolute_error
from sklearn.model_selection import train_test_splitdf=pd.read_excel('KH-1982-2018.xlsx')
查看資料訊息
df.describe().T
df.info()
選擇要預測的標籤
X = df[['SO2', 'CO', 'O3', 'Nox', 'NO', 'NO2', 'THC', 'NMHC', 'CH4', 'WindSpeed','TEMP','Humidity']]y = df['PM25']
分成測試集和訓練集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=23)X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5, random_state=23)#列出訓練集和測試集的筆數
X_train.shape, X_test.shape, X_val.shape print("Training instances {}, Training features {}".format(X_train.shape[0], X_train.shape[1])) print("Validation instances {}, Validation features {}".format(X_val.shape[0], X_val.shape[1])) print("Testing instances {}, Testing features {}".format(X_test.shape[0], X_test.shape[1]))
TensorFlow
#使用TensorFlow選擇特徵
feature_cols = [tf.feature_column.numeric_column(col) for col in X.columns]#深度學習DNN迴歸regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols,
hidden_units=[50, 50],
model_dir='tf_wx_model')#建立函式def wx_input_fn(X, y=None, num_epochs=None, shuffle=True, batch_size=400):
return tf.estimator.inputs.pandas_input_fn(x=X,
y=y,
num_epochs=num_epochs,
shuffle=shuffle,
batch_size=batch_size)#執行函式wx_input_fn(X, y=None, num_epochs=None, shuffle=True, batch_size=400)
#進行深度學習evaluations = []
STEPS = 400
for i in range(100):
regressor.train(input_fn=wx_input_fn(X_train, y=y_train), steps=STEPS)
evaluations.append(regressor.evaluate(input_fn=wx_input_fn(X_val,
y_val,
num_epochs=1,
shuffle=False)))
#列出第一筆評估evaluations[0]#進行預測pred = regressor.predict(input_fn=wx_input_fn(X_test,
num_epochs=1,
shuffle=False))#列出各種指標predictions = np.array([p['predictions'][0] for p in pred])
print("The Explained Variance: %.2f" % explained_variance_score(
y_test, predictions))
print("The Mean Absolute Error: %.2f " % mean_absolute_error(
y_test, predictions))
print("The Median Absolute Error: %.2f " % median_absolute_error(
y_test, predictions))
The Explained Variance: 0.96
The Mean Absolute Error: 2.17
The Median Absolute Error: 1.55