generated from dominodatalab/reference-project-wind-turbine-scada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
57 lines (41 loc) · 1.87 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pickle
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import ExtraTreesRegressor
def data_prep(dataDF):
dataDF["Date/Time"] = pd.to_datetime(dataDF["Date/Time"])
dataDF["Month"] = pd.DatetimeIndex(dataDF["Date/Time"]).month
dataDF["Hour"] = pd.DatetimeIndex(dataDF["Date/Time"]).hour
dataDF.drop(["Date/Time"], axis=1, inplace=True)
dataDF = dataDF.drop(dataDF[(dataDF["Month"]==1) | (dataDF["Month"]==12)].index)
Q1 = dataDF["Wind Speed (m/s)"].quantile(0.25)
Q3 = dataDF["Wind Speed (m/s)"].quantile(0.75)
IQR = Q3 - Q1
dataDF = dataDF[~((dataDF["Wind Speed (m/s)"] < (Q1 - 1.5 * IQR)) | (dataDF["Wind Speed (m/s)"] > (Q3 + 1.5 * IQR)))]
dataDF = dataDF[~((dataDF["LV ActivePower (kW)"] == 0) & (dataDF["Theoretical_Power_Curve (KWh)"] !=0))]
return dataDF
def train():
print("Ingesting data...")
dataDF = pd.read_csv("data/T1.csv")
print("Preparing data...")
dataDF = data_prep(dataDF)
trainDF, testDF = train_test_split(dataDF, test_size=0.2, random_state=1234)
X_train = trainDF.drop(columns=["LV ActivePower (kW)"]).values
y_train = trainDF["LV ActivePower (kW)"].values
X_test = testDF.drop(columns=["LV ActivePower (kW)"]).values
y_test = testDF["LV ActivePower (kW)"].values
print("Model training...")
model = ExtraTreesRegressor(n_estimators=100, random_state=1234)
model.fit(X_train, y_train)
print("Model evaluation...")
print("Accuracy on train: {:.0%}".format(model.score(X_train,y_train)))
print("Accuracy on test : {:.0%}".format(model.score(X_test,y_test)))
print("Persisting model...")
with open("model.bin", 'wb') as f_out:
pickle.dump(model, f_out)
f_out.close()
print("Model training completed.")
def main():
train()
if __name__ == "__main__":
main()