0
$\begingroup$

I have a dataset and on that, I have performed OneHotEncoding and Standardization using standard scalar, Now that I have preprocessed data I have to visualize it, but on converting it to pandas dataframe it is showing error. What is the way to visualize preprocessed data?

steps that I have taken

df = pd.read_csv(filepath)

categorical_features = [column names which are categorical in nature]

numerical_features = [column names which are numerical in nature]

one_hot = OneHotEncoder()
scaler = StandardScaler()

tranformer = ColumnTransformer([("one_hot", one_hot, categorial_features),("standard_scaler", scaler, numerical_features)], remainder = "passthrough")

transformed_X = tranformer.fit_transform(X)

Now, how to view this transformed_X in a tabular manner that we see using the .head() function of pandas dataframe?

$\endgroup$

1 Answer 1

1
$\begingroup$

Try this,

col_names = list(tranformer.named_transformers_['one_hot'].get_feature_names())+numerical_features

df1 = pd.DataFrame.sparse.from_spmatrix(transformed_X)
df1.columns = col_names
df1.head()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.