Skip to content

Instantly share code, notes, and snippets.

@drorata
Created July 24, 2017 06:54
Show Gist options
  • Select an option

  • Save drorata/0151a895cfaa6cd5e50d5faa4ecfe921 to your computer and use it in GitHub Desktop.

Select an option

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
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