Příprava dat pro segmentaci

Machine Learning for Marketing in Python

Karolis Urbonas

Head of Analytics & Science, Amazon

Předpoklady modelu

  • Začneme s K-means
  • K-means funguje dobře, když jsou data 1) přibližně normálně rozdělena (bez šikmosti) a 2) standardizována (průměr = 0, směrodatná odchylka = 1)
  • Druhý model – NMF – lze použít na surová data, zejména pokud je matice řídká
Machine Learning for Marketing in Python

Odstranění šikmosti logaritmickou transformací

# First option - log transformation
wholesale_log = np.log(wholesale)
sns.pairplot(wholesale_log, diag_kind='kde')
plt.show()
Machine Learning for Marketing in Python

Průzkum logaritmicky transformovaných dat

Pairplot s logaritmickou transformací

Machine Learning for Marketing in Python

Odstranění šikmosti transformací Box-Cox

# Second option - Box-Cox transformation
from scipy import stats

def boxcox_df(x):
    x_boxcox, _ = stats.boxcox(x)
    return x_boxcox

wholesale_boxcox = wholesale.apply(boxcox_df, axis=0)
sns.pairplot(wholesale_boxcox, diag_kind='kde')
plt.show()
Machine Learning for Marketing in Python

Průzkum dat transformovaných metodou Box-Cox

Pairplot Box-Cox

Machine Learning for Marketing in Python

Škálování dat

  • Odečtěte průměr sloupce od každé hodnoty
  • Vydělte každou hodnotu směrodatnou odchylkou sloupce
  • Použijeme modul StandardScaler() z sklearn
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

scaler.fit(wholesale_boxcox) wholesale_scaled = scaler.transform(wholesale_boxcox) wholesale_scaled_df = pd.DataFrame(data=wholesale_scaled, index=wholesale_boxcox.index, columns=wholesale_boxcox.columns) wholesale_scaled_df.agg(['mean','std']).round()
      Fresh  Milk  Grocery  Frozen  Detergents_Paper  Delicassen
mean   -0.0   0.0      0.0     0.0              -0.0         0.0
std     1.0   1.0      1.0     1.0               1.0         1.0
Machine Learning for Marketing in Python

Pojďme si procvičit!

Machine Learning for Marketing in Python

Preparing Video For Download...