对于数据评估,我想调整 this example 的等高线图。我无法理解示例中这些行的用途:
X1 = 0.3 * np.random.randn(0.5 * n_inliers, 2) - offset
X2 = 0.3 * np.random.randn(0.5 * n_inliers, 2) + offset
为什么有两列?为什么是 2 X?我认为有两个 X 只是因为偏移。
我假设这两列分别代表一组数据:以类似于散点图的方式,我认为这可能意味着:第一列是例如 MotorRpm,第二列可能是发动机的 MotorCurrent,并提出了this: (getSeries
是一个从数据帧中提取列的函数)
ser1 = getSeries("MotorRpm") # type pandas.Series, kind of numpy.array
ser2 = getSeries("MotorCurrent")
blub = []
for i in range(len(ser1)):
blub.append([ser1.ix[i], ser2.ix[i]])
length = len(blub)
if length%2==1:
blub.pop(length-1) # make list even
X1 = blub[:length/2]
X2 = blub[length/2:]
但这会引发ValueError:奇异协方差矩阵。请检查与数据集对应的协方差矩阵是否为满秩,并且 MinCovDet 是否与高斯分布数据(或至少从单峰对称分布提取的数据)一起使用。
在
clf.fit(X)
示例中的 行。当我想检查每个 X 的作用时,也会发生这种情况,将随机数更改为仅随机数。我知道什么是奇异矩阵 - 但不知道 fit
内部的作用。如果这个例子在适应现实生活数据方面如此有限,那么除了看起来好看之外,它的真正用途是什么?
编辑:
看来这个警告适用于我的情况(MinCovDet(EmpiricalCovariance)
):
The Minimum Covariance Determinant covariance estimator is to be applied
on Gaussian-distributed data, but could still be relevant on data
drawn from a unimodal, symmetric distribution. It is not meant to be used
with multi-modal data (the algorithm used to fit a MinCovDet object is
likely to fail in such a case).
One should consider projection pursuit methods to deal with multi-modal
datasets.
最佳答案
对这个谜团(为什么有两列)的解释进一步在示例代码中:
X1 = 0.3 * np.random.randn(0.5 * n_inliers, 2) - offset
X2 = 0.3 * np.random.randn(0.5 * n_inliers, 2) + offset
X = np.r_[X1, X2]
# Add outliers
X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))]
它的作用是使用 numpy.r_
垂直(沿着第一个轴)堆叠三个 2 列数组。数组的第一部分包含从中心偏移 (offset,offset)
或 (-offset,-offset)
的点。数组的第三部分包含完全关闭的点。
这意味着实际上不同的列中有不同的轴。因此,您需要根据自己的数据构建一个 Nx2 数组。如果您的 getSeries
返回 ndarray
或 pandas.Series
,那么这样做:
X = np.column_stack((getSeries("MotorRPM"), getSeries("MotorCurrent")))
完成此操作后,检查 X.shape
是否为您提供 (123,2)
(当然 123
是别的东西)。这应该会让 clf.fit
更快乐。
如果您的代码遇到进一步的挑战,请编辑您的问题以显示您所做的更多内容。
关于python - 适应 matplotlib 中的协方差等高线绘图示例会引发奇异矩阵错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590284/