R或Python - 循环测试数据 - 24小时预测验证(每天96个值) [英] R or Python - loop the test data - Prediction validation next 24 hours (96 values each day)
问题描述
我有一个大型数据集,低于培训和测试数据集
I have a large dataset, below the training and test datasets
head(train_data)
date Date_time Temp Ptot JFK AEH ART CS CP
1 2016-01-29 2016-01-29 00:00:00 30.3 1443.888 52.87707 49.36879 28.96548 6.239999 49.61212
2 2016-01-29 2016-01-29 00:15:00 30.3 1410.522 49.50248 49.58356 26.37977 5.024000 49.19649
3 2016-01-29 2016-01-29 00:30:00 30.3 1403.191 50.79809 49.04253 26.15317 5.055999 47.48126
4 2016-01-29 2016-01-29 00:45:00 30.3 1384.337 48.88359 49.14100 24.52135 5.088000 46.19261
5 2016-01-29 2016-01-29 01:00:00 30.1 1356.690 46.61842 48.80624 24.28208 5.024000 43.00352
6 2016-01-29 2016-01-29 01:15:00 30.1 1341.985 48.09687 48.87748 24.49988 4.975999 39.90505
test_data是2018-01-01至2018-07-12
test_data is from 2018-01-01 to 2018-07-12
tail(test_data)
date Date_time Temp Ptot JFK AEH ART CS CP
86007 2018-07-12 2018-07-12 22:30:00 64.1 1458.831 82.30099 56.93944 27.20252 2.496 54.41050
86008 2018-07-12 2018-07-12 22:45:00 64.1 1457.329 61.68535 54.28934 28.59752 3.728 54.15208
86009 2018-07-12 2018-07-12 23:00:00 63.5 1422.419 80.56367 56.40752 27.99190 3.520 53.85705
86010 2018-07-12 2018-07-12 23:15:00 63.5 1312.021 52.25757 56.40283 22.03727 2.512 53.72166
86011 2018-07-12 2018-07-12 23:30:00 63.5 1306.349 65.65347 56.20145 22.77093 3.680 52.71584
86012 2018-07-12 2018-07-12 23:45:00 63.5 1328.528 57.47283 57.73747 19.50940 2.432 52.37458
I想要在 test_data 中进行24小时(2018-01-01到2018-07-12的每一天)的预测验证循环。每天预测是(96)值 - 15分钟采样 - 。换句话说,我每次都要选择96个值并将它们放在代码中显示的test_data中并计算MAPE。
I want to make a prediction validation loop for 24 hours(Each day from 2018-01-01 to 2018-07-12) in test_data . Each day prediction is (96) values-15 minute sampling-. In other words, I have to select 96 values each time and put them in the test_data shown in the code and calculate MAPE.
input = train_data[c("Temp","JFK","AEH","ART","CS","CP","RLF", "FH" ,"TJF" ,"GH" , "JPH","JEK", "KL",
"MH","MC","MRH", "PH","OR","RP","RC","RL","SH", "SPC","SJH","SMH","VWK","WH","Month","Day",
"Year","hour")]
target = train_data["Ptot"]
glm_model <- glm(Ptot~ ., data= c(input, target), family=gaussian)
我想迭代 test_data - 创建一个循环 - 每次从测试表中取96次观察-96行 - 按顺序一直到数据集结束并计算MAPE和保存所有的价值。我在R中实现了这个。
I want to iterate through the "test_data" -create a loop- by taking each time 96 observation -96 rows- from the test table sequentially until the end of the dataset and calculate MAPE and save all of the value. I implemented this in R.
如下图所示。每次从(test_data)获取96行并将它们放在函数中的test_data中。这只是一个解释,没有显示所有96个值:)
As shown below in fig. each time take 96 rows from (test_data) and put them in "test_data" in the function. It is just an explanation, not showing all 96 values :)
pred<- predict.glm(glm_model,test_data)
mape <- function(actual, pred){
return(100 * mean(abs((actual- pred)/actual)))
}
我将展示如何进行第一天预测验证
1-从test_data中选择96个值(即2018-01-01)
I will show how to make first-day prediction validation
1- select 96 values from test_data(i.e 2018-01-01)
One_day_data <- test_data[test_data$date == "2018-01-01",]
2-在函数中放置一天值
2- Put one day values in the function
pred<- predict.glm(glm_model,One_day_data )
3-这是运行pred之后的预测结果(96值=一天)
3- This is the prediction results after running pred (96 values =one day)
print(pred)
67489 67490 67491 67492 67493 67494 67495 67496 67497 67498
1074.164 1069.527 1063.726 1082.404 1077.569 1071.265 1070.776 1073.686 1061.720 1063.554
67499 67500 67501 67502 67503 67504 67505 67506 67507 67508
1074.264 1067.393 1071.111 1076.754 1079.700 1071.244 1097.977 1089.862 1091.817 1098.025
67509 67510 67511 67512 67513 67514 67515 67516 67517 67518
1125.495 1133.786 1136.545 1138.473 1176.555 1183.483 1184.795 1186.220 1192.328 1187.582
67519 67520 67521 67522 67523 67524 67525 67526 67527 67528
1186.513 1254.844 1262.021 1258.816 1240.280 1229.237 1237.582 1250.030 1243.189 1262.266
67529 67530 67531 67532 67533 67534 67535 67536 67537 67538
1251.563 1242.417 1259.352 1269.760 1271.318 1266.984 1260.113 1247.424 1200.905 1198.161
67539 67540 67541 67542 67543 67544 67545 67546 67547 67548
1202.372 1189.016 1193.479 1194.668 1207.064 1199.772 1189.068 1176.762 1188.671 1208.944
67549 67550 67551 67552 67553 67554 67555 67556 67557 67558
1199.216 1193.544 1215.866 1209.969 1180.115 1182.482 1177.049 1196.165 1145.335 1146.028
67559 67560 67561 67562 67563 67564 67565 67566 67567 67568
1161.821 1163.816 1114.529 1112.068 1113.113 1107.496 1073.080 1082.271 1097.888 1095.782
67569 67570 67571 67572 67573 67574 67575 67576 67577 67578
1081.863 1068.071 1061.651 1072.511 1057.184 1068.474 1062.464 1061.535 1054.550 1050.287
67579 67580 67581 67582 67583 67584
1038.086 1045.610 1038.836 1030.429 1031.563 1019.997
我们可以从Ptot获得实际值
We can get the actual value from "Ptot"
actual<- One_day_data$Ptot
[1] 1113.398 1110.637 1111.582 1110.816 1101.921 1111.091 1108.501 1112.535 1104.631 1108.284
[11] 1110.994 1106.585 1111.397 1117.406 1106.690 1101.783 1101.605 1110.183 1104.162 1111.829
[21] 1117.093 1125.493 1118.417 1127.879 1133.574 1136.395 1139.048 1141.850 1145.630 1141.288
[31] 1141.897 1140.310 1138.026 1121.849 1122.069 1120.479 1120.970 1111.594 1109.572 1116.355
[41] 1115.454 1113.911 1115.509 1113.004 1119.440 1112.878 1117.642 1100.516 1099.672 1109.223
[51] 1105.088 1107.167 1114.355 1110.620 1110.499 1110.161 1107.868 1118.085 1108.166 1106.347
[61] 1114.036 1106.968 1109.807 1113.943 1106.869 1104.390 1102.446 1110.770 1114.684 1114.142
[71] 1118.877 1128.470 1133.922 1128.420 1134.058 1142.529 1126.432 1127.824 1124.561 1130.823
[81] 1122.907 1117.422 1116.851 1114.980 1114.543 1108.584 1120.410 1120.900 1109.226 1101.367
[91] 1098.330 1110.474 1106.010 1108.451 1095.196 1096.007
4-运行Mape功能并保存结果(我有实际值)
4- Run Mape function and save the results (I have the actual values)
mape <- function(actual, pred){
return(100 * mean(abs((actual- pred)/actual)))
}
5-在接下来的24小时内做同样的事情(即2018-01-02)等等
不完整的解决方案,这是不正确的! (我认为应该通过这样的方式完成)
5- Do the same thing it for next 24 hours (i.e 2018-01-02) and so on
Incomplete Solution, it is not correct! (I think it should be done by something like this)
result_df =[]
for (i in 1:96){
test_data<- test_data[i,]
pred<- predict.glm(glm_model,test_data)
result_df$pred[i] <- pred
result_df$Actual[i+1] <- result_df$pred[i]
mape[i] <- function(actual, pred){
return(100 * mean(abs((actual- pred)/actual)))
}
}
< h1>摘要:我想通过每次递增一天将所有值的mape存储到pred。
注意:我会感谢您能在R和/或Python中向我展示循环过程。
SUMMARY: I want to store all of the values of mape by passing one day incrementally each time to pred.
NOTE: I will appreciate if you can show me the loop process in R and/or Python.
推荐答案
考虑构建一个通用函数 mape_calc ,以接收子集数据帧作为输入和调用R的中的函数由
组成。作为 tapply
的面向对象的包装器,的将按每个不同的日期<对主数据框进行子集化/ em>,将子集传递给定义的函数进行计算。
Consider building a generalized function, mape_calc, to receive a subset data frame as input and call the function in R's by
. As the object-oriented wrapper to tapply
, by
will subset the main data frame by each distinct date, passing subsets into defined function for calculation.
在该方法中,构建一个新的单行数据框,以便将 mape 与每个日期对齐。然后所有行都与 do.call
绑定在一起:
Within the method, a new, one-row data frame is built to align mape with each date. Then all rows are binded together with do.call
:
mape_calc <- function(sub_df) {
pred <- predict.glm(glm_model, sub_df)
actual <- sub_df$Ptot
mape <- 100 * mean(abs((actual - pred)/actual))
new_df <- data.frame(date = sub_df$date[[1]], mape = mape)
return(new_df)
}
# LIST OF ONE-ROW DATAFRAMES
df_list <- by(test_data, test_data$date, map_calc)
# FINAL DATAFRAME
final_df <- do.call(rbind, df_list)
如果你在Python pandas和numpy中有相同的设置(可能 statmodels for glm model),使用pandas DataFrame.groupby
作为R的对应
。当然,根据你的实际需要调整伪代码。
Should you have same setup in Python pandas and numpy (possibly statsmodels for glm model), use pandas DataFrame.groupby
as the counterpart to R's by
. Of course adjust below pseudocode to your actual needs.
import pandas as pd
import numpy as np
import statsmodels.api as sm
...
train_data = sm.add_constant(train_data)
model_formula = 'Ptot ~ Temp + JFK + AEH + ART + CS + CP ...'
glm_model = sm.glm(formula = model_formula,
data = train_data.drop(columns=['date','Date_time']),
family = sm.families.Gaussian()).fit()
def mape_calc(dt, sub_df):
pred = glm_model.predict(sub_df.drop(columns=['date','Date_time','Ptot']))
actual = sub_df['Ptot']
mape = 100 * np.mean(np.abs((actual - pred)/actual))
new_df = pd.DataFrame({'date': dt, 'mape': mape}, index=[0])
return new_df
# LIST OF ONE-ROW DATAFRAMES
df_list = [mape_calc(i, g) for i, g in test_data.groupby('date')]
# FINAL DATAFRAME
final_df = pd.concat(df_list, ignore_index=True)
这篇关于R或Python - 循环测试数据 - 24小时预测验证(每天96个值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!