# calibrated classifier ValueError: could not convert string to float

Dataframe:

id    review                                              name         label
1     it is a great product for turning lights on.        Ashley
2     plays music and have a good sound.                  Alex
3     I love it, lots of fun.                             Peter


I want to use probabilistic classifier (linear_svc) to predict labels (probability of 1) based on review. My code:

from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn import datasets

X = training['review']
y = training['label']

linear_svc = LinearSVC()     #The base estimator

# This is the calibrated classifier which can give probabilistic classifier
calibrated_svc = CalibratedClassifierCV(linear_svc,
method='sigmoid',  #sigmoid will use Platt's scaling. Refer to documentation for other methods.
cv=3)
calibrated_svc.fit(X, y)

# predict
prediction_data = predict_data['review']
predicted_probs = calibrated_svc.predict_proba(prediction_data)


It gives following error on calibrated_svc.fit(X, y):

ValueError: could not convert string to float: 'it is a great product for turning...'

Once I assume you are using text data as your input matrix X. The first point is that you have to include your preprocessing step as you would do when not using a calibrated classifier, so as you already know you can use a Pipeline like so:

calibrated_svc = CalibratedClassifierCV(linear_svc,
method='sigmoid',
cv=3)

model = Pipeline([('tfidf', TfidfVectorizer()), ('clf', calibrated_svc)]).fit(X, y)


Another option if your are interested in using probabilities in your SVM you can set the parameter probability = True inside your SVM but using the class SVC with a linear kernel is equvilalent to LinearSVC like:

model = Pipeline([('tfidf', TfidfVectorizer()), ('clf',SVC(probability = True, kernel = 'linear') )]).fit(X, y)


This will run a Logistic regression on the top of the binary predictions of the SVM.

Both options are feasible if you are only interested in using probabilities per se but if you are also interested on the calibration of your probabilities, the first option is better

For any kind of Machine Learning task or a NLP task (which is what you are doing), you need to convert string/text values to numeric values. The machine cannot uderstand or work with string values. It only understands numeric values.

So for example if you are doing a machine learning task, you would use libraries like OneHotEncoder, LabelEncoder etc to covert string values to numeric.

For your case, you are working on a NLP task which uses text values instead of string values. So you need to convert them into numeric values first and then fit the preferred algorithm. There are many ways to encode text into numeric such as Bag of Words, Tfidf, word2vec etc. You can read about them by searching on Google.

• @SaNa Kindly consider marking the answer as best if you think it helped you as it can help others to get the correct answer if they come across your question. Thank you. Sep 23 at 7:48