减少sns.pairplot()中的绘图数量

时间:2018-12-11 04:51:12

标签: python tensorflow matplotlib pycharm seaborn

我正在遵循this website中的教程,并试图形象化顺序模型df

这是我的代码:

# LAST MODIFIED: December 10, 2018
# NOTE: The loss function is used to optimize your model. This is the function that will get minimized by the optimizer.
#       A metric is used to judge the performance of your model. This is only for you to look at and has nothing to do
#       with the optimization process.
#       "val" refers to "validation" AKA testing dataset

# LIBRARY AND PACKAGE IMPORTING
from __future__ import absolute_import, division, print_function

import matplotlib
from mpl_toolkits.mplot3d import Axes3D
import tensorflow as tf
from tensorflow import keras
from ann_visualizer.visualize import ann_viz;
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# VARIABLE DECLARATIONS
TFVersion = tf.__version__
newline = "\n"
noisePatience = 400
# Hyper-parameters
EPOCHS = 1000
learningRate = 0.001
HLneuronFrequency = [128, 64]
trainDataPercentage = 80
testDataPercentage = 100 - trainDataPercentage


# FUNCTION DEFINITIONS
def ThreeDimenDisplay(x_axis_data, y_axis_data, z_axis_data):
    # THE 3D PLOTS
    # Make the plot
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_trisurf(y_axis_data, x_axis_data, z_axis_data, cmap=plt.cm.viridis, linewidth=0.2)
    plt.show()
    # to Add a color bar which maps values to colors.
    surf = ax.plot_trisurf(y_axis_data, x_axis_data, z_axis_data, cmap=plt.cm.viridis, linewidth=0.2)
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()
    # Rotate it
    ax.view_init(30, 45)
    plt.show()
    # Other palette
    ax.plot_trisurf(y_axis_data, x_axis_data, z_axis_data, cmap=plt.cm.jet, linewidth=0.01)
    plt.show()


# CONFIGURATIONS ------------------------

# SOFTWARE CHECKING
print(newline)
print("Current version of TensorFlow: ", TFVersion)

# FOR BEST-VISUALIZATIONS (OVERWRITES MATPLOTLIB)
sns.set()

# Downloading the Boston Housing Data Set - it is already present in the keras
# NOTE: This will be referred to as the "BHD"
boston_housing = keras.datasets.boston_housing

# Initializing the training + testing data and labels as per the information suggested in the BHD
(train_data, train_labels), (test_data, test_labels) = boston_housing.load_data()

# Shuffle the training set in order to assure randomness - this condition is required for any statistical analysis
order = np.argsort(np.random.random(train_labels.shape))
train_data = train_data[order]
train_labels = train_labels[order]

# Printing the training and testing data sets (the .shape member function gets the examples and feature frequency
# from the train_data vector instance)
print("Training set: {}".format(train_data.shape))  # 404 examples, 13 features
print("Testing set:  {}".format(test_data.shape))  # 102 examples, 13 features

# Initializing the variables/attributes for the data-set
column_names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',
                'TAX', 'PTRATIO', 'B', 'LSTAT']
Pcolumn_names = []
df = pd.DataFrame(train_data, columns=column_names)
df.head()

sns.pairplot(df)

当我当前运行上述代码时,我获得了大量的比较列表,其中包含一些琐碎的数字: Output of Python Run Command

由于这一次显然有太多数据,我如何减少图的数量并真正知道该函数实际比较的是什么(即x和y轴)?

1 个答案:

答案 0 :(得分:0)

您可以在数据框中选择一些列,然后进行配对图。如果选中seaborn documentation,您会发现可以使用 vars 参数完成此操作:

sns.pairplot(df, vars=[columns_names])

如果您不知道要选择哪一列,并且想成对绘制所有变量,则可以对数据框的列名称进行组合,但要记住,最终结果会很多(n!/ r!/( nr)!)的情节:

from itertools import combinations
number_of_variables = 6
for columns_names in combinations(df.columns, number_of_variables):
    sns.pairplot(df, vars=[columns_names])