Моя цель состоит в том, чтобы создать комбинацию доступных функций (например: комбинация только 1 функции, всего 2, просто 3 и т. Д.).Я добился этого с помощью функции combination
в Python.проблема возникает, когда я передаю свой combofallfeat на X = signaldata[i]
и получаю ошибку.Я намерен проверить каждую комбинацию и сравнить их точность:
import pandas as pd
from sklearn import metrics
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# Path to the dataset
file_path = '../input/Data.csv'
#import dataset
signaldata = pd.read_csv(file_path)
comb1 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 1)
comb2 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 2)
comb3 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 3)
comb4 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 4)
comb5 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 5)
comb6 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 6)
comb7 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 7)
comb8 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 8)
comb9 = combinations(['RMSEVM', 'MAXEVM', 'EYEAMP',
'EYESNR', 'EYEDELAY', 'EYEWIDTH',
'ENERGY', 'BPR', 'MEANEIGEN'], 9)
combofallfeat = list(comb1) + list(comb2) + list(comb3) + list(comb4) + list(comb5) + list(comb6) + list(comb7) + list(comb8) + list(comb9)
#for i in list(combofallfeat):
# print (i)
#set y to be our target of prediction
y = signaldata.LABEL
#set x to be our features used in training
for i in list(combofallfeat):
X = signaldata[i]
#split data set into training and testing with %70 for training and %30 for testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
#Create a Gaussian Classifier
clf = RandomForestClassifier(n_estimators=100)
#Train the model using the training sets y_pred=clf.predict(X_test)
clf.fit(X_train,y_train)
#get predictions
y_pred=clf.predict(X_test)
#print accuracy
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Полученная ошибка:
KeyError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('RMSEVM',)
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-48-aeda6c0b8228> in <module>
58 #set x to be our features used in training
59 for i in list(combofallfeat):
---> 60 X = signaldata[i]
61
62 #split data set into training and testing with %70 for training and %30 for testing
/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
2978 if self.columns.nlevels > 1:
2979 return self._getitem_multilevel(key)
-> 2980 indexer = self.columns.get_loc(key)
2981 if is_integer(indexer):
2982 indexer = [indexer]
/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2897 return self._engine.get_loc(key)
2898 except KeyError:
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2901 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: ('RMSEVM',)
Образец набора данных