ValueError: Number of features of the model must match the input

后端 未结 5 1240
谎友^
谎友^ 2020-12-14 12:11

I\'m getting this error when trying to predict using a model I built in scikit learn. I know that there are a bunch of questions about this but mine seems different from the

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 13:12

    The reason you're getting the error is due to the different distinct values in your features where you're generating the dummy values with get_dummies.

    Let's suppose the Word_1 column in your training set has the following distinct words: the, dog, jumps, roof, off. That's 5 distinct words so pandas will generate 5 features for Word_1. Now, if your scoring dataset has a different number of distinct words in the Word_1 column, then you're going to get a different number of features.

    How to fix:

    You'll want to concatenate your training and scoring datasets using concat, apply get_dummies, and then split your datasets. That'll ensure you have captured all the distinct values in your columns. Given that you're using two different csv's, you probably want to generate a column that specifies your training vs scoring dataset.

    Example solution:

    train_df = pd.read_csv("Cinderella.csv")
    train_df['label'] = 'train'
    
    score_df = pandas.read_csv("Slaughterhouse_copy.csv")
    score_df['label'] = 'score'
    
    # Concat
    concat_df = pd.concat([train_df , score_df])
    
    # Create your dummies
    features_df = pd.get_dummies(concat_df, columns=['Overall_Sentiment', 'Word_1','Word_2','Word_3','Word_4','Word_5','Word_6','Word_7','Word_8','Word_9','Word_10','Word_11','Word_1','Word_12','Word_13','Word_14','Word_15','Word_16','Word_17','Word_18','Word_19','Word_20','Word_21','Word_22','Word_23','Word_24','Word_25','Word_26','Word_27','Word_28','Word_29','Word_30','Word_31','Word_32','Word_33','Word_34','Word_35','Word_36','Word_37','Word_38','Word_39','Word_40','Word_41', 'Word_42', 'Word_43'], dummy_na=True)
    
    # Split your data
    train_df = features_df[features_df['label'] == 'train']
    score_df = features_df[features_df['label'] == 'score']
    
    # Drop your labels
    train_df = train_df.drop('label', axis=1)
    score_df = score_df.drop('label', axis=1)
    
    # Now delete your 'slope' feature, create your features matrix, and create your model as you have already shown in your example
    ...
    

提交回复
热议问题