import sys
import pandas as pd
import numpy as np
import math
"""
sys:该模块提供对解释器使用或维护的一些变量的访问,以及与解释器强烈交互的函数
pandas:一个强大的分析结构化数据的工具集
numpy: Python的一个扩展程序库,支持大量的维度数组与矩阵运算
math:数学运算的库
作业一:由每天前9个小时的18个空气的影响因素(如:NO,CO,SO2,PM2.5等等)来预测第10个小时的PM2.5,train.csv是一年的数据,每个月取了20天,每天24小时
"""
data = pd. read_csv( r"G:\课程学习\机器学习\Mr_Li_ML\HomeWorks\数据\hw1\train.csv" , encoding= "big5" )
data_train = data. iloc[ : , 3 : ]
data_train[ data_train == "NR" ] = 0
data_train_row = data_train. to_numpy( )
month_data = { }
for month in range ( 12 ) :
sample = np. zeros( [ 18 , 480 ] )
for day in range ( 20 ) :
sample[ : , 24 * day: ( day + 1 ) * 24 ] = data_train_row[ 18 * ( 20 * month + day) : 18 * ( 20 * month + day + 1 ) , : ]
month_data[ month] = sample
x = np. empty( [ 12 * 471 , 18 * 9 ] , dtype= float )
y = np. empty( [ 12 * 471 , 1 ] , dtype= float )
for month in range ( 12 ) :
for day in range ( 20 ) :
for hour in range ( 24 ) :
if day == 19 and hour > 14 :
continue
x[ 471 * month + day * 24 + hour, : ] = month_data[ month] [ : , day * 24 + hour: day * 24 + hour + 9 ] . reshape( 1 ,
- 1 )
y[ 471 * month + day * 24 + hour, 0 ] = month_data[ month] [ 9 , day * 24 + hour + 9 ]
x_mean = np. mean( x, axis= 0 )
x_std = np. std( x, axis= 0 )
for i in range ( x. shape[ 0 ] ) :
for j in range ( x. shape[ 1 ] ) :
if x_std[ j] != 0 :
x[ i] [ j] = ( x[ i] [ j] - x_mean[ j] ) / x_std[ j]
x_train_set = x[ : math. floor( len ( x) * 0.8 ) , : ]
y_train_set = y[ : math. floor( len ( x) * 0.8 ) , : ]
x_validation = x[ math. floor( len ( x) * 0.8 ) : , : ]
y_validation = y[ math. floor( len ( x) * 0.8 ) : , : ]
dim = 18 * 9 + 1
w = np. zeros( [ dim, 1 ] )
x_train_set = np. concatenate( ( np. ones( [ len ( x_train_set) , 1 ] ) , x_train_set) , axis= 1 ) . astype( float )
learning_rate = 1
iter_time = 30000
adagrad = np. zeros( [ dim, 1 ] )
eps = 0.00000001
for t in range ( iter_time) :
loss = np. sqrt( np. sum ( np. power( np. dot( x_train_set, w) - y_train_set, 2 ) ) / len ( x_train_set) )
if ( t % 100 == 0 ) :
print ( "迭代次数: %i, 损失值: %i" % ( t, loss) )
gradient = ( np. dot( x_train_set. transpose( ) , np. dot( x_train_set, w) - y_train_set) ) / (
loss * len ( x_train_set) )
adagrad += ( gradient ** 2 )
w = w - learning_rate * gradient / np. sqrt( adagrad + eps)
np. save( "weight.npy" , w)
x_validation = np. concatenate( ( np. ones( [ len ( x_validation) , 1 ] ) , x_validation) , axis= 1 ) . astype( float )
for m in range ( len ( x_validation) ) :
Loss = np. sqrt( np. sum ( np. power( np. dot( x_validation, w) - y_validation, 2 ) ) / len ( x_validation) )
print ( "The Loss on val data is %f" % ( Loss) )
import sys
import pandas as pd
import numpy as np
import math
testdata = pd. read_csv( r"G:\课程学习\机器学习\Mr_Li_ML\HomeWorks\数据\hw1\test.csv" , header= None , encoding= "big5" )
test_data = testdata. iloc[ : , 2 : ]
test_data[ test_data == "NR" ] = 0
test_data = test_data. to_numpy( )
test_x = np. empty( [ 240 , 18 * 9 ] , dtype= float )
mean_x = np. mean( test_x, axis= 0 )
std_x = np. std( test_x, axis= 0 )
for i in range ( 240 ) :
test_x[ i, : ] = test_data[ 18 * i: 18 * ( i + 1 ) , : ] . reshape( 1 , - 1 )
for i in range ( len ( test_x) ) :
for j in range ( len ( test_x[ 0 ] ) ) :
if std_x[ j] != 0 :
test_x[ i] [ j] = ( test_x[ i] [ j] - mean_x[ j] ) / std_x[ j]
test_x = np. concatenate( ( np. ones( [ 240 , 1 ] ) , test_x) , axis= 1 ) . astype( float )
w = np. load( "weight.npy" )
ans_y = np. dot( test_x, w)
print ( "----预测PM2.5的值----" )
print ( ans_y)