<tfoot id='Tygne'></tfoot>
<legend id='Tygne'><style id='Tygne'><dir id='Tygne'><q id='Tygne'></q></dir></style></legend>
    • <bdo id='Tygne'></bdo><ul id='Tygne'></ul>

    <small id='Tygne'></small><noframes id='Tygne'>

      1. <i id='Tygne'><tr id='Tygne'><dt id='Tygne'><q id='Tygne'><span id='Tygne'><b id='Tygne'><form id='Tygne'><ins id='Tygne'></ins><ul id='Tygne'></ul><sub id='Tygne'></sub></form><legend id='Tygne'></legend><bdo id='Tygne'><pre id='Tygne'><center id='Tygne'></center></pre></bdo></b><th id='Tygne'></th></span></q></dt></tr></i><div id='Tygne'><tfoot id='Tygne'></tfoot><dl id='Tygne'><fieldset id='Tygne'></fieldset></dl></div>
      2. 加载 svmlight 格式错误

        时间:2023-10-09
        <i id='Ba3p4'><tr id='Ba3p4'><dt id='Ba3p4'><q id='Ba3p4'><span id='Ba3p4'><b id='Ba3p4'><form id='Ba3p4'><ins id='Ba3p4'></ins><ul id='Ba3p4'></ul><sub id='Ba3p4'></sub></form><legend id='Ba3p4'></legend><bdo id='Ba3p4'><pre id='Ba3p4'><center id='Ba3p4'></center></pre></bdo></b><th id='Ba3p4'></th></span></q></dt></tr></i><div id='Ba3p4'><tfoot id='Ba3p4'></tfoot><dl id='Ba3p4'><fieldset id='Ba3p4'></fieldset></dl></div>

      3. <tfoot id='Ba3p4'></tfoot>
            <tbody id='Ba3p4'></tbody>

              • <bdo id='Ba3p4'></bdo><ul id='Ba3p4'></ul>

                <small id='Ba3p4'></small><noframes id='Ba3p4'>

                • <legend id='Ba3p4'><style id='Ba3p4'><dir id='Ba3p4'><q id='Ba3p4'></q></dir></style></legend>
                  本文介绍了加载 svmlight 格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我尝试将 svmlight python 包 与我已转换为 svmlight 格式的数据一起使用时我得到一个错误.它应该是非常基本的,我不明白发生了什么.代码如下:

                  When I try to use the svmlight python package with data I already converted to svmlight format I get an error. It should be pretty basic, I don't understand what's happening. Here's the code:

                  import svmlight
                  training_data = open('thedata', "w")
                  model=svmlight.learn(training_data, type='classification', verbosity=0)
                  

                  我也试过了:

                  training_data = numpy.load('thedata')
                  

                  training_data = __import__('thedata')
                  

                  推荐答案

                  一个明显的问题是您在打开数据文件时会截断它,因为您指定了写入模式 "w".这意味着将没有要读取的数据.

                  One obvious problem is that you are truncating your data file when you open it because you are specifying write mode "w". This means that there will be no data to read.

                  无论如何,如果您的数据文件类似于此 example,因为是python文件,所以需要导入.这应该有效:

                  Anyway, you don't need to read the file like that if your data file is like the one in this example, you need to import it because it is a python file. This should work:

                  import svmlight
                  from data import train0 as training_data    # assuming your data file is named data.py
                  # or you could use __import__()
                  #training_data = __import__('data').train0
                  
                  model = svmlight.learn(training_data, type='classification', verbosity=0)
                  

                  您可能希望将您的数据与示例的数据进行比较.

                  You might want to compare your data against that of the example.

                  数据文件格式明确后编辑

                  输入文件需要被解析成这样的元组列表:

                  The input file needs to be parsed into a list of tuples like this:

                  [(target, [(feature_1, value_1), (feature_2, value_2), ... (feature_n, value_n)]),
                   (target, [(feature_1, value_1), (feature_2, value_2), ... (feature_n, value_n)]),
                   ...
                  ]
                  

                  svmlight 包似乎不支持读取 SVM 文件格式的文件,并且没有任何解析功能,因此必须在 Python 中实现.SVM 文件如下所示:

                  The svmlight package does not appear to support reading from a file in the SVM file format, and there aren't any parsing functions, so it will have to be implemented in Python. SVM files look like this:

                  <target> <feature>:<value> <feature>:<value> ... <feature>:<value> # <info>
                  

                  所以这里有一个解析器,可以将文件格式转换为 svmlight 包所需的格式:

                  so here is a parser that converts from the file format to that required by the svmlight package:

                  def svm_parse(filename):
                  
                      def _convert(t):
                          """Convert feature and value to appropriate types"""
                          return (int(t[0]), float(t[1]))
                  
                      with open(filename) as f:
                          for line in f:
                              line = line.strip()
                              if not line.startswith('#'):
                                  line = line.split('#')[0].strip() # remove any trailing comment
                                  data = line.split()
                                  target = float(data[0])
                                  features = [_convert(feature.split(':')) for feature in data[1:]]
                                  yield (target, features)
                  

                  你可以这样使用它:

                  import svmlight
                  
                  training_data = list(svm_parse('thedata'))
                  model=svmlight.learn(training_data, type='classification', verbosity=0)
                  

                  这篇关于加载 svmlight 格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我在哪里弄乱了输出格式? 下一篇:如何为新的 python 格式选项设置可变精度

                  相关文章

                  最新文章

                    <tfoot id='FYNtK'></tfoot>

                      <legend id='FYNtK'><style id='FYNtK'><dir id='FYNtK'><q id='FYNtK'></q></dir></style></legend>

                      <small id='FYNtK'></small><noframes id='FYNtK'>

                    1. <i id='FYNtK'><tr id='FYNtK'><dt id='FYNtK'><q id='FYNtK'><span id='FYNtK'><b id='FYNtK'><form id='FYNtK'><ins id='FYNtK'></ins><ul id='FYNtK'></ul><sub id='FYNtK'></sub></form><legend id='FYNtK'></legend><bdo id='FYNtK'><pre id='FYNtK'><center id='FYNtK'></center></pre></bdo></b><th id='FYNtK'></th></span></q></dt></tr></i><div id='FYNtK'><tfoot id='FYNtK'></tfoot><dl id='FYNtK'><fieldset id='FYNtK'></fieldset></dl></div>
                      • <bdo id='FYNtK'></bdo><ul id='FYNtK'></ul>