当数据集具有 NaN 值时如何处理 seaborn pairplot 错误?
How to handle seaborn pairplot errors when the dataset has NaN values?
我有一个 pandas DataFrame,其中多列填充了数字和行,第一列包含分类数据。显然,我在多行(当然不是整个空白行)和不同的列中有 NaN 值和零。
这些行在其他列中具有非 NaN 的有价值数据。并且这些列在其他行中有有价值的数据,这些数据也不是 NaN。
问题是 sns.pairplot
不会忽略相关的 NaN 值和 returns 错误(例如被零除、字符串到浮点数的转换等)。
我看到有人说要使用 fillna()
方法,但我希望如果有人知道更优雅的方法来做到这一点,而不必通过该解决方案并花费大量时间来修复情节,轴,过滤器等之后。我不喜欢这种解决方法。
这与此人报告的内容相似:
https://github.com/mwaskom/seaborn/issues/1699
ZeroDivisionError: 0.0 cannot be raised to a negative power
这是示例数据集:
Seaborn 的 PairGrid
功能将允许您创建您想要的情节。 PairGrid
比 sns.pairplot
灵活得多。创建的任何 PairGrid
都包含三个部分:上三角、下三角和对角线。
对于每个部分,您可以定义一个自定义的绘图函数。上三角和下三角部分可以采用任何接受两个特征数组(例如 plt.scatter
)以及任何相关关键字(例如 marker
)的绘图函数。对角线部分接受一个绘图函数,该函数除了相关关键字之外还具有单个特征数组作为输入(例如 plt.hist
)。
为了您的目的,您可以过滤掉自定义函数中的 NaN:
from sklearn import datasets
import pandas as pd
import numpy as np
import seaborn as sns
data = datasets.load_iris()
iris = pd.DataFrame(data.data, columns=data.feature_names)
# break iris dataset to create NaNs
iris.iat[1, 0] = np.nan
iris.iat[4, 0] = np.nan
iris.iat[4, 2] = np.nan
iris.iat[5, 2] = np.nan
# create customized scatterplot that first filters out NaNs in feature pair
def scatterFilter(x, y, **kwargs):
interimDf = pd.concat([x, y], axis=1)
interimDf.columns = ['x', 'y']
interimDf = interimDf[(~ pd.isnull(interimDf.x)) & (~ pd.isnull(interimDf.y))]
ax = plt.gca()
ax = plt.plot(interimDf.x.values, interimDf.y.values, 'o', **kwargs)
# Create an instance of the PairGrid class.
grid = sns.PairGrid(data=iris, vars=list(iris.columns), size = 4)
# Map a scatter plot to the upper triangle
grid = grid.map_upper(scatterFilter, color='darkred')
# Map a histogram to the diagonal
grid = grid.map_diag(plt.hist, bins=10, edgecolor='k', color='darkred')
# Map a density plot to the lower triangle
grid = grid.map_lower(scatterFilter, color='darkred')
这将产生以下情节:
PairPlot
允许您绘制等高线图、使用描述性统计数据注释面板等。有关详细信息,请参阅 here。
我有一个 pandas DataFrame,其中多列填充了数字和行,第一列包含分类数据。显然,我在多行(当然不是整个空白行)和不同的列中有 NaN 值和零。
这些行在其他列中具有非 NaN 的有价值数据。并且这些列在其他行中有有价值的数据,这些数据也不是 NaN。
问题是 sns.pairplot
不会忽略相关的 NaN 值和 returns 错误(例如被零除、字符串到浮点数的转换等)。
我看到有人说要使用 fillna()
方法,但我希望如果有人知道更优雅的方法来做到这一点,而不必通过该解决方案并花费大量时间来修复情节,轴,过滤器等之后。我不喜欢这种解决方法。
这与此人报告的内容相似:
https://github.com/mwaskom/seaborn/issues/1699
ZeroDivisionError: 0.0 cannot be raised to a negative power
这是示例数据集:
Seaborn 的 PairGrid
功能将允许您创建您想要的情节。 PairGrid
比 sns.pairplot
灵活得多。创建的任何 PairGrid
都包含三个部分:上三角、下三角和对角线。
对于每个部分,您可以定义一个自定义的绘图函数。上三角和下三角部分可以采用任何接受两个特征数组(例如 plt.scatter
)以及任何相关关键字(例如 marker
)的绘图函数。对角线部分接受一个绘图函数,该函数除了相关关键字之外还具有单个特征数组作为输入(例如 plt.hist
)。
为了您的目的,您可以过滤掉自定义函数中的 NaN:
from sklearn import datasets
import pandas as pd
import numpy as np
import seaborn as sns
data = datasets.load_iris()
iris = pd.DataFrame(data.data, columns=data.feature_names)
# break iris dataset to create NaNs
iris.iat[1, 0] = np.nan
iris.iat[4, 0] = np.nan
iris.iat[4, 2] = np.nan
iris.iat[5, 2] = np.nan
# create customized scatterplot that first filters out NaNs in feature pair
def scatterFilter(x, y, **kwargs):
interimDf = pd.concat([x, y], axis=1)
interimDf.columns = ['x', 'y']
interimDf = interimDf[(~ pd.isnull(interimDf.x)) & (~ pd.isnull(interimDf.y))]
ax = plt.gca()
ax = plt.plot(interimDf.x.values, interimDf.y.values, 'o', **kwargs)
# Create an instance of the PairGrid class.
grid = sns.PairGrid(data=iris, vars=list(iris.columns), size = 4)
# Map a scatter plot to the upper triangle
grid = grid.map_upper(scatterFilter, color='darkred')
# Map a histogram to the diagonal
grid = grid.map_diag(plt.hist, bins=10, edgecolor='k', color='darkred')
# Map a density plot to the lower triangle
grid = grid.map_lower(scatterFilter, color='darkred')
这将产生以下情节:
PairPlot
允许您绘制等高线图、使用描述性统计数据注释面板等。有关详细信息,请参阅 here。