![見出し画像](https://assets.st-note.com/production/uploads/images/56048987/rectangle_large_type_2_48bcba64d34f112666a9d185fa6b7136.png?width=1200)
超簡単Pythonで株価予測(TPOT 利用)自動機械学習(AutoML)-基本-
PythonでTPOTを利用して翌日の株価の上下予測を超簡単に自動機械学習(AutoML)
1. ツールインストール
$ pip install tpot yfinance
2. ファイル作成
pred.py
from tpot import TPOTClassifier
import yfinance as yf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
df = yf.download("AAPL", start="2010-11-01", end="2020-11-01")
df["Diff"] = df.Close.diff()
df["SMA_2"] = df.Close.rolling(2).mean()
df["Force_Index"] = df.Close * df.Volume
df["y"] = df["Diff"].apply(lambda x: 1 if x > 0 else 0).shift(-1)
df = df.drop(
["Open", "High", "Low", "Close", "Volume", "Diff", "Adj Close"],
axis=1,
).dropna()
# print(df)
X = StandardScaler().fit_transform(df.drop(["y"], axis=1))
y = df["y"].values
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
shuffle=False,
)
tpot = TPOTClassifier(
generations=5,
population_size=50,
verbosity=2,
random_state=42,
)
tpot.fit(
X_train,
y_train,
)
y_pred = tpot.predict(X_test)
print(accuracy_score(y_test, y_pred))
3. 実行
$ python pred.py
[*********************100%***********************] 1 of 1 completed
Generation 1 - Current best internal CV score: 0.5293137291211436
Generation 2 - Current best internal CV score: 0.5293137291211436
Generation 3 - Current best internal CV score: 0.5303062849524092
Generation 4 - Current best internal CV score: 0.5303062849524092
Generation 5 - Current best internal CV score: 0.5303062849524092
Best pipeline: GaussianNB(GaussianNB(PolynomialFeatures(input_matrix, degree=2, include_bias=False, interaction_only=False)))
0.5555555555555556
以上、超簡単!
4. 結果
同じデータ、特徴量で、計算した結果、PyCaret・PyCaret(bagging)・PyCaret(voting)・PyCaret(stacking)・TPOT・TPOT(NN)・Auto-sklearn・AutoGluon・AutoKeras・FLAMLのうちTPOTが最も良いという事に
PyCaret 0.5178571428571429
PyCaret(bagging) 0.5496031746031746
PyCaret(voting) 0.5535714285714286
PyCaret(stacking) 0.5496031746031746
TPOT 0.5555555555555556
TPOT(NN) 0.503968253968254
Auto-sklearn 0.5198412698412699
AutoGluon 0.5496031746031746
AutoKeras 0.4861111111111111
FLAML 0.5277777777777778
5. 参考
6. 関連記事
この記事が気に入ったらサポートをしてみませんか?