Context: I'm familiar with the basics of Random Forests, and I'm currently implementing a random forest classifier to make predictions on new proteins using sklearn's
RandomForestClassifier. The problem is that the proteins I want to make predictions on (new 'testing' data, not part of original matrix) usually have a different number of features than the training data I used to create/train the prediction model(classifier). There is overlap between the features, but sklearn expects the number of features to be the same between the trained model and the new feature matrix(protein) that I want to predict on, as evidenced by the error:
ValueError: Number of features of the model must match the input. Model n_features is 256 and input n_features is 75
In the the above example, there was overlap between 45 features. With small datasets, I could retrain the prediction model on just the overlapping features, for each new feature matrix, and then predict away. However, training is the major bottleneck and typically takes over a day on the largest dataset, so retraining for each new feature matrix(protein) is not feasible.
sklearn.externals.joblib to save and load the trained model, but I haven't found a way to modify/transform the model so that the number of features match. Something else I considered was to re-size the new feature matrix to be the same number of features as the model and then use some methods to fill in the missing values. However, I'm not sure how to do this with sklearn.
Problem: How can I use sklearn's
RandomForestClassifier to predict on new feature matrices that have a different number of features, some of which are overlapping features that I could to make predictions, without retraining (due to time)?
Clarification: This isn't a problem that can be solved with regular methods that work on data with the same number of features, such as cross validation.
Relevant Training Code:
##train.py: create classifier model_clf = RandomForestClassifier(n_estimators=10) model_clf = model_clf.fit(X_train, y_train) # X_train (100x100), y_train (100,) # Save model to binary file joblib.dump(model_clf, 'rf.model')
Relevant Prediction Code:
##predict.py: load trained model and use it to predict on X_test feature matrix model_clf = joblib.load('rf.model') #X_test: (50,50), 25 overlapping features with X_train from train.py y_score = model_clf.predict_proba(X_test)[:,1]