-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_plots.py
112 lines (89 loc) · 4.24 KB
/
dataset_plots.py
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
import json
from pathlib import Path
from utils import get_plot_folder
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
def build_info(file_name, meta_features):
"""Builds the info of a dataset in a presentable manner"""
info = {'dataset': int(file_name[:-5])}
for feat_name, value in meta_features.items():
new_name = feat_name.replace("_", " ")
info[new_name] = value
return info
def extract_feature(task):
"""Extracts a feature from an specific task info"""
info_folder = Path('results/datasets_info') / task
data_dict = []
for fn in info_folder.glob('*.json'):
info = json.load(open(fn, 'r+'))
data_dict.append(build_info(fn.name, info['meta_features']))
return pd.DataFrame(data_dict)
def plot_numerical_values(data, prop_name):
"""Plots numerical values in a kde format"""
# sns.displot(data=data[prop_name], kind='kde')
plt.figure(prop_name)
sns.kdeplot(x=prop_name, data=data, shade=True)
def plot_categorical_values(data, prop_name):
"""Plots categorical values counting values"""
plt.figure(prop_name)
sns.countplot(x=prop_name, data=data, palette='rainbow')
def plot_values(data, values, plot_folder, type_):
"""Plots categorical or numeric values"""
for value in values:
if type_ == 'numerical':
plot_numerical_values(data, value)
elif type_ == 'categorical':
try:
plot_categorical_values(data, value)
except:
print(value)
plt.savefig(plot_folder / f'{value}.pdf')
plt.close()
def plot_scatterplot(data, plot_folder):
"""Scatter plot of #instances-#features, #instances-#classes (auto-sklearn style)"""
fig, axs = plt.subplots(nrows=2)
sns.scatterplot(data=data, x='input dimensionality', y='number of samples', ax=axs[0])
sns.scatterplot(data=data, x='input dimensionality', y='number of classes', ax=axs[1])
plt.savefig(plot_folder / f'scatterplot.pdf')
plt.close()
def plot_lineplot(data, plot_folder):
"""Line plot of #instances, #features, #classes - dataset id (atm style)"""
fig, axs = plt.subplots(nrows=3, figsize=(9, 7))
datasets_number = range(len(data))
sns.lineplot(data=data, x=datasets_number, y='number of samples', ax=axs[0])
axs[0].set_ylabel('Número de Instancias')
sns.lineplot(data=data, x=datasets_number, y='input dimensionality', ax=axs[1])
axs[1].set_ylabel('Número de Características')
logaritmic = np.log2(data['number of classes'])
sns.lineplot(x=datasets_number, y=logaritmic, ax=axs[2])
axs[2].set_ylabel('Número de Clases')
plt.savefig(plot_folder / 'lineplot.pdf')
plt.close()
def main():
"""Does all main stuff"""
numerical_features = ['number of samples', 'input dimensionality',
'output dimensionality', 'dataset dimensionality', 'standard deviation',
'coefficient of variation', 'covariance avg', 'linear corr coef',
'skewness', 'skewness 1', 'skewness 2', 'skewness 3', 'kurtosis',
'kurtosis 1', 'kurtosis 2', 'kurtosis 3', 'normalized class entropy',
'normalized attr entropy', 'normalized attr entropy 1',
'normalized attr entropy 2', 'normalized attr entropy 3',
'joint entropy', 'joint entropy 1', 'joint entropy 2',
'joint entropy 3', 'mutual information', 'equivalent number of attr',
'noise signal ratio']
categorical_features = ['is supervised', 'has numeric features',
'average number of words', 'has text features', 'semantic input types',
'semantic output types']
df = extract_feature('Classification') # build dataframe with all datasets characteristics
plt_folder = get_plot_folder('plots/meta_features')
plot_scatterplot(df, plt_folder)
plot_lineplot(df, plt_folder)
# Plots numerical values
# plot_values(df, numerical_features, plt_folder, 'numerical')
#
# # Plots categorical values
# plot_values(df, categorical_features, plt_folder, 'categorical')
if __name__ == '__main__':
main()