python - Scikit-learn SVM: Reshaping X leads to incompatible shapes -


i try use scikit-learn svm predict whether stock s&p500 beats index or not. have 'sample' file extract features x , labels (beats index or doesn't beat it) y.

when tried first time (without reshaping x) got the following depreciation error:

deprecationwarning: passing 1d arrays data deprecated in 0.17  , raise valueerror in 0.19. reshape data either using x.reshape(-1, 1) if data has single feature or x.reshape(1, -1)  if contains single sample. 

consequently tried reshaping of x according recommendation , forum posts. following value error x , y don't have same shape.

valueerror: x , y have incompatible shapes. x has 4337 samples, y has 393. 

below can see shapes of x , y before reshaping:

('shape of x = ', (493, 9)) ('shape of y = ', (493,)) 

and after reshaping:

('shape of x = ', (4437, 1)) ('shape of y = ', (493,)) 

i tried reshape (493,9) shape, didn't work got following error.

valueerror: total size of new array must unchanged. 

i posted below code extract features , labels pandas dataframe , and svm analysis:

feature & label selection:

x = np.array(sample[features].values) x = preprocessing.scale(x)     x = np.array(x)     x = x.reshape(-1,1)      y = sample['status'].values.tolist() y = np.array(y)  z = np.array(sample[['changemktvalue', 'benchmark']]) 

svm testing:

test_size = 50  invest_amount = 1000 total_invests = 0 if_market = 0 if_strat = 0        clf = svm.svc(kernel="linear", c= 1.0) clf.fit(x[:-test_size],y[:-test_size])  correct_count = 0  x in range(1, test_size+1):     if clf.predict(x[-x])[0] == y[-x]:         correct_count += 1      if clf.predict(x[-x])[0] == 1:         invest_return = invest_amount + (invest_amount * (z[-x][0]/100)) #zeroth element of z          market_return = invest_amount + (invest_amount * (z[-x][1]/100)) #marketsp500 @ pos 1          total_invests += 1         if_market += market_return         if_strat += invest_return  print("accuracy:", (float(correct_count)/test_size) * 100.00) 

would great if have inputs on how solve this.

you should not reshaping x (-1, 1). in fact error in call predict method.

change

clf.predict(x[-x])[0] 

to

clf.predict(x[-x].reshape((-1, 9)))[0] 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -