This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def SMA_TaLib(df): | |
df1 = df.copy() | |
df1["ma5"] = talib.SMA(df1['close'], timeperiod=5) | |
df1["ma15"] = talib.SMA(df1['close'], timeperiod=15) | |
df1["diff"] = df1.ma5 - df1.ma15 | |
df1["unixtime"] = [datetime.timestamp(t) for t in df1.index] | |
# line and Moving Average | |
fig = plt.figure(figsize=(15,5)) | |
ax = fig.add_subplot(1, 1, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pct_change | |
f = lambda x: 1 if x>0.0001 else -1 if x<-0.0001 else 0 if -0.0001<=x<=0.0001 else np.nan | |
y = df.rename(columns={'close': 'y'}).loc[:, 'y'].pct_change(1).shift(-1).fillna(0) | |
X = df.copy() | |
y_ = pd.DataFrame(y.map(f), columns=['y']) | |
df_ = pd.concat([df, y_], axis=1) | |
# check the shape | |
print('----------------------------------------------------------------------------------------') | |
print('X shape: (%i,%i)' % X.shape) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.pipeline import Pipeline | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score, f1_score | |
X_train, X_test, y_train, y_test = train_test_split(X_, y_, test_size=0.33, random_state=42) | |
print('X_train shape: {}'.format(X_train.shape)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open = pd.Series(df['open']) | |
high = pd.Series(df['high']) | |
low = pd.Series(df['low']) | |
close = pd.Series(df['close']) | |
volume = pd.Series(df['volume']) | |
# pct_change for new column | |
X['diff'] = y | |
# Exponential Moving Average |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ipyw_jlab_nb_ext_conf 0.1.0 py37_0 | |
_libgcc_mutex 0.1 main | |
alabaster 0.7.12 py37_0 | |
anaconda 2019.07 py37_0 | |
anaconda-client 1.7.2 py37_0 | |
anaconda-navigator 1.9.7 py37_0 | |
anaconda-project 0.8.3 py_0 | |
asn1crypto 0.24.0 py37_0 | |
astroid 2.2.5 py37_0 | |
astropy 3.2.1 py37h7b6447c_0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ipyw_jlab_nb_ext_conf 0.1.0 py37_0 | |
_libgcc_mutex 0.1 main | |
alabaster 0.7.12 py37_0 | |
anaconda 2019.07 py37_0 | |
anaconda-client 1.7.2 py37_0 | |
anaconda-navigator 1.9.7 py37_0 | |
anaconda-project 0.8.3 py_0 | |
asn1crypto 0.24.0 py37_0 | |
astroid 2.2.5 py37_0 | |
astropy 3.2.1 py37h7b6447c_0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# feature selection | |
open = pd.Series(df['open']) | |
high = pd.Series(df['high']) | |
low = pd.Series(df['low']) | |
close = pd.Series(df['close']) | |
volume = pd.Series(df['volume']) | |
# pct_change for new column | |
X['diff'] = y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Univariate Statistics | |
from sklearn.feature_selection import SelectPercentile | |
select = SelectPercentile(percentile=25) | |
select.fit(X_train_full, y_train_full.values.ravel()) | |
X_train_selected = select.transform(X_train_full) | |
X_test_selected = select.transform(X_test_full) | |
mask = select.get_support() | |
print(mask) | |
plt.matshow(mask.reshape(1, -1), cmap='gray_r') | |
plt.xlabel("Technical Indexes") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Model-based Selection | |
from sklearn.feature_selection import SelectFromModel | |
select = SelectFromModel(RandomForestClassifier(n_estimators=100, random_state=42), | |
threshold="1.25*mean") | |
select.fit(X_train_full, y_train_full.values.ravel()) | |
X_train_model = select.transform(X_train_full) | |
print(X_train_model.shape) | |
X_test_model = select.transform(X_test_full) | |
mask = select.get_support() | |
print(mask) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Recursive Feature Elimination | |
from sklearn.feature_selection import RFE | |
select = RFE(RandomForestClassifier(n_estimators=100, random_state=42), | |
n_features_to_select=15) | |
select.fit(X_train_full, y_train_full.values.ravel()) | |
X_train_rfe = select.transform(X_train_full) | |
X_test_rfe = select.transform(X_test_full) | |
mask = select.get_support() | |
print(mask) | |
plt.matshow(mask.reshape(1, -1), cmap='gray_r') |