【发布时间】:2017-01-25 20:24:14
【问题描述】:
我有 9164 个点,其中 4303 被标记为我要预测的类,而 4861 被标记为不是该类。它们不是重复的点。
在How to split into train, test and evaluation sets in sklearn? 之后,由于我的dataset 是3 个项目(id、向量、标签)的元组,我这样做:
df = pd.DataFrame(dataset)
train, validate, test = np.split(df.sample(frac=1), [int(.6*len(df)), int(.8*len(df))])
train_labels = construct_labels(train)
train_data = construct_data(train)
test_labels = construct_labels(test)
test_data = construct_data(test)
def predict_labels(test_data, classifier):
labels = []
for test_d in test_data:
labels.append(classifier.predict([test_d]))
return np.array(labels)
def construct_labels(df):
labels = []
for index, row in df.iterrows():
if row[2] == 'Trump':
labels.append('Atomium')
else:
labels.append('Not Trump')
return np.array(labels)
def construct_data(df):
first_row = df.iloc[0]
data = np.array([first_row[1]])
for index, row in df.iterrows():
if first_row[0] != row[0]:
data = np.concatenate((data, np.array([row[1]])), axis=0)
return data
然后:
>>> classifier = SVC(verbose=True)
>>> classifier.fit(train_data, train_labels)
[LibSVM].......*..*
optimization finished, #iter = 9565
obj = -2718.376533, rho = 0.132062
nSV = 5497, nBSV = 2550
Total nSV = 5497
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=True)
>>> predicted_labels = predict_labels(test_data, classifier)
>>> for p, t in zip(predicted_labels, test_labels):
... if p == t:
... correct = correct + 1
我在 1833 个标签中只得到正确的 943 个标签 (=len(test_labels)) -> (943*100/1843 = 51.4%)
我怀疑我在这里错过了一些重要的时间,也许我应该为分类器设置一个parameter 以做更精细的工作或其他什么?
注意:第一次在这里使用 SVM,所以任何你认为理所当然的事情,我可能都没有想到......
尝试:
我同意并将负面示例的数量减少到 4303(与正面示例的数量相同)。这略微提高了准确性。
回答后编辑:
>>> print(clf.best_estimator_)
SVC(C=1000.0, cache_size=200, class_weight='balanced', coef0=0.0,
decision_function_shape=None, degree=3, gamma=0.0001, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> classifier = SVC(C=1000.0, cache_size=200, class_weight='balanced', coef0=0.0,
... decision_function_shape=None, degree=3, gamma=0.0001, kernel='rbf',
... max_iter=-1, probability=False, random_state=None, shrinking=True,
... tol=0.001, verbose=False)
>>> classifier.fit(train_data, train_labels)
SVC(C=1000.0, cache_size=200, class_weight='balanced', coef0=0.0,
decision_function_shape=None, degree=3, gamma=0.0001, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
我也尝试了clf.fit(train_data, train_labels),效果相同。
用数据编辑(数据不是随机的):
>>> train_data[0]
array([ 20.21062112, 27.924016 , 137.13815308, 130.97432804,
... # there are 256 coordinates in total
67.76352596, 56.67798138, 104.89566517, 10.02616417])
>>> train_labels[0]
'Not Trump'
>>> train_labels[1]
'Trump'
【问题讨论】:
-
SVM 需要参数调整,这非常重要(尤其是非线性内核)。你似乎没有调整这些。标准化数据(均值和方差)也非常重要。使用 scikit-learns GridSearchCV 通过交叉验证自动调整这些。
-
@sascha 您能否提供一个示例或更多内容?我真的是这里的新手!而且你说的很对!
-
阅读 scikit-learns user-guide。这些是非常基本的步骤,我很困惑为什么人们在没有阅读基本使用规则的情况下使用像 SVM 这样理论上复杂的概念。 Heres a GridSearch example 这也显示了参数调整的重要性 -> 精度在 ~ [0.2, 0.95] 之间
-
@sascha 我在开始实验之前阅读了 SVM 的文档,但是我没有找到你说的,对不起!无论如何,我刚刚应用了答案,并获得了 49.8% 的准确率,但我的尝试并没有更好……如果您对此有任何意见,请告诉我。
-
如果你这样做了,然后显示代码。尝试其他参数很有可能(~100%)会提高分数。您还应该阅读一些基本的 ML 课程。
标签: python pandas machine-learning scikit-learn classification