Created
July 24, 2017 06:54
-
-
Save drorata/0151a895cfaa6cd5e50d5faa4ecfe921 to your computer and use it in GitHub Desktop.
Small utility that predicts the class of an instance (one row) from a pandas.DataFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def predict_row(row, clf): | |
| """ | |
| Transform row to a 1-row pandas.DataFrame and predict | |
| When iterating of rows of a DataFrame, each row is represented as a | |
| pd.Series. The classifiers in use are expecting a DataFrame. This function | |
| turns the row into a 1 x n_featurs DataFrame and apply the prediction of a | |
| trained classier. | |
| Parameters | |
| ---------- | |
| row : pandas.Series | |
| An instance from the data. | |
| clf | |
| Predictor which expects the features which are the index of ``row`` | |
| and is already trained | |
| Returns | |
| ------- | |
| int | |
| Value of the prediction | |
| """ | |
| return clf.predict( | |
| pd.DataFrame(row).T | |
| )[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment