Machine Learning for Marketing in Python
Karolis Urbonas
Head of Analytics & Science, Amazon
First, let's load the libraries.
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 1. Split data to training and testing train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.25)
# 2. Initialize the model mytree = tree.DecisionTreeClassifier()
# 3. Fit the model on the training data treemodel = mytree.fit(train_X, train_Y)
# 4. Predict values on the testing data pred_Y = treemodel.predict(test_X)
# 5. Measure model performance on testing data accuracy_score(test_Y, pred_Y)
First, let's load the libraries.
from sklearn.cluster import KMeans
import pandas as pd
1. Initialize the model kmeans = KMeans(n_clusters=3)
# Fit the model kmeans.fit(data)
# 3. Asign cluster values data.assign(Cluster=kmeans.labels_)
# 4. Explore results data.groupby('Cluster').mean()
Machine Learning for Marketing in Python