Scikit学习算法表现极差

时间:2015-10-24 11:23:27

标签: algorithm machine-learning scikit-learn

我刚接触scikit学习,而且我的头撞在墙上。我已经使用了现实世界和测试数据,并且scikit算法在预测任何事情时都没有达到机会水平以上。我尝试过knn,决策树,svc和天真的贝叶斯。

基本上,我制作了一个由0和1列组成的测试数据集,所有0的特征值介于0和0.5之间,所有1的特征值介于.5和1之间。这应该是非常的容易并且准确度接近100%。但是,没有一种算法在机会级别之上执行。准确率从45%到55%不等。我已经尝试为每个算法调整一大堆参数,但注意到有帮助。我认为我的实施存在根本性的错误。

请帮帮我。这是我的代码:

from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score
import sklearn
import pandas
import numpy as np


df=pandas.read_excel('Test.xlsx') 



# Make data into np arrays
y = np.array(df[1])
y=y.astype(float) 
y=y.reshape(399)

x = np.array(df[2])
x=x.astype(float) 
x=x.reshape(399, 1)



# Creating training and test data

labels_train, labels_test = train_test_split(y)
features_train, features_test = train_test_split(x)

#####################################################################
# PERCEPTRON
#####################################################################

from sklearn import linear_model

perceptron=linear_model.Perceptron()

perceptron.fit(features_train, labels_train)

perc_pred=perceptron.predict(features_test)

print sklearn.metrics.accuracy_score(labels_test, perc_pred, normalize=True, sample_weight=None)
print 'perceptron'

#####################################################################
# KNN classifier
#####################################################################
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(features_train, labels_train) 


knn_pred = knn.predict(features_test)


# Accuraatheid

print sklearn.metrics.accuracy_score(labels_test, knn_pred, normalize=True, sample_weight=None) 
print 'knn'


#####################################################################
## SVC
#####################################################################

from sklearn.svm import SVC
from sklearn import svm
svm2 = SVC(kernel="linear")


svm2 = svm.SVC()
svm2.fit(features_train, labels_train)  
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
gamma=1.0, kernel='linear', max_iter=-1, probability=False, 
random_state=None,
shrinking=True, tol=0.001, verbose=False)



svc_pred = svm2.predict(features_test)

print sklearn.metrics.accuracy_score(labels_test, svc_pred, normalize=True, 
sample_weight=None)

#####################################################################
# Decision tree
#####################################################################
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features_train, labels_train)

tree_pred=clf.predict(features_test)

# Accuraatheid

print sklearn.metrics.accuracy_score(labels_test, tree_pred, normalize=True, 
sample_weight=None)
print 'tree'

#####################################################################
# Naive bayes
#####################################################################


import sklearn
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train, labels_train)

print "training time:", round(time()-t0, 3), "s"


GaussianNB()
bayes_pred = clf.predict(features_test)



print sklearn.metrics.accuracy_score(labels_test, bayes_pred, 
normalize=True, sample_weight=None)

1 个答案:

答案 0 :(得分:5)

您好像以错误的方式使用train_test_split

labels_train, labels_test = train_test_split(y)      #WRONG
features_train, features_test = train_test_split(x)  #WRONG

分割您的标签和数据不是必需的。手动拆分数据的一种简单方法:

randomvec=np.random.rand(len(data))  
randomvec=randomvec>0.5

train_data=data[randomvec]
train_label=labels[randomvec]
test_data=data[np.logical_not(randomvec)]
test_label=labels[np.logical_not(randomvec)]

或正确使用scikit方法:

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=42)