I am trying to perform classification in Python using Pandas and scikit-learn. My dataset contains a mix of text variables, numerical variables and categorical variables.
Let's say my dataset looks like this:
Project Cost Project Category Project Description Project Outcome
12392.2 ABC This is a description Fully Funded
493992.4 DEF Stack Overflow rocks Expired
And I need to predict the variable Project Outcome. Here is what I did (assuming df contains my dataset):
I converted the categories
Project CategoryandProject Outcometo numeric valuesdf['Project Category'] = df['Project Category'].factorize()[0] df['Project Outcome'] = df['Project Outcome'].factorize()[0]
Dataset now looks like this:
Project Cost Project Category Project Description Project Outcome
12392.2 0 This is a description 0
493992.4 1 Stack Overflow rocks 1
Then I processed the text column using
TF-IDFtfidf_vectorizer = TfidfVectorizer() df['Project Description'] = tfidf_vectorizer.fit_transform(df['Project Description'])
Dataset now looks something like this:
Project Cost Project Category Project Description Project Outcome
12392.2 0 (0, 249)\t0.17070240732941433\n (0, 304)\t0.. 0
493992.4 1 (0, 249)\t0.17070240732941433\n (0, 304)\t0.. 1
So since all variables are now numerical values, I thought I would be good to go to start training my model
X = df.drop(columns=['Project Outcome'], axis=1) y = df['Project Outcome'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1) model = MultinomialNB() model.fit(X_train, y_train)
But I get the error ValueError: setting an array element with a sequence. when attempting to do the model.fit. When I print X_train, I noticed that Project Description was replaced by NaN for some reason.
Any help on this? Is there a good way to do classification using variables with various data types? Thank you.