使用 matplotlib 可视化 10 维数据
Visualising 10 dimensional data with matplotlib
我有这样的数据:
ID x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1 -0.18 5 -0.40 -0.26 0.53 -0.66 0.10 2 -0.20 1
2 -0.58 5 -0.52 -1.66 0.65 -0.15 0.08 3 3.03 -2
3 -0.62 5 -0.09 -0.38 0.65 0.22 0.44 4 1.49 1
4 -0.22 -3 1.64 -1.38 0.08 0.42 1.24 5 -0.34 0
5 0.00 5 1.76 -1.16 0.78 0.46 0.32 5 -0.51 -2
可视化此数据的最佳方法是什么,我正在使用 matplotlib 对其进行可视化,并使用 pandas
从 csv 中读取它
谢谢
在高维度中可视化数据 space 始终是一个难题。一种常用的解决方案 (and is now available in pandas
) 是检查数据的所有 1D 和 2D 投影。它不会为您提供有关数据的所有信息,但除非您可以在 10D 中看到,否则无法将其可视化!下面是如何使用 pandas(版本 0.7.3 以上)执行此操作的示例:
import numpy as np
import pandas as pd
from pandas.plotting import scatter_matrix
#first make some fake data with same layout as yours
data = pd.DataFrame(np.random.randn(100, 10), columns=['x1', 'x2', 'x3',\
'x4','x5','x6','x7','x8','x9','x10'])
#now plot using pandas
scatter_matrix(data, alpha=0.2, figsize=(6, 6), diagonal='kde')
这会生成一个图,其中所有二维投影都作为散点图,以及一维投影的 KDE 直方图:
我在 my github page 上也有一个纯 matplotlib 方法来处理这个问题,它产生了一种非常相似的绘图类型(它是为 MCMC 输出设计的,但在这里也适用)。以下是您在这里的使用方式:
import corner_plot as cp
cp.corner_plot(data.as_matrix(),axis_labels=data.columns,nbins=10,\
figsize=(7,7),scatter=True,fontsize=10,tickfontsize=7)
您可以随时间更改绘图,对于每个瞬间,您都绘制不同的 "dimension" 数据帧。
这里有一个关于如何绘制随时间变化的图的示例,您可以根据自己的目的进行调整
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
plt.grid(True)
plt.hold(False)
x = np.arange(-3, 3, 0.01)
for n in range(15):
y = np.sin(np.pi*x*n) / (np.pi*x*n)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)
我有这样的数据:
ID x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1 -0.18 5 -0.40 -0.26 0.53 -0.66 0.10 2 -0.20 1
2 -0.58 5 -0.52 -1.66 0.65 -0.15 0.08 3 3.03 -2
3 -0.62 5 -0.09 -0.38 0.65 0.22 0.44 4 1.49 1
4 -0.22 -3 1.64 -1.38 0.08 0.42 1.24 5 -0.34 0
5 0.00 5 1.76 -1.16 0.78 0.46 0.32 5 -0.51 -2
可视化此数据的最佳方法是什么,我正在使用 matplotlib 对其进行可视化,并使用 pandas
从 csv 中读取它谢谢
在高维度中可视化数据 space 始终是一个难题。一种常用的解决方案 (and is now available in pandas
) 是检查数据的所有 1D 和 2D 投影。它不会为您提供有关数据的所有信息,但除非您可以在 10D 中看到,否则无法将其可视化!下面是如何使用 pandas(版本 0.7.3 以上)执行此操作的示例:
import numpy as np
import pandas as pd
from pandas.plotting import scatter_matrix
#first make some fake data with same layout as yours
data = pd.DataFrame(np.random.randn(100, 10), columns=['x1', 'x2', 'x3',\
'x4','x5','x6','x7','x8','x9','x10'])
#now plot using pandas
scatter_matrix(data, alpha=0.2, figsize=(6, 6), diagonal='kde')
这会生成一个图,其中所有二维投影都作为散点图,以及一维投影的 KDE 直方图:
我在 my github page 上也有一个纯 matplotlib 方法来处理这个问题,它产生了一种非常相似的绘图类型(它是为 MCMC 输出设计的,但在这里也适用)。以下是您在这里的使用方式:
import corner_plot as cp
cp.corner_plot(data.as_matrix(),axis_labels=data.columns,nbins=10,\
figsize=(7,7),scatter=True,fontsize=10,tickfontsize=7)
您可以随时间更改绘图,对于每个瞬间,您都绘制不同的 "dimension" 数据帧。 这里有一个关于如何绘制随时间变化的图的示例,您可以根据自己的目的进行调整
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
plt.grid(True)
plt.hold(False)
x = np.arange(-3, 3, 0.01)
for n in range(15):
y = np.sin(np.pi*x*n) / (np.pi*x*n)
line, = ax.plot(x, y)
plt.draw()
plt.pause(0.5)