刚学python小白的疑惑
importtalib
fromjqdataimport
teststock600600。XSHG
startdatedatetime。date(2012,1,4)
enddatedatetime。date(2019,1,2)
tradingdayslist(getalltradedays())
startdateindextradingdays。index(startdate)
enddateindextradingdays。index(enddate)
xall〔〕
yall〔〕
forindexinrange(startdateindex,enddateindex):startdaytradingdays〔index30〕enddaytradingdays〔index〕stockdatagetprice(teststock,startdatestartday,enddateendday,frequencydaily,fields〔close〕)closepricesstockdata〔close〕。valuesmadatatalib。MA(closeprices)〔2〕rsidatatalib。RSI(closeprices)〔2〕momdatatalib。MOM(closeprices)〔2〕features〔〕features。append(madata)features。append(rsidata)features。append(momdata)labelFalse
ifcloseprices〔1〕closeprices〔2〕:labelTrue
xall。append(features)yall。append(label)
xtrainxall〔:1〕
ytrainyall〔:1〕
xtestxall〔1〕ytestyall〔1〕print(数据已准备好了)
fromsklearn。ensembleimportRandomForestClassifier
clfRandomForestClassifier()
clf。fit(xtrain,ytrain)
predictionclf。predict(xtest)
ifpredictionytest:
print(预测正确)
else:
print(预测错误)
现在就是出错的提示,因为我是头条新手,排版很乱,不能按原来的格式
ValueErrorTraceback(mostrecentcalllast)in5clf。fit(xtrain,ytrain)67predictionclf。predict(xtest)89ifpredictionytest:optcondalibpython3。6sitepackagessklearnensembleforest。pyinpredict(self,X)536Thepredictedclasses。537538probaself。predictproba(X)539540ifself。noutputs1:optcondalibpython3。6sitepackagessklearnensembleforest。pyinpredictproba(self,X)576checkisfitted(self,estimators)577Checkdata578Xself。validateXpredict(X)579580Assignchunkoftreestojobsoptcondalibpython3。6sitepackagessklearnensembleforest。pyinvalidateXpredict(self,X)355callfitbeforeexploitingthemodel。)356357returnself。estimators〔0〕。validateXpredict(X,checkinputTrue)358359propertyoptcondalibpython3。6sitepackagessklearntreetree。pyinvalidateXpredict(self,X,checkinput)371ValidateXwheneveronetriestopredict,apply,predictproba372ifcheckinput:373Xcheckarray(X,dtypeDTYPE,acceptsparsecsr)374ifissparse(X)and(X。indices。dtype!np。intcor375X。indptr。dtype!np。intc):optcondalibpython3。6sitepackagessklearnutilsvalidation。pyincheckarray(array,acceptsparse,dtype,order,copy,forceallfinite,ensure2d,allownd,ensureminsamples,ensureminfeatures,warnondtype,estimator)439Reshapeyourdataeitherusingarray。reshape(1,1)if440yourdatahasasinglefeatureorarray。reshape(1,1)441ifitcontainsasinglesample。。format(array))442arraynp。atleast2d(array)443ToensurethatarrayflagsaremaintainedValueError:Expected2Darray,got1Darrayinstead:array〔32。13354。3597150。56〕。Reshapeyourdataeitherusingarray。reshape(1,1)ifyourdatahasasinglefeatureorarray。reshape(1,1)ifitcontainsasinglesample。
这个是再joinquant的预测股价的随机森林的应用代码,我就想学习下,发现运行的时候就出错了,请问错在哪里