1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
- import pickle
- import warnings
- from typing import Tuple
- import matplotlib
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- from omegaconf import DictConfig
- from prefect import flow, task
- from sklearn.cluster import KMeans
- from sklearn.decomposition import PCA
- from yellowbrick.cluster import KElbowVisualizer
- from helper import create_parent_directory, load_config
- warnings.simplefilter(action="ignore", category=DeprecationWarning)
- @task
- def read_process_data(config: DictConfig):
- return pd.read_csv(config.intermediate.path)
- @task
- def get_pca_model(data: pd.DataFrame) -> PCA:
- pca = PCA(n_components=3)
- pca.fit(data)
- return pca
- @task
- def reduce_dimension(df: pd.DataFrame, pca: PCA) -> pd.DataFrame:
- return pd.DataFrame(pca.transform(df), columns=["col1", "col2", "col3"])
- @task
- def get_3d_projection(pca_df: pd.DataFrame) -> dict:
- """A 3D Projection Of Data In The Reduced Dimensionality Space"""
- return {"x": pca_df["col1"], "y": pca_df["col2"], "z": pca_df["col3"]}
- @task
- def get_best_k_cluster(
- pca_df: pd.DataFrame, config: DictConfig
- ) -> pd.DataFrame:
- matplotlib.use("svg")
- fig = plt.figure(figsize=(10, 8))
- fig.add_subplot(111)
- elbow = KElbowVisualizer(KMeans(), metric="distortion")
- elbow.fit(pca_df)
- create_parent_directory(config.image.kmeans)
- elbow.fig.savefig(config.image.kmeans)
- k_best = elbow.elbow_value_
- return k_best
- @task
- def get_clusters_model(
- pca_df: pd.DataFrame, k: int
- ) -> Tuple[pd.DataFrame, pd.DataFrame]:
- model = KMeans(n_clusters=k)
- # Fit model
- return model.fit(pca_df)
- @task
- def predict(model, pca_df: pd.DataFrame):
- return model.predict(pca_df)
- @task
- def insert_clusters_to_df(
- df: pd.DataFrame, clusters: np.ndarray
- ) -> pd.DataFrame:
- return df.assign(clusters=clusters)
- @task
- def plot_clusters(
- pca_df: pd.DataFrame,
- preds: np.ndarray,
- projections: dict,
- config: DictConfig,
- ) -> None:
- pca_df["clusters"] = preds
- matplotlib.use("svg")
- plt.figure(figsize=(10, 8))
- ax = plt.subplot(111, projection="3d")
- ax.scatter(
- projections["x"],
- projections["y"],
- projections["z"],
- s=40,
- c=pca_df["clusters"],
- marker="o",
- cmap="Accent",
- )
- ax.set_title("The Plot Of The Clusters")
- plt.savefig(config.image.clusters)
- @task
- def save_data_and_model(data: pd.DataFrame, model: KMeans, config: DictConfig):
- create_parent_directory(config.final.path)
- data.to_csv(config.final.path, index=False)
- create_parent_directory(config.model.path)
- pickle.dump(model, open(config.model.path, "wb"))
- @flow(name="Segment customers")
- def segment() -> None:
- config = load_config()
- data = read_process_data(config)
- pca = get_pca_model(data)
- pca_df = reduce_dimension(data, pca)
- projections = get_3d_projection(pca_df)
- k_best = get_best_k_cluster(pca_df, config)
- model = get_clusters_model(pca_df, k_best)
- pred = predict(model, pca_df)
- data = insert_clusters_to_df(data, pred)
- plot_clusters(
- pca_df,
- pred,
- projections,
- config,
- )
- save_data_and_model(data, model, config)
- if __name__ == "__main__":
- segment()
|