1
$\begingroup$
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm


X=[[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]] 

y=[0,1,1,1,0]

model=svm.SVC()
model.fit(X,y)
print(model.predict([1,0,1,0,0]))

I was working on this but Iam getting an error as

"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[1. 0. 1. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single fe
ature or array.reshape(1, -1) if it contains a single sample.

Iam new to this can u guys help me out?

$\endgroup$

2 Answers 2

2
$\begingroup$

After having performed

X=np.array([[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]])

y=np.array([0,1,1,1,0])

you have to do the following:

y = y.reshape(1, -1)

model=svm.SVC()
model.fit(X,y)
test = np.array([1,0,1,0,0])
test = test.reshape(1,-1)
print(model.predict(test))

In future you have to scale your dataset. You can use either Standard Scaler (suggested) or MinMax Scaler.

$\endgroup$
2
  • $\begingroup$ I guess print(model.predict([1,0,1,0,0])) should be print(model.predict(np.array([[1,0,1,0,0]]))), maybe :) Idon't have possibility to check right now $\endgroup$ Apr 12, 2018 at 16:11
  • 1
    $\begingroup$ did you run it? $\endgroup$ Apr 12, 2018 at 16:27
0
$\begingroup$

Your inputs are lists but they need to be arrays.

X=np.array([[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]])

y=np.array([0,1,1,1,0])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.