【发布时间】:2018-12-22 04:26:18
【问题描述】:
我正在尝试将组件减少到 2 个而不是 64 个,但我不断收到此错误: “长度不匹配:预期轴有 64 个元素,新值有 4 个元素” 为什么我在数据集上运行的 PCA 没有将数字更改为 2?
这就是我所拥有的:
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import sklearn.metrics as sm
import pandas as pd
import numpy as np
import scipy
from sklearn import decomposition
digits = datasets.load_digits() #load the digits dataset instead of the iris dataset
x = pd.DataFrame(digits.data) #was(iris.data)
x.columns = ['Sepal_L', 'Sepal_W', 'Sepal_L', 'Sepal_W']
plt.cla()
pca = decomposition.PCA(n_components=2)
pca.fit(x)
x = pca.transform(x)
y = pd.DataFrame(digits.target)
y.columns = ['Targets']
# this line actually builds the machine learning model and runs the algorithm
# on the dataset
model = KMeans(n_clusters = 10) #Run k-means on this datatset to cluster the data into 10 classes
model.fit(x)
#print(model.labels_)
colormap = np.array(['red', 'blue', 'yellow', 'black'])
# Plot the Models Classifications
plt.subplot(1, 2, 2)
plt.scatter(x.Petal_L, x.Petal_W, c=colormap[model.labels_], s=40)
plt.title('K Means Classification')
plt.show()
【问题讨论】:
-
@sacul 哦,这有道理,你知道我应该如何修复我的列以代替用于数字数据集吗?
-
在下面查看我的答案。
标签: python pandas scipy scikit-learn