我是python的新手。我正在嘗試了解此 K NN 演算法的作業原理,我嘗試應用此代碼。
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
print (mnist.data.shape)
print (mnist.target.shape)
import numpy as np
sample = np.random.randint(70000, size=5000)
data = mnist.data[sample]
target = mnist.target[sample]
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.8)
但它不起作用它顯示錯誤
(70000, 784)
(70000,)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-6-3b6254553355> in <module>
8 import numpy as np
9 sample = np.random.randint(70000, size=5000)
---> 10 data = mnist.data[sample]
11 #target = mnist.target[sample]
12 #from sklearn.model_selection import train_test_split
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3028 if is_iterator(key):
3029 key = list(key)
-> 3030 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
3031
3032 # take() does not accept boolean indexers
uj5u.com熱心網友回復:
您正在索引 Pandas 資料框,您應該使用 .loc 或 .iloc,正如這里指出的那樣,而不是您習慣于使用 numpy 陣列的正常索引,這應該可以作業:
data = mnist.data.loc[sample]
target = mnist.target.loc[sample]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384688.html
下一篇:sklearn多標簽分類概率校準
