对于数据评估,我想调整 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 列?为什么是 2 X?我认为有两个 X 只是因为偏移量。 我假设 2 列每列代表一组数据:在某种程度上像散点图,我认为它可能意味着:第一列是例如 MotorRpm,第二列可能是发动机的 MotorCurrent,并想出了这个:(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: Singular covariance matrix。请检查与数据集对应的协方差矩阵是否为满秩,并且 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 更快乐。
如果您对代码有进一步的挑战,请编辑您的问题以更多地展示您所做的事情。
