6
$\begingroup$

I am new to data science and trying get some results. I'm applying Decision Tree Classifier. When my train and test datasets' size are not equal I get an error `Number of features of the model must match the input. Model n_features is N (no. of entries in training datasets) and input n_features is X (no. of entries in test datasets).

If I have 100 entries in my dataset and parameter for split is test_size=0.30 as:

import pandas as pd
from pandas import Series, DataFrame
import numpy as np
from sklearn import tree
from sklearn.model_selection import train_test_split



data=pd.read_csv("ndata.csv")

X_train, X_test, y_train, y_test = train_test_split(data.dis, data.gen, test_size=0.30, random_state=42)


c = tree.DecisionTreeClassifier()

y_test_size = y_test.size
y_train_size = y_train.size
X_train = [X_train]
y_train = [y_train]
X_test = [X_test]
y_test = [y_test]

c.fit(X_train, y_train)

accu_train = np.sum(c.predict(X_train) == y_train)/y_train_size
accu_test = np.sum(c.predict(X_test) == y_test)/y_test_size

print("Accuracy on Train: ", accu_train)
print("Accuracy on Test: ", accu_test)

And the error occurs as follows:

ValueError                                Traceback (most recent call last)
<ipython-input-33-f6cc77390526> in <module>()
     24 
     25 accu_train = np.sum(c.predict(X_train) == y_train)/y_train_size
---> 26 accu_test = np.sum(c.predict(X_test) == y_test)/y_test_size
     27 
     28 print("Accuracy on Train: ", accu_train)

~/anaconda3/lib/python3.6/site-packages/sklearn/tree/tree.py in predict(self, X, check_input)
    410         """
    411         check_is_fitted(self, 'tree_')
--> 412         X = self._validate_X_predict(X, check_input)
    413         proba = self.tree_.predict(X)
    414         n_samples = X.shape[0]

~/anaconda3/lib/python3.6/site-packages/sklearn/tree/tree.py in _validate_X_predict(self, X, check_input)
    382                              "match the input. Model n_features is %s and "
    383                              "input n_features is %s "
--> 384                              % (self.n_features_, n_features))
    385 
    386         return X

ValueError: Number of features of the model must match the input. Model n_features is 70 and input n_features is 30 

Link to data file: https://gist.github.com/mutafaf/7715ad67bc3cf4e08985afefcc0ce08a#file-ndata-csv

Why do I'm getting this error. Is it necessary to have dataset size of both train and test equal?

$\endgroup$
1
  • $\begingroup$ Just a quick question, I tried the implementation in the answer I tried converting avgw2v and tfidfw2v vectors into array as I was getting the same problem mentioned in the question. The fact is model's performance had dropped drastically with this implementation. My roc score is just 60 and its was 85 when I didnot convert the vectors into array. Any reason for this massive change in performance? $\endgroup$ Jan 25, 2019 at 16:20

1 Answer 1

8
$\begingroup$

You are supposed to pass numpy arrays and not lists as arguments to the DecisionTree, since your input was a list it gets trained as 70 features (1D list) and your test had list of 30 elements and the classifier sees it as 30 features.

Nonetheless, you need to reshape your input numpy array and pass it as a matrix

meaning: X_train.values.reshape(-1, 1) instead of X_train (it should be a numpy array not a list)

this is the entire gist:

data=pd.read_csv("ndata.csv")

X_train, X_test, y_train, y_test = train_test_split(data.dis, data.gen, test_size=0.30, random_state=42)

from sklearn import tree

c = tree.DecisionTreeClassifier()
c.fit(X_train.values.reshape(-1, 1), y_train)

accu_train = np.sum(c.predict(X_train.values.reshape(-1, 1)) == y_train)/y_train_size
accu_test = np.sum(c.predict(X_test.values.reshape(-1, 1)) == y_test)/y_test_size

print("Accuracy on Train: ", accu_train)
print("Accuracy on Test: ", accu_test)

I'm getting the following output:

Accuracy on Train:  0.8857142857142857
Accuracy on Test:  0.7333333333333333

Thanks for sharing the dataset. It was helpful for testing.

$\endgroup$
1
  • $\begingroup$ Thanks @tenshi. That solves my problem.Thanks for guiding. $\endgroup$
    – Mutafaf
    Jul 3, 2018 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.