【问题标题】:Wine dataset LDA & PCA comparison - PythonWine 数据集 LDA 和 PCA 比较 - Python
【发布时间】:2020-09-18 21:15:09
【问题描述】:

我正在尝试使用从 Internet 下载的 WINE 数据集运行此 Comparison of LDA and PCA 2D projection of Iris dataset example,但出现错误:

d:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().   y = column_or_1d(y, warn=True)
--------------------------------------------------------------------------- IndexError                                Traceback (most recent call last) <ipython-input-44-a42b504da984> in <module>
     14 
     15 for color, i, target_name in zip(colors, [0, 1, 2], target_names):
---> 16     plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw,
     17                 label=target_name)
     18 plt.legend(loc='best', shadow=False, scatterpoints=1)

IndexError: too many indices for array

<Figure size 432x288 with 0 Axes>

我该如何解决?

代码:

import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

import pandas as pd

df = pd.read_csv('https://gist.githubusercontent.com/tijptjik/9408623/raw/b237fa5848349a14a14e5d4107dc7897c21951f5/wine.csv')
df.to_numpy()

X  = df.iloc[:,[1,2,3,4,5,6,7,8,9,10,11,12,13]]
X.to_numpy()
y  = df.iloc[:,[0]]
y.to_numpy()
target_names = y

pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)

lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)

# Percentage of variance explained for each components
print('explained variance ratio (first two components): %s'
      % str(pca.explained_variance_ratio_))

plt.figure()
colors = ['navy', 'turquoise', 'darkorange']
lw = 2

for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw,
                label=target_name)

【问题讨论】:

  • 由于错误来自matplotlib 命令,因此在此处包含相应标签而不是pandasnumpy 应该是很自然的。另外,任何出现在错误之后的代码都与问题无关(因为从未执行过),不应包含在此处(已编辑)。

标签: python numpy matplotlib scikit-learn


【解决方案1】:

如果您查看示例脚本中的yy,您将看到您的y 具有多个维度,即使第二个暗淡的长度为0。您需要删除此第二个维度可以通过np.squeeze完成

y = np.squeeze(df.loc[:,['Wine']].to_numpy())

这是完整的代码

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

df = pd.read_csv('https://gist.githubusercontent.com/tijptjik/9408623/raw/b237fa5848349a14a14e5d4107dc7897c21951f5/wine.csv')

X = df.loc[:, df.columns != 'Wine'].to_numpy()
y = np.squeeze(df.loc[:,['Wine']].to_numpy())
target_names = ['first', 'sceond', 'third']

pca = PCA(n_components=2)
X_r = pca.fit(X).transform(X)

lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)


lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)

# Percentage of variance explained for each components
print('explained variance ratio (first two components): %s'
    % str(pca.explained_variance_ratio_))

plt.figure()
colors = ['navy', 'turquoise', 'darkorange']
lw = 2

for color, i, target_name in zip(colors, [1,2,3], target_names):
    plt.scatter(X_r[y == i, 0], X_r[y == i, 1], color=color, alpha=.8, lw=lw,
                label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.title('PCA of Wine dataset')

plt.figure()
for color, i, target_name in zip(colors, [1, 2, 3], target_names):
    plt.scatter(X_r2[y == i, 0], X_r2[y == i, 1], alpha=.8, color=color,
                label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.title('LDA of Wine dataset')

plt.show()

【讨论】:

  • 谢谢。 “第一”点不在 LDA 图中的任何原因?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多