PCA icon indicating copy to clipboard operation
PCA copied to clipboard

报错查找!!!

Open yangtzelsl opened this issue 7 years ago • 5 comments

default 请问这是哪里出错了呢?

yangtzelsl avatar May 09 '18 05:05 yangtzelsl

你好,看错误提示貌似你的pd.series索引为int类型,你在使用get_loc时可能给了string类型。当然只是猜测,如果要分析原因最好能贴代码,如果已经解决请忽略。

在 2018-05-09 13:39:22,"yangtzelsl" [email protected] 写道:

请问这是哪里出错了呢?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

zhaoxingfeng avatar May 10 '18 02:05 zhaoxingfeng

嗯,代码完全用的是您github上的代码,暂时未做修改,运行初报了这个错误,不知如何解决

yangtzelsl avatar May 10 '18 03:05 yangtzelsl

代码还是您的代码;报错如下: C:\software\Anaconda3\python.exe D:/python/Machine-Learning-master/PCA/PCA_test02.py Traceback (most recent call last): File "pandas_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item TypeError: an integer is required

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\software\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc return self._engine.get_loc(key) File "pandas_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 156, in pandas._libs.index.IndexEngine.get_loc KeyError: range(0, 4)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "pandas_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item TypeError: an integer is required

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "D:/python/Machine-Learning-master/PCA/PCA_test02.py", line 82, in data, label = df[range(len(df.columns) - 1)], df[[len(df.columns) - 1]] File "C:\software\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in getitem return self._getitem_column(key) File "C:\software\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column return self._get_item_cache(key) File "C:\software\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache values = self._data.get(item) File "C:\software\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get loc = self.items.get_loc(item) File "C:\software\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 156, in pandas._libs.index.IndexEngine.get_loc KeyError: range(0, 4)

Process finished with exit code 1

yangtzelsl avatar May 10 '18 03:05 yangtzelsl

@yangtzelsl 找到原因了。你使用的是python3,在python3中range函数返回的是一个可迭代对象,而非python2中的list,所以会提示get_loc需要int类型。 改正:data, label = df[list(range(len(df.columns) - 1))], df[[len(df.columns) - 1]] 即可。

zhaoxingfeng avatar May 10 '18 07:05 zhaoxingfeng

哦哦,原来是这样,非常感谢大神耐心解决,根本不知道还有这种区别,再次测试过,成功了,谢谢谢谢!

yangtzelsl avatar May 10 '18 08:05 yangtzelsl