【发布时间】:2016-06-03 06:38:16
【问题描述】:
我有一个如下所示的数据集:
Out Revolver Ratio Num ...
0 1 0.766127 0.802982 0 ...
1 0 0.957151 0.121876 1
2 0 0.658180 0.085113 0
3 0 0.233810 0.036050 3
4 1 0.907239 0.024926 5
...
Out 只能取值 0 和 1。
然后,我尝试使用与此处类似的以下代码生成 PCA 和 LCA 图:http://scikit-learn.org/stable/auto_examples/decomposition/plot_pca_vs_lda.html
features = Train.columns[1:]
Xf = newTrain[features]
yf = newTrain.Out
pca = PCA(n_components=2)
X_r = pca.fit(Xf).transform(Xf)
lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(Xf, yf).transform(Xf)
plt.figure()
for c, i, name in zip("rgb", [0, 1], names):
plt.scatter(X_r[yf == i, 0], X_r[yf == i, 1], c=c, label=name)
plt.legend()
plt.title('PCA plt')
plt.figure()
for c, i, name in zip("rgb", [0, 1], names):
plt.scatter(X_r2[yf == i, 0], X_r2[yf == i, 1], c=c, label=name)
plt.legend()
plt.title('LDA plt')
我可以让 PCA 情节发挥作用。但是,它没有意义,因为它只显示 2 个点。一个在 (-4000, 30) 左右,另一个在 (2400, 23.7)。我没有在该链接的图中看到一堆数据点
LDA 绘图不起作用并给出错误
IndexError: 索引 1 超出轴 1 的范围,大小为 1
我也尝试了下面的代码来生成 LDA 图,但得到了同样的错误
for c, i, name in zip("rgb", [0, 1], names):
plt.scatter(x=X_LDA_sklearn[:, 0][yf==i], y=X_LDA_sklearn[:, 1][yf==i], c=c, label=name)
plt.legend()
有人知道这是怎么回事吗?
编辑:这是我的导入
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import csv
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.lda import LDA
至于错误发生在哪里:
我明白了
FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
plt.scatter(X_r[yf == i,0], X_r[yf == i, 1], c=c, label=name)
在 for 循环内 PCA 图的行
至于线路上的LDA
plt.scatter(X_r2[yf == i, 0], X_r2[yf == i, 1], c=c, label=name)
我明白了
FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
和
IndexError: index 1 is out of bounds for axis 1 with size 1
【问题讨论】:
-
您能否添加您的
import语句并让我们知道发生错误的行? -
Train和newTrain是如何定义的?你如何读入数据?这显然是一个维度问题,所以如果你告诉我们你是如何创建你正在使用的数据的,那将会有很大帮助。 :) -
你可以在 pastebin.com/bDee3TtZ 看到我的代码 我在那个 pastebin 中忘记了一件事:我忘了在
all_cols=行上方添加行X=trainDF -
我发现了错误并更正了代码。现在请告诉我这是否能解决您的问题。
标签: python matplotlib scikit-learn pca