Advertisement
Guest User

Untitled

a guest
Feb 26th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | Source Code | 0 0
  1. # !!!!pred from the question = capacity_kw!!!!
  2. weather_states = pd.read_sql("SELECT stations.id, stations.capacity_kw, start, wind_speed_10m, wind_direction_10m, wind_speed_80m, wind_direction_80m, wind_speed_180m, wind_direction_180m FROM stations INNER JOIN weather_states ON stations.id = weather_states.station WHERE weather_states.source = 'openmeteo_forecast/history/best' AND stations.source = 'wind'", db_client)
  3.  
  4. grid_states = pd.read_sql("SELECT start, wind FROM grid_states", db_client)
  5.  
  6. def create_x_y(df: tuple[Any, pd.DataFrame]):
  7.     start = df[1]["start"].iloc[0]
  8.     res = df[1].sort_values("id").drop(["id", "start"], axis=1)
  9.     temp_wind = grid_states.loc[grid_states["start"] == start]["wind"].to_list()
  10.     wind_kw = temp_wind if len(temp_wind) >= 1 else None
  11.     res_flat_df = pd.DataFrame(res.to_numpy().reshape((1, -1)))
  12.     res_flat_df["wind_kw"] = wind_kw
  13.     return res_flat_df
  14.  
  15. data = pd.concat(map(create_x_y, weather_states.groupby("start"))).dropna()
  16. from sklearn.model_selection import train_test_split
  17.  
  18.  
  19. data = data.astype("float32")
  20. train, test = train, test = train_test_split(data.dropna(), test_size=0.2)
  21.  
  22. train_y = train.pop("wind_kw")
  23. train_x = train
  24.  
  25.  
  26. test_y = test.pop("wind_kw")
  27. test_x = test
  28.  
  29. norm = tf.keras.layers.Normalization()
  30. norm.adapt(train_x)
  31.  
  32. model = tf.keras.Sequential([
  33.     norm,
  34.     tf.keras.layers.Dense(16, activation="linear"),
  35.     tf.keras.layers.Dropout(0.3),
  36.     tf.keras.layers.Dense(1, activation="linear"),
  37. ])
  38.  
  39.  
  40. model.compile(
  41.     optimizer=tf.keras.optimizers.legacy.Adam(0.001),
  42.     metrics=[tf.keras.metrics.R2Score(dtype=tf.float32)],
  43.     loss=tf.keras.losses.MeanSquaredError(),
  44. )
  45.  
  46. model.fit(train_x, train_y, epochs=7, batch_size=2)
  47.  
  48. tf.keras.models.save_model(model, 'wind.keras')
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement