我试图通过在Pandas 数据框中创建散射矩阵来显示成对图.以下是如何创建配对图:

# Create dataframe from data in X_train
# Label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# Create a scatter matrix from the dataframe, color by y_train
grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)

我想把配对图显示成这样;

Enter image description here

我正在使用Python v3.6和PyCharm,我不使用Jupyter笔记本.

推荐答案

这段代码适用于我使用Python 3.5.2的情况:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn import datasets

iris_dataset = datasets.load_iris()
X = iris_dataset.data
Y = iris_dataset.target

iris_dataframe = pd.DataFrame(X, columns=iris_dataset.feature_names)

# Create a scatter matrix from the dataframe, color by y_train
grr = pd.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
                                 hist_kwds={'bins': 20}, s=60, alpha=.8)

Pandas 版<;v0.20.0.

感谢michael-szczepaniak指出此API已被弃用.

grr = pd.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
                        hist_kwds={'bins': 20}, s=60, alpha=.8)

我只是不得不把cmap=mglearn.cm3块go 掉,因为我无法让mglearn工作.sklearn存在版本不匹配问题.

要不显示图像并将其直接保存到文件,可以使用以下方法:

plt.savefig('foo.png')

同 timeshift 除

# %matplotlib inline

Enter image description here

Python-3.x相关问答推荐

海象表达可以放在方括号中而不是括号中吗?

是否有必要使用Threads()中的args显式地将共享变量传递给Python中的线程函数或直接访问它?

没有这样的命令';角色';-可靠分子

如何计算累积几何平均数?

汉明距离:涉及按位运算的逻辑步骤不清楚

PyTest:尽管明确运行了测试,但是被标记为没有运行测试

在Pandas中,根据另一列中的重复值将数据分组为一列

python3,将整数转换为字节:对于小整数使用 to_bytes() 有哪些替代方法?

如何在 Telethon 中向机器人发送发送表情符号

如果原始字符串包含正斜杠,如何返回具有不同可能性的新字符串

如何通过 GitLab V4 api 列出 gitlab 项目中的所有项目变量

以编程方式映射 uniprot ID 时如何解决 400 客户端错误?

Python:如何从句子/段落中提取地址(非正则表达式方法)?

Semaphore信号量 Python 的工作原理

如何从脚本中提取 PDF 文档的标题以进行重命名?

如何从字典中打印特定键值?

str.format_map(mapping) 和 str.format 有什么区别

tkinter TclError:错误的文件类型使用 askopenfilename

在 Python 3 中调用 super() 的 4 种方法中的哪一种?

TypeError:无法实例化类型元组;使用 tuple() 代替