I want to normalize my both categorical and numeric values.
cols = df.columns.values.tolist()
df_num = df.drop(CAT_COLUMNS, axis=1)
df_num = df_num.as_matrix()
df_num = preprocessing.StandardScaler().fit_transform(df_num)
df.fillna('NA', inplace=True)
df_cat = df.T.to_dict().values()
vec_cat = DictVectorizer( sparse=False )
df_cat = vec_cat.fit_transform(df_cat)
After that I need to combine 2 numpy arrays back to pandas dataframe, but below approach doesn't work for me.
mas = np.hstack((df_num, df_cat))
df = pd.DataFrame(data=mas, columns=cols)
Error Message: ValueError: Shape of passed values is (475, 243), indices imply (83, 243)
One more approach:
columns = df.columns.values.tolist()
for col in columns:
try:
if col in CAT_COLUMNS:
df[col] = pd.get_dummies(df[col])
else:
df[col] = df[col].apply(preprocessing.StandardScaler().fit)
except Exception, err:
print 'Column: %s and msg=%s' % (col, err.message)
Error Message:
Column: DATE and msg=Singleton array array(1444424400.0) cannot be considered a valid collection.
Column: QTR_HR_START and msg=Singleton array array(21600000L, dtype=int64) cannot be considered a valid collection.
...
PS. Is there any way to avoid numpy et all? As example, I want to leverage on
pandas_mllibrary
pandas_ml, and here all calculations based on pandas