ValueError:未知的url类型:sklearn中出现0.0错误

2024-04-27 02:45:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的脚本,它试图将csv数据文件转换成工具svm_light可以接受的形式。代码如下:

    import csv
import sys
import numpy as np
from sklearn.cross_validation import train_test_split

def svm_light_conversion(row):
    conv_row = row[len(row) - 1] + ' '

    for i in xrange(len(row) - 1):
        conv_row = conv_row + str(i + 1) + ':' + str(row[i]) + ' '

    return conv_row

def reaData(inputfile):

    with open(inputfile, 'r') as inFile: 
        reader = csv.reader(inFile)
        my_content = list(reader)

    my_content = my_content[0:len(my_content) - 1]

    return my_content

def converToSVMLiteFormat(outputfile, train, test):

    train_file = outputfile + '_train.dat'
    test_file = outputfile + '_test.dat'
    #svm_light conversion for training data
    with open(train_file, 'wb') as txtfile:
        for i in xrange(len(train)):
            converted_row = svm_light_conversion(train[i]) + '\n'

            txtfile.write(converted_row)

    txtfile.close()

    #svm_light conversion for test data#
    with open(test_file, 'wb') as txtfile:
        for i in xrange(len(test)):
            converted_row = svm_light_conversion(test[i]) + '\n'

            txtfile.write(converted_row)

    txtfile.close()



def main():

    inputfile = sys.argv[1]
    outputfile = sys.argv[2]

    content = reaData(inputfile)

    train, test = train_test_split(content, train_size = 0.8) #split data
    converToSVMLiteFormat(outputfile, train, test)



if __name__ == "__main__":
    main()

它以前工作得非常好,但现在突然出现了错误:

^{pr2}$

谁能帮我分析一下错误吗?似乎错误发生在sklearn的某个地方,但我不完全理解可能出了什么问题。谢谢。在


Tags: testimportforlenmydefastrain
1条回答
网友
1楼 · 发布于 2024-04-27 02:45:43

如果你跟踪回溯,从你的文件行

from sklearn.cross_validation import train_test_split

您将创建导入的级联。但如果你在回溯中读到,你会看到这个

^{pr2}$

在Python的某个地方有一个名为new.py的模块。但是,您还在当前目录中创建了一个名为new.py的模块。由于priority of imports,Python将首先在当前工作目录中查找模块。据介绍,如果找不到,它会尝试其他地方

>>> import sys
>>> sys.path

所以基本上Python导入了错误的new.py,而这一切都是雪球。为了避免这个问题,只需将new文件夹和new.py文件重命名为其他文件。另外,请确保删除已创建的new.pyc文件,因为它的存在足以尝试从那里导入。在

对于好奇的人来说,这是文件的内容,位于Windows上的…/Python27/Lib/。在

"""Create new objects of various types.  Deprecated.
This module is no longer required except for backward compatibility.
Objects of most types can now be created by calling the type object.
"""
from warnings import warnpy3k
warnpy3k("The 'new' module has been removed in Python 3.0; use the 'types' "
            "module instead.", stacklevel=2)
del warnpy3k

from types import ClassType as classobj
from types import FunctionType as function
from types import InstanceType as instance
from types import MethodType as instancemethod
from types import ModuleType as module

from types import CodeType as code

相关问题 更多 >