首页 > 解决方案 > 如何解决模块'pandas'没有属性'scatter_matrix'属性错误

问题描述

这是我使用的代码,但即使在导入 scatter_matrix 之后它也会给我错误:

from matplotlib import cm
    from pandas.plotting import scatter_matrix
    X = fruits[['height', 'width', 'mass', 'color_score']]
    y = fruits['fruit_label']
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    
    cmap = cm.get_cmap('gnuplot')
    scatter = pd.scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)

标签: python-3.xpandas

解决方案


确保您的格式没问题,您可以尝试删除pd.scatter_matrix并保留scatter_matrix

scatter = pd.scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)

进入

scatter = scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)

如果您使用 jupyter notebook,您可能还需要为您的笔记本添加 matplotlib 内联

%matplotlib notebook

from matplotlib import cm
from pandas.plotting import scatter_matrix
    
X = fruits[['height', 'width', 'mass', 'color_score']]
y = fruits['fruit_label']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
        
cmap = cm.get_cmap('gnuplot')
scatter = scatter_matrix(X_train, c= y_train, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap=cmap)

推荐阅读