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
$\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.

|improve this answer|||||
$\endgroup$
  • $\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$ – Media Apr 12 '18 at 16:11
  • 1
    $\begingroup$ did you run it? $\endgroup$ – Media Apr 12 '18 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])
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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