Created
December 3, 2015 07:36
-
-
Save danieljfarrell/90aefee59c5292740f0b to your computer and use it in GitHub Desktop.
Convert a feature-list Pandas data frame to a feature-matrix.
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
import pandas as pd | |
from sklearn.preprocessing import MultiLabelBinarizer | |
# Feature-list data frame | |
df = pd.DataFrame(columns = ["features"], index=['Item 1', 'Item 2']) | |
df['features'] = [["A", "B"], ["C", "D"]] | |
# Use scikits-learn to create feature matrix and feature names | |
mlb = MultiLabelBinarizer() | |
feature_column_name = 'features' | |
feature_matrix = mlb.fit_transform(df[feature_column_name]) | |
feature_names = mlb.classes_ | |
# Create feature-martrix data frame | |
feature_df = pd.DataFrame(data=feature_matrix, columns=feature_names, index=df.index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The binarizer approach came from a answer to my question posted here, http://stackoverflow.com/questions/34052903/forming-a-sparse-feature-matrix-data-frame-in-pandas/34053068#34053068