Commit
•
15f79b5
1
Parent(s):
6988e01
Create plots.py
Browse files
plots.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import seaborn as sns
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Combine "combined_df.csv" and "combined_val_df.csv" into one dataframe
|
7 |
+
df = pd.concat([pd.read_csv('phishing_features_train.csv'), pd.read_csv('phishing_features_val.csv')], ignore_index=True)
|
8 |
+
|
9 |
+
# Define the columns to plot
|
10 |
+
columns_to_plot = ['redirects', 'not_indexed_by_google', 'issuer', 'certificate_age', 'email_submission', 'request_url_percentage', 'url_anchor_percentage', 'meta_percentage', 'script_percentage', 'link_percentage', 'mouseover_changes', 'right_click_disabled', 'popup_window_has_text_field', 'use_iframe', 'has_suspicious_port', 'external_favicons', 'TTL', 'ip_address_count', 'TXT_record', 'check_sfh', 'count_domain_occurrences', 'domain_registeration_length', 'abnormal_url', 'age_of_domain', 'page_rank_decimal']
|
11 |
+
|
12 |
+
# Create a list to store the file names of the saved plots
|
13 |
+
file_names = []
|
14 |
+
|
15 |
+
# Loop through the columns and create the scatterplot or barplot
|
16 |
+
for column in columns_to_plot:
|
17 |
+
if df[column].dtype == 'int64' or df[column].dtype == 'float64':
|
18 |
+
fig, ax = plt.subplots()
|
19 |
+
sns.regplot(x=column, y='is_malicious', data=df, ax=ax)
|
20 |
+
corr_coef = df[[column, 'is_malicious']].corr().iloc[0,1]
|
21 |
+
ax.set_title(f'{column} vs is_malicious\nCorrelation Coefficient: {corr_coef:.2f}')
|
22 |
+
file_name = f'{column}_scatterplot.png'
|
23 |
+
plt.savefig(file_name)
|
24 |
+
file_names.append(file_name)
|
25 |
+
elif df[column].dtype == 'object':
|
26 |
+
fig, ax = plt.subplots()
|
27 |
+
if (df[column] == "None").sum() > 0:
|
28 |
+
sns.countplot(x=column, hue='is_malicious', data=df[df[column] == "None"], ax=ax)
|
29 |
+
ax.set_title(f'{column} (null) vs is_malicious')
|
30 |
+
file_name = f'{column}_null_barplot.png'
|
31 |
+
plt.savefig(file_name)
|
32 |
+
file_names.append(file_name)
|
33 |
+
sns.countplot(x=column, hue='is_malicious', data=df, ax=ax)
|
34 |
+
ax.set_title(f'{column} (all) vs is_malicious')
|
35 |
+
file_name = f'{column}_all_barplot.png'
|
36 |
+
plt.savefig(file_name)
|
37 |
+
file_names.append(file_name)
|
38 |
+
|
39 |
+
# Create a figure with subplots to combine the saved plots
|
40 |
+
num_plots = len(file_names)
|
41 |
+
num_rows = int(np.ceil(num_plots/2))
|
42 |
+
fig, axs = plt.subplots(num_rows, 2, figsize=(20, 5*num_rows))
|
43 |
+
for i, file_name in enumerate(file_names):
|
44 |
+
row = i // 2
|
45 |
+
col = i % 2
|
46 |
+
img = plt.imread(file_name)
|
47 |
+
axs[row, col].imshow(img)
|
48 |
+
axs[row, col].axis('off')
|
49 |
+
if num_plots % 2 == 1:
|
50 |
+
axs[num_rows-1, 1].axis('off')
|
51 |
+
plt.tight_layout()
|
52 |
+
plt.savefig('correlation_coefficient.png')
|