报错查找!!!
请问这是哪里出错了呢?
你好,看错误提示貌似你的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.
嗯,代码完全用的是您github上的代码,暂时未做修改,运行初报了这个错误,不知如何解决
代码还是您的代码;报错如下: 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
Process finished with exit code 1
@yangtzelsl 找到原因了。你使用的是python3,在python3中range函数返回的是一个可迭代对象,而非python2中的list,所以会提示get_loc需要int类型。 改正:data, label = df[list(range(len(df.columns) - 1))], df[[len(df.columns) - 1]] 即可。
哦哦,原来是这样,非常感谢大神耐心解决,根本不知道还有这种区别,再次测试过,成功了,谢谢谢谢!