#NEWLINE# def compute_mse( regressor , param ) : #NEWLINE# #INDENT# park_id = NREL . park_id [ 'tehachapi' ] #NEWLINE# windpark = NREL ( ) . get_windpark ( park_id , 3 , 2004 ) #NEWLINE# target = windpark . get_target ( ) #NEWLINE# feature_window = 6 #NEWLINE# horizon = 3 #NEWLINE# mapping = PowerMapping ( ) #NEWLINE# X = mapping . get_features_park ( windpark , feature_window , horizon ) #NEWLINE# Y = mapping . get_labels_turbine ( target , feature_window , horizon ) #NEWLINE# train_to = int ( math . floor ( ( len ( X ) * 0.5 ) ) ) #NEWLINE# test_to = len ( X ) #NEWLINE# ( train_step , test_step ) = ( 5 , 5 ) #NEWLINE# if ( regressor == 'rf' ) : #NEWLINE# #INDENT# reg = RandomForestRegressor ( n_estimators = param , criterion = 'mse' ) #NEWLINE# reg = reg . fit ( X [ 0 : train_to : train_step ] , Y [ 0 : train_to : train_step ] ) #NEWLINE# y_hat = reg . predict ( X [ train_to : test_to : test_step ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( regressor == 'knn' ) : #NEWLINE# #INDENT# reg = KNeighborsRegressor ( param , 'uniform' ) #NEWLINE# reg = reg . fit ( X [ 0 : train_to : train_step ] , Y [ 0 : train_to : train_step ] ) #NEWLINE# y_hat = reg . predict ( X [ train_to : test_to : test_step ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'No regressor set.' ) #NEWLINE# #UNINDENT# #UNINDENT# naive_hat = zeros ( len ( y_hat ) , dtype = float32 ) #NEWLINE# for i in range ( 0 , len ( y_hat ) ) : #NEWLINE# #INDENT# naive_hat [ i ] = Y [ ( ( train_to + ( i * test_step ) ) - horizon ) ] #NEWLINE# #UNINDENT# ( mse_y_hat , mse_naive_hat ) = ( 0 , 0 ) #NEWLINE# for i in range ( 0 , len ( y_hat ) ) : #NEWLINE# #INDENT# y = Y [ ( train_to + ( i * test_step ) ) ] #NEWLINE# mse_y_hat += ( ( y_hat [ i ] - y ) ** 2 ) #NEWLINE# mse_naive_hat += ( ( naive_hat [ i ] - y ) ** 2 ) #NEWLINE# #UNINDENT# mse_y_hat /= float ( len ( y_hat ) ) #NEWLINE# mse_naive_hat /= float ( len ( y_hat ) ) #NEWLINE# return ( mse_y_hat , mse_naive_hat )