Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python机器学习预测错误_Python_Python 3.x_Machine Learning - Fatal编程技术网

Python机器学习预测错误

Python机器学习预测错误,python,python-3.x,machine-learning,Python,Python 3.x,Machine Learning,我有一个.csv文件,有26个功能列和1个列类。我用过 Python为了训练和测试,我使用了26个特性来预测类 显示此错误: ubuntu@ubuntu:~/python$python3 classifier.py train.csv /usr/local/lib/python3.4/dist-packages/sklearn/utils/validation.py:395: 不推荐使用警告:在0.17中不推荐将1d数组作为数据传递 威尔 提升值误差为0.19。使用以下两种方法重塑数据: X.重

我有一个
.csv
文件,有26个功能列和1个列类。我用过

Python
为了训练和测试,我使用了26个特性来预测类

显示此错误:

ubuntu@ubuntu:~/python$python3 classifier.py train.csv

/usr/local/lib/python3.4/dist-packages/sklearn/utils/validation.py:395: 不推荐使用警告:在0.17中不推荐将1d数组作为数据传递 威尔

提升值误差为0.19。使用以下两种方法重塑数据: X.重塑(-1,1)如果

您的数据只有一个特征或X。如果包含 单一样本

弃用警告)回溯(最近一次调用上次):文件 “分类器.py”,第44行,在 准确度评分(训练、预测)文件“/usr/local/lib/python3.4/dist-packages/sklearn/metrics/classification.py”, 第172行,准确度评分 y_type,y_true,y_pred=_check_targets(y_true,y_pred)文件“/usr/local/lib/python3.4/dist packages/sklearn/metrics/classification.py”, 第72行,输入检查目标 检查长度一致的文件“/usr/local/lib/python3.4/dist-packages/sklearn/utils/validation.py”, 第181行,检查长度是否一致 “样本:%r”%[int(l)表示长度为l的样本])值错误:找到样本数不一致的输入变量:[778,1]


你看到错误信息了吗?它直接告诉您使用什么代码来修复问题。如果这还不清楚,你能说是哪一部分导致了问题吗?准确度评分(train\u X,predict)导致了问题,当我打印predict时,代码显示了预测的类,但评分是错误的地方OK right,警告是针对另一行的。您是否意识到您在
准确性评分(train\ux,predict)
中使用了训练集示例和测试集预测?即使它们是相同数量的示例,结果也将是无意义的值。只需将其更改为
准确度评分(测试、预测)
。但是,除非你也修复了这个错误,否则你仍然会收到弃用警告(可能还有其他错误)。我没有定义test_Y,test_X是我想要预测它属于哪个类的数据。如果你还不知道真相,那么你就不可能知道你对特定新数据的预测有多准确。但是,您可以通过将训练数据拆分为单独的训练集和测试集来获得一个大致的概念-在这种情况下,您可以对代码使用
test_X
test_y
。请参阅交叉验证网站上的问答:
import sys
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score

def getFeatures(file_name,length=27):
all_features=[]
all_classes=[]
for line in open(file_name):
    s=line.strip().split(",")
    lst=[]
    if len(s)==length:
        class_=int(s[length-1])
        for i in range(length-1):
            lst.append(float(s[i]))
        all_features.append(lst)
        all_classes.append(class_)

return np.array(all_features),np.array(all_classes)


 if len(sys.argv)>1:
input_train_file=sys.argv[1]       # e.g. input train file
#input_test_file=sys.argv[2]        # e.g. input test file

# make features ready for train set
train_X,train_y=getFeatures(input_train_file)

# make features ready for test set
#test_X,test_y=getFeatures(input_test_file)

clf = SVC(kernel='linear')

# train
clf.fit(train_X, train_y)

#predict
test_X=[0.24    ,0.27031,   0.239808    ,0.271922   ,0.249783   ,0.271922,  0.239808    ,0.271922,  0.261446,   0.27692 ,0.270599   ,0.27031,   0.270599,   0.281724,   0.259792,   0.271922,   0.239808,   0.271922,   0.261212    ,0.271922   ,0.259792   ,0.276243   ,0.261212,  0.27031 ,0.259792   ,0.274605
]

predict=clf.predict(test_X)

accuracy_score(train_X, predict)