Convert categorical variables from String to int representation
Try factorize method:
In [264]: y_train = pd.Series(['A', 'B', 'A', 'C'])
In [265]: y_train
Out[265]:
0 A
1 B
2 A
3 C
dtype: object
In [266]: pd.factorize(y_train)
Out[266]: (array([0, 1, 0, 2], dtype=int64), Index(['A', 'B', 'C'], dtype='object'))
Demo:
In [271]: fct = pd.factorize(y_train)[0]+1
In [272]: fct
Out[272]: array([1, 2, 1, 3], dtype=int64)
Another way is use the astype('category').cat.codes of the dataframe to convert the string values into number
X=df[['User ID', 'Gender', 'Age', 'EstimatedSalary']]
X['Gender']=X['Gender'].astype('category').cat.codes
If you are using sklearn, I would suggest sticking with methods in that library that do these things for you. Sklearn has a number of ways of preprocessing data such as encoding labels. One of which is the sklearn.preprocessing.LabelEncoder
function.
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit_transform(y_train)
Which outputs
array([0, 1, 0, 2])
Use le.inverse_transform([0,1,2])
to map back