我正在尝试运行在github上找到的用于超参数优化和网格搜索的代码。因此,我的csv文件的内容为:
所以,我的代码是:
import pandas as pd
import svm
from random import randint
from sklearn import svm
#read data
dataframe=pd.read_csv("treshold1.csv")
X = dataframe[['t1']]
y = dataframe[['t2']]
best_score = 0
best_params = {'C': None, 'gamma': None}
#for a preset number of iterations
for i in range(10):
#try random values for each hyperparameter
svc = svm.SVC(C=randint(0, 9), gamma=randint(0, 3))
svc.fit(X, y)
score = svc.score(Xval, yval)
if score > best_score:
best_score = score
best_params['C'] = C
best_params['gamma'] = gamma
best_score, best_params
运行后,出现此错误:
Traceback (most recent call last):
File "g:/Maksa/Programiranje/Python/SI/image_quality_assessment-master/common/s.py", line 11, in <module>
X = dataframe[['t1']]
File "C:\Users\ftnkm\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 3001, in __getitem__
indexer = self.loc._convert_to_indexer(key, axis=1, raise_missing=True)
File "C:\Users\ftnkm\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1285, in _convert_to_indexer
return self._get_listlike_indexer(obj, axis, **kwargs)[1]
File "C:\Users\ftnkm\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1092, in _get_listlike_indexer
keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
File "C:\Users\ftnkm\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1177, in _validate_read_indexer
key=key, axis=self.obj._get_axis_name(axis)
KeyError: "None of [Index(['t1'], dtype='object')] are in the [columns]"
答案 0 :(得分:0)
我相信您只需要将列分隔符和小数点分隔符精确到read_csv函数即可。请注意,它期望使用“,”和“。”默认。 尝试: dataframe = pd.read_csv(“ treshold1.csv”,sep =“;”,十进制=“,”)
答案 1 :(得分:0)
如果您的输入文件确实由逗号分隔,则您的代码实际上可以运行。
但是,由于您获得了KeyError
,因此您的输入文件treshold1.csv
可能没有用逗号分隔。
从“ csv”文件内容的屏幕快照中猜测,我认为您的输入文件可能是制表符分隔的。修复它,它应该修复KeyError
。