Guest User

Untitled

a guest
Mar 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def scale_df(df,axis=0):
  2. return (df - df.mean(axis=axis)) / df.std(axis=axis)
  3.  
  4. def plot_hmap(df, ix=None, cmap='bwr'):
  5. if ix is None:
  6. ix = np.arange(df.shape[0])
  7. plt.imshow(df.iloc[ix,:], cmap=cmap)
  8. plt.colorbar(fraction=0.03)
  9. plt.yticks(np.arange(df.shape[0]), df.index[ix])
  10. plt.xticks(np.arange(df.shape[1]))
  11. plt.grid(False)
  12. plt.show()
  13.  
  14. def scale_and_plot(df, ix = None):
  15. df_marginal_scaled = scale_df(df.T).T
  16. if ix is None:
  17. ix = AC(4).fit(df_marginal_scaled).labels_.argsort() # a trick to make better heatmaps
  18. cap = np.min([np.max(df_marginal_scaled.as_matrix()), np.abs(np.min(df_marginal_scaled.as_matrix()))])
  19. df_marginal_scaled = np.clip(df_marginal_scaled, -1*cap, cap)
  20. plot_hmap(df_marginal_scaled, ix=ix)
  21.  
  22. def normalize(df):
  23. result = df.copy()
  24. for feature_name in df.columns:
  25. max_value = df[feature_name].max()
  26. min_value = df[feature_name].min()
  27. result[feature_name] = (df[feature_name] - min_value) / (max_value - min_value)
  28. return result
  29.  
  30. dayofweek_by_location = df.pivot_table(values='ID', index='Location.Description', columns=df.index.dayofweek, aggfunc=np.size).fillna(0)
  31. dayofweek_by_type = df.pivot_table(values='ID', index='Primary.Type', columns=df.index.dayofweek, aggfunc=np.size).fillna(0)
  32. location_by_type = df.pivot_table(values='ID', index='Location.Description', columns='Primary.Type', aggfunc=np.size).fillna(0)
  33.  
  34. from sklearn.cluster import AgglomerativeClustering as AC
  35.  
  36. plt.figure(figsize=(17,17))
  37. scale_and_plot(dayofweek_by_type)
Add Comment
Please, Sign In to add comment