-
Notifications
You must be signed in to change notification settings - Fork 0
/
Outlier_pairplot_using_Spark_andSeaborn
316 lines (250 loc) · 9.61 KB
/
Outlier_pairplot_using_Spark_andSeaborn
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, when, lit, percentile_approx
# Initialize Spark session
spark = SparkSession.builder.appName("IrisOutliers").getOrCreate()
# Load the Iris dataset into a Spark DataFrame
# For demonstration purposes, using a small dataset
# Replace this with your large dataset
iris = load_iris()
data = iris.data
columns = iris.feature_names
# Convert to Spark DataFrame
df = spark.createDataFrame(pd.DataFrame(data, columns=columns))
# Calculate the 1st and 99th percentiles for each column
percentiles = df.agg(
*[percentile_approx(col(c), [0.01, 0.99]).alias(c) for c in columns]
).collect()[0]
# Create lower and upper bounds dictionaries
lower_bounds = {c: percentiles[c][0] for c in columns}
upper_bounds = {c: percentiles[c][1] for c in columns}
# Add a new column 'category' to categorize data
for c in columns:
df = df.withColumn(
'category',
when(col(c) < lower_bounds[c], 'lower_outlier')
.when(col(c) > upper_bounds[c], 'higher_outlier')
.otherwise(col('category'))
)
# Fill null values with 'normal'
df = df.fillna('normal', subset=['category'])
# Convert to Pandas DataFrame for visualization
pandas_df = df.toPandas()
import seaborn as sns
import matplotlib.pyplot as plt
# Initialize the PairGrid with the 'category' column as hue
g = sns.PairGrid(pandas_df, hue="category", diag_sharey=False)
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.scatterplot)
# Add a legend
g.add_legend()
plt.show()
##################################################################################################################
## Another version using pandas
##################################################################################################################
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# Calculate percentiles
percentiles = df.quantile([0.05, 0.95])
# Function to categorize each value
def categorize_value(val, lower_bound, upper_bound):
return 'outlier' if val < lower_bound or val > upper_bound else 'normal'
# Apply categorization to each row and create a new column 'category'
def categorize_row(row):
for col in df.columns:
lower_bound = percentiles[col].iloc[0]
upper_bound = percentiles[col].iloc[1]
if categorize_value(row[col], lower_bound, upper_bound) == 'outlier':
return 'outlier'
return 'normal'
df['category'] = df.apply(categorize_row, axis=1)
# Initialize the PairGrid with the 'category' column as hue
g = sns.PairGrid(df, hue="category", diag_sharey=False)
g.map_upper(sns.scatterplot)
#g.map_diag(sns.histplot, color=".1") # Uncomment for histograms on diagonal
g.map_lower(sns.kdeplot, color=".3")
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.scatterplot)
# Add a legend
g.add_legend()
#################################################
### with Many variables - create a pair plot where the target variable is based on outliers (values greater than the 99th percentile and less than the 1st percentile)
#####################################################
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Load your dataset (here I'm using a random dataset for demonstration)
np.random.seed(42)
data = np.random.rand(1000, 30)
columns = ['NoOd', 'NoE7', 'Pop', 'Wok', 'HH', 'OwnO', 'OwnM', 'SocR', 'PriR',
'FV0', 'FV1', 'FV2', 'FV3', 'FV4', 'FV5', 'FV6', 'FV7', 'FV8', 'FV9',
'FV10', 'FV11', 'DmA', 'CTA', 'CTB', 'CTC', 'CTD', 'CTE', 'CTF', 'CTG', 'CTH']
df = pd.DataFrame(data, columns=columns)
# Compute the 1st and 99th percentiles
lower_bound = df.quantile(0.01)
upper_bound = df.quantile(0.99)
# Create a new column indicating outliers
df['outlier'] = ((df < lower_bound) | (df > upper_bound)).any(axis=1)
# Map outliers to strings for better visualization
df['outlier'] = df['outlier'].map({True: 'outlier', False: 'normal'})
# Create the pair plot
sns.set(style="ticks")
pair_plot = sns.pairplot(df, hue="outlier", markers=["o", "s"])
# Show the plot
plt.show()
plt.show()
############################################
# Heatmap
############################################
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Example DataFrame
# Replace this with your actual data
np.random.seed(0)
data = np.random.rand(1000, 30)
columns = ['NoOd', 'NoE7', 'Pop', 'Wok', 'HH', 'OwnO', 'OwnM', 'SocR', 'PriR',
'FV0', 'FV1', 'FV2', 'FV3', 'FV4', 'FV5', 'FV6', 'FV7', 'FV8', 'FV9',
'FV10', 'FV11', 'DmA', 'CTA', 'CTB', 'CTC', 'CTD', 'CTE', 'CTF', 'CTG', 'CTH']
df = pd.DataFrame(data, columns=columns)
# Calculate the correlation matrix
corr = df.corr()
# Set up the matplotlib figure
plt.figure(figsize=(12, 10))
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, annot=False, fmt=".2f", cmap='coolwarm', center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5},
vmin=-1, vmax=1)
# Adjust the ticks and labels for better visualization
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.tight_layout()
# Show the plot
plt.show()
###############################
## Heatmap - only lwer diagonal
##############################
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Sample DataFrame
np.random.seed(0)
data = np.random.rand(1000, 30) # Adjusted to 30 columns to match the number of labels
columns = ['NoOd', 'NoE7', 'Pop', 'Wok', 'HH', 'OwnO', 'OwnM', 'SocR', 'PriR',
'FV0', 'FV1', 'FV2', 'FV3', 'FV4', 'FV5', 'FV6', 'FV7', 'FV8', 'FV9',
'FV10', 'FV11', 'DmA', 'CTA', 'CTB', 'CTC', 'CTD', 'CTE', 'CTF', 'CTG', 'CTH']
df = pd.DataFrame(data, columns=columns)
# Calculate the correlation matrix
corr = df.corr()
# Set up the matplotlib figure
plt.figure(figsize=(12, 10))
# Draw the heatmap with a mask and correct aspect ratio
mask = np.triu(np.ones_like(corr, dtype=bool))
heatmap = sns.heatmap(corr, mask=mask, annot=False, fmt=".2f", cmap='coolwarm', center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5}, vmin=-1, vmax=1)
# Customize tick labels
heatmap.set_xticks(np.arange(len(columns)) + 0.5)
heatmap.set_xticklabels(columns, rotation=90)
heatmap.set_yticks(np.arange(len(columns)) + 0.5)
heatmap.set_yticklabels(columns, rotation=0)
plt.tight_layout()
plt.show()
############################
## One more correlogram
############################
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data creation (you should replace this with your actual data)
np.random.seed(0)
data = pd.DataFrame({
'SCST': np.random.rand(100),
'female': np.random.rand(100),
'mmedia': np.random.rand(100),
'poor': np.random.rand(100),
'prop4pl': np.random.rand(100),
'lat': np.random.rand(100),
'never_breastfeed': np.random.rand(100),
'low_bmi': np.random.rand(100),
'hindu': np.random.rand(100),
'mothage_20': np.random.rand(100),
'low_birthintval': np.random.rand(100),
'watersup': np.random.rand(100),
'hh_fem': np.random.rand(100),
'edu': np.random.rand(100),
'ptoilet': np.random.rand(100),
'urban': np.random.rand(100),
'pcfuel': np.random.rand(100),
'vacc': np.random.rand(100)
})
# Calculate the correlation matrix
corr = data.corr()
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(240, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=1, vmin=-1, center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5}, annot=True, fmt=".1f")
# Adjust the title and labels
plt.title('Correlogram')
plt.show()
###################################
## Another one
##################################
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data creation (replace this with your actual data)
np.random.seed(0)
data = pd.DataFrame({
'SCST': np.random.rand(100),
'female': np.random.rand(100),
'mmedia': np.random.rand(100),
'poor': np.random.rand(100),
'prop4pl': np.random.rand(100),
'lat': np.random.rand(100),
'never_breastfeed': np.random.rand(100),
'low_bmi': np.random.rand(100),
'hindu': np.random.rand(100),
'mothage_20': np.random.rand(100),
'low_birthintval': np.random.rand(100),
'watersup': np.random.rand(100),
'hh_fem': np.random.rand(100),
'edu': np.random.rand(100),
'ptoilet': np.random.rand(100),
'urban': np.random.rand(100),
'pcfuel': np.random.rand(100),
'vacc': np.random.rand(100)
})
# Calculate the correlation matrix
corr = data.corr()
# Generate a mask for the lower triangle
mask = np.tril(np.ones_like(corr, dtype=bool))
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(12, 10))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(240, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=1, vmin=-1, center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5}, annot=True, fmt=".1f", annot_kws={"size": 8})
# Rotate the tick labels for better readability
plt.xticks(rotation=90)
plt.yticks(rotation=0)
# Adjust the title and labels
plt.title('Correlogram', fontsize=15)
plt.show()