pandas_TPOT.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import pandas as pd
  2. import numpy as np
  3. from tpot import TPOTClassifier
  4. from sklearn.model_selection import train_test_split
  5. benchmark = pd.read_pickle(
  6. "us_pct.pickle"
  7. ) # us overall housing price index percentage change
  8. HPI = pd.read_pickle(
  9. "HPI_complete.pickle"
  10. ) # all of the state data, thirty year mortgage, unemployment rate, GDP, SP500
  11. HPI = HPI.join(benchmark["United States"])
  12. # all in percentage change since the start of the data (1975-01-01)
  13. HPI.dropna(inplace=True)
  14. housing_pct = HPI.pct_change()
  15. housing_pct.replace([np.inf, -np.inf], np.nan, inplace=True)
  16. housing_pct["US_HPI_future"] = housing_pct["United States"].shift(-1)
  17. housing_pct.dropna(inplace=True)
  18. def create_labels(cur_hpi, fut_hpi):
  19. if fut_hpi > cur_hpi:
  20. return 1
  21. else:
  22. return 0
  23. housing_pct["label"] = list(
  24. map(create_labels, housing_pct["United States"], housing_pct["US_HPI_future"])
  25. )
  26. # housing_pct['ma_apply_example'] = housing_pct['M30'].rolling(window=10).apply(moving_average)
  27. # print(housing_pct.tail())
  28. X = np.array(housing_pct.drop(["label", "US_HPI_future"], 1))
  29. y = np.array(housing_pct["label"])
  30. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
  31. tpot = TPOTClassifier(generations=10, population_size=20, verbosity=2)
  32. tpot.fit(X_train, y_train)
  33. print(tpot.score(X_test, y_test))
  34. tpot.export("HPI_tpot_pipeline.py")