Reputation: 269
I am making a program to train SVM (Support Vector Machine) classifier in python using the below code: `
print ("Fetching saved features and corresponding labels from disk")
features, labels = load_features_labels(1)
print("Features and labels successfully loaded")
print(np.array(labels))
print(len(np.array(features)))
print(len(np.array(labels)))
clf = LinearSVC()
print("Fitting classifier")
clf.fit(np.array(features), np.array(labels))
print("Classifier fitting completed")
print("Persisting classifier model")
pickle.dump(clf, open("clf.p", "wb"))
print("Model persistence completed")
but i am getting this output:
Fetching saved features and corresponding labels from disk
Features and labels successfully loaded
[1 1 1 ..., 0 0 0]
1722
1722
Fitting classifier
Traceback (most recent call last):
File "train.py", line 20, in <module>
clf.fit(np.array(features), np.array(labels))
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/svm/classes.py", line 205, in fit
dtype=np.float64, order="C")
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 510, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 415, in check_array
context))
ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.
As can be seen in the output that the total number of features and labels is equal i-e 1722. Why its showing this error:
ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.
Upvotes: 2
Views: 785
Reputation: 2758
Looks like your features array is not correct, it should have a form similar to this np.array([[1,2,3],[2,2,2],[1,1,1]])
, more like an array of arrays. I'm suspecting you might have something like this np.array([1,2,3,2,2,2,1,1,1])
?
Basically the fit
method expects an array of n_samples and n_features, from the documentation "fit(X,y) X : {array-like, sparse matrix}, shape = [n_samples, n_features]". Your error message is telling you that no features have been provided and that's why I'm suspecting that instead of an array [n_samples,n_features] as the X parameter you are just feeding an array.
Upvotes: 1