資源簡介
本資源使用線性回歸的手段模擬預測PM2.5,包含了所有的數據以及代碼。代碼含有詳細地注釋,歡迎下載學習使用!

代碼片段和文件信息
‘‘‘
本程序簡單預測PM2.5的走向,每個月只取了20天的測量數據。
共有18中污染源,即將?18種污染源的數據和對應的PM2.5的數據?進行訓練
再根據訓練結果來預測新的數據下PM2.5的值
‘‘‘
#???import?library??#
import?csv?
import?numpy?as?np
from?numpy.linalg?import?inv
import?random
import?math
import?sys
#???read?data???#
data?=?[]
#?每一個維度存儲一種污染物的數據一共有18種污染物
for?i?in?range(18):
data.append([])?#?[]表示這十八個輸入中,每一個輸入都是一個列表
n_row?=?0?#?初始從第0行開始
#?打開數據文件文件big5編碼為繁體字
text?=?open(‘D:/PythonCodes/CNN/train.csv‘?‘r‘?encoding=‘big5‘)?
#?讀取名稱為text的Excel文件,返回文件行的累加信息類型為_csv.reader
row?=?csv.reader(text??delimiter=““)?
#?r中保存了當前行的所有信息,r是一個列表,每次循環(huán)到這就更換到下一行
for?r?in?row:?
????#?第0行沒有數據信息
????if?n_row?!=?0:
????????#?每一列只有第3-27格有值(一天內24小時的數值)
????????for?i?in?range(327):
????????????if?r[i]?!=?“NR“:
????????????????#?data[]中一共可以存放18個列表,每一個列表存放某一種污染物的所有數值
????????????????data[(n_row-1)%18].append(float(r[i]))?
????????????else:
????????????????data[(n_row-1)%18].append(float(0))
????n_row?=?n_row+1
text.close()
‘‘‘
file?=?open(‘D:/PythonCodes/CNN/testdata.csv‘‘w‘?encoding?=?‘utf-8‘)
for?i?in?range(len(data)):
????s?=?str(data[i]).replace(‘[‘‘‘).replace(‘]‘‘‘)#去除[]這兩行按數據不同,可以選擇
????s?=?s?+?‘\n‘???#去除單引號,逗號,每行末尾追加換行符
????file.write(s)
file.close()
print(“保存文件成功“)?
‘‘‘
#????Parse?Data?to?(xy)?????#
x?=?[]
y?=?[]
#?一共有12個月,每個月20天
for?i?in?range(12):
????#?一共有480個小時,連續(xù)取10個小時一組的數據,可取471組(最后9個數據舍去)
????for?j?in?range(471):
????????x.append([])?#?在x中加入列表存儲數據
????????#?污染物的種類一共有18種
????????for?t?in?range(18):
????????????#?取每種污染物前9小時的數據在第10小時存放PM2.5的值
????????????for?s?in?range(9):?
????????????????x[471*i+j].append(data[t][480*i+j+s]?)
????????y.append(data[9][480*i+j+9])?#?PM2.5的數據存放在第10行
#?每個按行排列
x?=?np.array(x)
y?=?np.array(y)
#?add?square?term
#?x?=?np.concatenate((xx**2)?axis=1)?將數據量翻倍,新的數據為x^2
#?add?bias
#?5652行1列的ones,每一行1后面直接連接x的每行列表
#?此處多的一列存儲的是bias,值為1
x?=?np.concatenate((np.ones((x.shape[0]1))x)?axis=1)?
#print(x.shape)?#?(5652?163)
#init?weight?&?other?hyperparams#
w?=?np.zeros(len(x[0]))?#?shap:?(163)?0:?163?__len__:?1?size:?163
l_rate?=?10?#?學習率,用于更新w
repeat?=?10000?#?迭代次數
#check?your?ans?with?close?form?solution#
#?use?close?form?to?check?whether?ur?gradient?descent?is?good
#?however?this?cannot?be?used?in?hw1.sh?
#?w?=?np.matmul(np.matmul(inv(np.matmul(x.transpose()x))x.transpose())y)
‘‘‘模型是Y?=?b?+?w?*?x‘‘‘
#start?training#
#?將x矩陣進行轉置,此時第一行全是1
#?shape:?(5652)?0:?5652?len:?1?size:?5652
x_t?=?x.transpose()?
s_gra?=?np.zeros(len(x[0]))?#?163行的0
for?i?in?range(repeat):
????hypo?=?np.dot(xw)?#?做矩陣乘法,(5652?163)*(163?1)?shape?=?(5652?1)預測值
????loss?=?hypo?-?y?#?預測數值?-?真實數值
????cost?=?np.sum(loss**2)?/?len(x)?#?L(f)?=?(y?-?f)^2?求和再求平局值
????cost_a??=?math.sqrt(cost)?#?開方
????#?梯度下降
????gra?=?np.dot(x_tloss)?#?(163?5652)?*?(5652?1)???shape:?(163?1)???dL
????s_gra?+=?gra**2?
????ada?=?np.sqrt(s_gra)?#?均方根
????w?=?w?-?l_rate?*?(gra/ada)?#?更新后的w
????print?(‘iteration:?%d?|?Cost:?%f??‘?%?(?icost_a))
#?sa
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????26033??2018-11-28?13:30??PM2.5Prediction\20171109191242797.png
?????文件??????30322??2018-11-28?13:30??PM2.5Prediction\20171109192721426.png
?????文件?????132209??2018-11-28?14:24??PM2.5Prediction\20171228222742072.png
?????文件??????86445??2018-11-28?14:24??PM2.5Prediction\20171228222805380.png
?????文件???????2264??2018-11-25?13:39??PM2.5Prediction\ans.csv
?????文件????8449299??2018-11-27?13:35??PM2.5Prediction\arrayx.csv
?????文件??????32474??2018-11-27?13:39??PM2.5Prediction\arrayy.csv
?????文件????8500646??2018-11-28?14:41??PM2.5Prediction\concatenateX.csv
?????文件????5132390??2018-11-28?09:38??PM2.5Prediction\listx.csv
?????文件???????1432??2018-11-25?15:33??PM2.5Prediction\model.npy
?????文件???????4678??2018-11-28?20:43??PM2.5Prediction\predict.csv
?????文件???????5270??2018-11-28?14:39??PM2.5Prediction\PredictionofPM2.5.py
?????文件??????23440??2018-11-27?13:10??PM2.5Prediction\QQ截圖20181127131014.png
?????文件???????2300??2018-11-25?13:52??PM2.5Prediction\sampleSubmission.csv
?????文件????????815??2018-11-28?10:40??PM2.5Prediction\s_gra.csv
?????文件?????192547??2018-11-25?13:52??PM2.5Prediction\test.csv
?????文件?????367871??2018-11-27?13:44??PM2.5Prediction\testdata.csv
?????文件?????465573??2018-11-25?13:51??PM2.5Prediction\train.csv
?????文件???????5311??2018-11-28?10:37??PM2.5Prediction\x_t.csv
?????目錄??????????0??2018-11-28?20:43??PM2.5Prediction
-----------?---------??----------?-----??----
?????????????23461319????????????????????20
- 上一篇:大氣湍流相位屏快速模擬
- 下一篇:od和CE查找call基址教材.rar
評論
共有 條評論