簡體   English   中英

ValueError:未知的url類型:sklearn中的0.0錯誤

[英]ValueError: unknown url type: 0.0 error in sklearn

我有一個簡單的腳本,試圖將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()

之前它工作得非常好,但是現在突然出現了錯誤:

(env)fieldsofgold@fieldsofgold-VirtualBox:~/new$ python prac.py data.csv outt
Traceback (most recent call last):
  File "prac.py", line 4, in <module>
    from sklearn.cross_validation import train_test_split
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/sklearn/cross_validation.py", line 32, in <module>
    from .metrics.scorer import check_scoring
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/sklearn/metrics/__init__.py", line 7, in <module>
    from .ranking import auc
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 30, in <module>
    from ..utils.stats import rankdata
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/sklearn/utils/stats.py", line 2, in <module>
    from scipy.stats import rankdata as _sp_rankdata
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/scipy/stats/__init__.py", line 338, in <module>
    from .stats import *
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/scipy/stats/stats.py", line 189, in <module>
    from . import distributions
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/scipy/stats/distributions.py", line 10, in <module>
    from ._distn_infrastructure import (entropy, rv_discrete, rv_continuous,
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/scipy/stats/_distn_infrastructure.py", line 44, in <module>
    from new import instancemethod
  File "/home/fieldsofgold/new/new.py", line 10, in <module>
    response2 = urllib2.urlopen(row[12])
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 396, in open
    protocol = req.get_type()
  File "/usr/lib/python2.7/urllib2.py", line 258, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: 0.0

誰能幫我解析錯誤? 似乎該錯誤發生在sklearn中的某處,但我不完全了解可能出了什么問題。 謝謝。

如果您跟蹤回溯,請從文件中的行開始

from sklearn.cross_validation import train_test_split

您創建了一系列導入。 但是,如果您稍后在回溯中閱讀,您將看到

    from new import instancemethod
  File "/home/fieldsofgold/new/new.py", line 10, in <module>

Python中某個地方有一個名為new.py的模塊。 但是,您還在當前目錄中創建了一個名為new.py的模塊。 由於導入優先級 ,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM