ML 建模步骤

Python 营销中的机器学习

Karolis Urbonas

Head of Analytics & Science, Amazon

监督学习步骤

  1. 将数据划分为训练集和测试集
  2. 初始化模型
  3. 在训练集上训练模型
  4. 在测试集上预测
  5. 在测试集上评估模型性能
Python 营销中的机器学习

监督学习:代码

首先,加载库。

from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
Python 营销中的机器学习

监督学习步骤:代码

# 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)
Python 营销中的机器学习

无监督学习步骤

  1. 初始化模型
  2. 训练模型
  3. 分配簇标签
  4. 探索结果
Python 营销中的机器学习

无监督学习:代码

首先,加载库。

from sklearn.cluster import KMeans
import pandas as pd
Python 营销中的机器学习

无监督学习:代码

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()
Python 营销中的机器学习

开始构建模型吧!

Python 营销中的机器学习

Preparing Video For Download...