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
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Dec 9 23:03:35 2019 | |
@author: caiob | |
""" | |
import pandas as pd | |
import numpy as np | |
import io | |
from sklearn.model_selection import train_test_split |
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Dec 9 23:03:35 2019 | |
@author: caiob | |
""" | |
import pandas as pd | |
import numpy as np | |
import io | |
from sklearn.model_selection import train_test_split |
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
xgb_pipeline = Pipeline([("st_scaler", StandardScaler()),("xgb_model",xgb.XGBRegressor())]) | |
gbm_param_grid = { | |
'xgb_model__subsample': np.arange(.05, 1, 0.05), | |
'xgb_model__max_depth': np.arange(5,50,5), | |
'xgb_model__colsample_bytree': np.arange(.1,1.05,.05) | |
} | |
randomized_neg_mse = RandomizedSearchCV(estimator=xgb_pipeline, | |
param_distributions=gbm_param_grid, n_iter=10, |
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
cv_params = {"objective":"reg:squarederror", "max_depth":4} | |
cv_results = xgb.cv(dtrain=DM_train, params=cv_params,nfold=10,num_boost_round=5, metrics="rmse",as_pandas=True,seed=42) |
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
X,y = df_hist.iloc[:,1:],df_hist.iloc[:,1] | |
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=42) | |
DM_train = xgb.DMatrix(data=X_train, label=y_train) | |
DM_test = xgb.DMatrix(data=X_test, label=y_test) | |
params = {"booster":"gblinear", "objective":"reg:linear"} | |
xg_reg = xgb.train(params=params, dtrain=DM_train, num_boost_round=10) | |
preds = xg_reg.predict(DM_test) | |
rmse = np.sqrt(mean_squared_error(y_test,preds)) |
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
df_hist.to_csv('LMT_stock.csv') | |
df_hist[['Year','Month','Day']] = df_hist['Date'].str.split('-',3,expand=True) | |
df_hist = df_hist.drop('Date',axis=1) | |
df_hist = df_hist.apply(pd.to_numeric) |
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
wtd = WorldTradingData(token) | |
optional_params = {'output':'csv'} | |
LMT_history = wtd.history('LMT',optional_params) | |
df_hist = pd.read_csv(io.StringIO(LMT_history)) | |
df_hist.info() |
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
import requests | |
from worldtradingdata import WorldTradingData | |
import pandas as pd | |
import io | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import mean_squared_error | |
import xgboost as xgb | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.pipeline import Pipeline |
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
import requests | |
from worldtradingdata import WorldTradingData | |
import pandas as pd | |
import io | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import mean_squared_error | |
import xgboost as xgb | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.pipeline import Pipeline |
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
import pandas as pd | |
income_data = pd.read_csv("income.csv") | |
income.head() |