I have a dataset as follows:
X_data =
BankNum | ID |
00987772 | AB123 |
00987772 | AB123 |
00987772 | AB123 |
00987772 | ED245 |
00982123 | GH564 |
And another one as:
y_data =
ID | Labels
AB123 | High
ED245 | Low
GH564 | Low
I'm doing the following:
from sklearn import svm
from sklearn import metrics
import numpy as np
clf = svm.SVC(gamma=0.001, C=100., probability=True)
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.20, random_state=42)
clf.fit(X_train, y_train)
predicted = clf.predict(X_test)
But I want to know how do I transform this X_data
to float before I do clf.fit()
? Can I use DictVectorizer
in this case? If yes, then how do I use it?
Also, I'm passing X_data
and y_data
through train_test_split
to find out the prediction accuracy, but will it be splitting correctly? As in taking the correct Label
for a ID
in X_data
from y_data
?
UPDATE:
Can someone please tell me if I'm doing the following correctly?
new_df = pd.merge(df, df3, on="ID")
columns = ['BankNum', 'ID']
labels = new_df['Labels']
le = LabelEncoder()
labels = le.fit_transform(labels)
X_train, X_test, y_train, y_test = train_test_split(new_df[columns], labels, test_size=0.25, random_state=42)
X_train.fillna( 'NA', inplace = True )
X_test.fillna( 'NA', inplace = True )
x_cat_train = X_train.to_dict( orient = 'records' )
x_cat_test = X_test.to_dict( orient = 'records' )
vectorizer = DictVectorizer( sparse = False )
vec_x_cat_train = vectorizer.fit_transform( x_cat_train )
vec_x_cat_test = vectorizer.transform( x_cat_test )
x_train = vec_x_cat_train
x_test = vec_x_cat_test
clf = svm.SVC(gamma=0.001, C=100., probability=True)
clf.fit(x_train, y_train)