I'm getting the error "TypeError: 'StandardScaler' object is not iterable".
The error happens in this part of my code:
gps = joblib.load('gps.zip')
accuracies, remain_features, models = joblib.load(os.path.join(bagging_dir, model_fname))
remain_features = [c for c in remain_features if c != 'index']
y_preds = []
scalers = joblib.load('scalers.zip')
gp, scaler, model = list(zip(gps,scalers, models))[0]
Specifically, this line:
gp, scaler, model = list(zip(gps,scalers, models))[0]
How do I fix this error? If you know, please tell me. Thank you so much!
I'm guessing that comes from this line:
remain_features = [c for c in remain_features if c != 'index']
I think you can't use a list comprehension on that, maybe try converting it into a list first?
I am studying 'An Introduction to Statistical Learning' from James et al (2015). In the experiment section, a script to calculate the goodness-of-fit of different subsets using the k-fold cross validation method.
When I try to plot the error coefficients, I get the error:
Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "regsubsets"
The script makes too little sense for me to know what I'm doing wrong. Can anyone help me interpret?
library(leaps)
library(ISLR)
k=10
set.seed(1)
folds=sample(1:k,nrow(Hitters),replace=TRUE)
cv.errors=matrix(NA,k,19, dimnames=list(NULL, paste(1:19)))
for(j in 1:k){
best.fit=regsubsets(Salary~.,data=Hitters[folds!=j,],nvmax=19)
for(i in 1:19){
pred=predict(best.fit,Hitters[folds==j,],id=i)
cv.errors[j,i]=mean( (Hitters$Salary[folds==j]-pred)^2)
}
}
mean.cv.errors=apply(cv.errors,2,mean)
mean.cv.errors
par(mfrow=c(1,1))
plot(mean.cv.errors,type='b')
reg.best=regsubsets(Salary~.,data=Hitters, nvmax=19)
coef(reg.best,11)
I ran into the problem too. Hope you found the answer. If not, here is the answer.
I am sure that you already created the function below.
predict.regsubsets <- function(object, newdata, id,...) {
form <- as.formula(object$call[[2]])
mat <- model.matrix(form, newdata)
coefi <- coef(object, id = id)
xvars <- names(coefi)
mat[,xvars]%*%coefi
}
Now you have to change pred=predict(best.fit,Hitters[folds==j,],id=i) to pred <- predict.regsubsets(best.fit, hitters[folds == j, ], id = i)
Hope it helped.
I am using the blpapi package in R to download FX forward prices. In the formula I want to specify the setting to download forward prices as points or as outright prices. I have tried the following:
conn <- blpConnect()
sdate <- as.Date("1998-12-31")
edate <- Sys.Date()-1
vFWD <- c("EURAUD1M Curncy")
opts.daily <- c("periodicitySelection"="DAILY","nonTradingDayFillMethod"="PREVIOUS_VALUE","nonTradingDayFillOption"="NON_TRADING_WEEKDAYS")
opts.monthly <- c("periodicitySelection"="MONTHLY","nonTradingDayFillMethod"="PREVIOUS_VALUE","nonTradingDayFillOption"="NON_TRADING_WEEKDAYS")
opts.fwd <- c("FWD_CURVE_QUOTE_FORMAT"="OUTRIGHTS")
dfwd <- bdh(securities = vFWD, c("PX_LAST"), start.date = sdate, end.date = edate, options = opts.daily, overrides = opts.fwd, con = defaultConnection())
** for Java coding the answer is here: In Bloomberg API how do you specify to get FX forwards as a spread rather than absolute values?
Use "OUTRIGHT", not "OUTRIGHTS" as your override option value.
Error in UseMethod("QuinlanAttributes") :
no applicable method for 'QuinlanAttributes' applied to an object of class "logical"
I am getting this error whenever I am running a code. I have installed several packages but this error is keep on repitiating.
it seems that C50 does not accept BOOLEAN features.
you can simpliy drop that column or replace BOOLEAN to 0/1.
if "tdata$Windy" is the BOOLEAN feature, replace the value of it.
library(C50)
tdata = read.csv('play.csv', header = TRUE, sep = ",")
xdata <- data.frame(tdata$Outlook,tdata$Temperature, tdata$Humidity, tdata$Windy)
ydata <- tdata$Play
treeModel <- C5.0(x = xdata, y = ydata )
summary(treeModel)
It's old question but I had same issue today and realized it's was due to read_sav().
I solved applying haven::as_factor to columns that should be factors.
data <- read_sav("datafile.sav")
data <- mutate(data, across(ends_with("_fct"), haven::as_factor ))
Help, why is this simple approach not working?
I am trying to create a function around one of the quantstrat demos (bbands) and pass the stock symbol as a parameter.
I think I am trying to create a function around a working piece of code and replace one hard-coded constant by a parameter passed via the function. Where to look first, is this a problem with different classes or environments?
Reproducable code below:
try_BBands <- function(stock)
{
########## body of function is demo from quantstrat package
require(quantstrat)
suppressWarnings(rm("order_book.bbands",pos=.strategy))
suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))
########## here I replaced the following line:
########## stock.str = 'IBM'
stock.str <- stock
# we'll pass these
SD = 2 # how many standard deviations, traditionally 2
N = 20 # how many periods for the moving average, traditionally 20
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
initDate='2006-12-31'
initEq=1000000
portfolio.st='bbands'
account.st='bbands'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios='bbands', initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
addPosLimit(portfolio.st, stock.str, initDate, 200, 2 ) #set max pos
stratBBands <- strategy("bbands")
#one indicator
stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
#add signals:
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
# lets add some rules
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='exit')
#alternately, to exit at the opposite band, the rules would be...
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#TODO add thresholds and stop-entry and stop-exit handling to test
getSymbols(stock.str,from=initDate, to='2012-07-05', index.class=c('POSIXt','POSIXct'))
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
# look at the order book
#getOrderBook('bbands')
end_t<-Sys.time()
#print("strat execution time:")
#print(end_t-start_t)
#start_t<-Sys.time()
updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''))
#end_t<-Sys.time()
#print("updatePortf execution time:")
#print(end_t-start_t)
chart.Posn(Portfolio='bbands',Symbol=stock.str)
#plot(add_BBands(on=1,sd=SD,n=N))
}
try_BBands('AAPL')
Error returned is:
Error in get(symbol) : object 'AAPL' not found
Advise is much appreciated!
Solved!
I had to include env=globalenv() in the getSymbols call.
Apparently the xts object created inside my function is not stored in the global environment, I presume.
Following line does the trick:
getSymbols(stock.str,from=initDate, to='2012-07-05', index.class=c('POSIXt','POSIXct'), env=globalenv())