pandas_TPOT.py 1.3 KB

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