2013-10-22 74 views
10

は、私は簡単な機械学習アルゴリズムのために次のコードを実行しようとしています:コードのこの部分で sklearnのdatasets.fetch_mldata()の使用方法は?

import re 
import argparse 
import csv 
from collections import Counter 
from sklearn import datasets 
import sklearn 
from sklearn.datasets import fetch_mldata 

dataDict = datasets.fetch_mldata('MNIST Original') 

が、私はデータセットsklearn経由mldata.orgで「MNISTオリジナル」は、本を読むことをしようとしています。これは、次のエラーが発生(コードの複数行がありますが、私は、この特定の行でエラーを取得しています):

Traceback (most recent call last): 
    File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1481, in <module> 
    debugger.run(setup['file'], None, None) 
    File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1124, in run 
    pydev_imports.execfile(file, globals, locals) #execute the script 
    File "C:/Users/sony/PycharmProjects/Machine_Learning_Homework1/zeroR.py", line 131, in <module> 
    dataDict = datasets.fetch_mldata('MNIST Original') 
    File "C:\Anaconda\lib\site-packages\sklearn\datasets\mldata.py", line 157, in fetch_mldata 
    matlab_dict = io.loadmat(matlab_file, struct_as_record=True) 
    File "C:\Anaconda\lib\site-packages\scipy\io\matlab\mio.py", line 176, in loadmat 
    matfile_dict = MR.get_variables(variable_names) 
    File "C:\Anaconda\lib\site-packages\scipy\io\matlab\mio5.py", line 294, in get_variables 
    res = self.read_var_array(hdr, process) 
    File "C:\Anaconda\lib\site-packages\scipy\io\matlab\mio5.py", line 257, in read_var_array 
    return self._matrix_reader.array_from_header(header, process) 
    File "mio5_utils.pyx", line 624, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header (scipy\io\matlab\mio5_utils.c:5717) 
    File "mio5_utils.pyx", line 653, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header (scipy\io\matlab\mio5_utils.c:5147) 
    File "mio5_utils.pyx", line 721, in scipy.io.matlab.mio5_utils.VarReader5.read_real_complex (scipy\io\matlab\mio5_utils.c:6134) 
    File "mio5_utils.pyx", line 424, in scipy.io.matlab.mio5_utils.VarReader5.read_numeric (scipy\io\matlab\mio5_utils.c:3704) 
    File "mio5_utils.pyx", line 360, in scipy.io.matlab.mio5_utils.VarReader5.read_element (scipy\io\matlab\mio5_utils.c:3429) 
    File "streams.pyx", line 181, in scipy.io.matlab.streams.FileStream.read_string (scipy\io\matlab\streams.c:2711) 
IOError: could not read bytes 

私はインターネット上で研究を試してみましたが、利用可能なすべてのヘルプはほとんどありません。このエラーを解決するための専門家の助けがあれば幸いです。

TIA。

答えて

-1

これは「MNISTオリジナル」です。 "o"を小文字にします。

dataDict = fetch_mldata('MNIST original') 

これは私の仕事:

+0

こんにちは、お返事ありがとうございます。小さな 'o'でも試してみましたが、それと同じエラーです。 – Patthebug

+0

小文字の "o"または大文字を使用しても違いはありません。内部的にはsklearn [すべて小文字にする](https://github.com/scikit-learn/scikit-learn/blob/14031f6/sklearn/datasets/mldata.py#L33): 'dataname.lower()。replace( ' '、' - ') '。 –

0

はこのようにそれを試してみてください。 from ... import ...の構文を使用していたため、使用するときにはdatasetsを前に追加しないでください。

6

キャッシュされたデータが壊れているようです。それらを削除してもう一度ダウンロードしてみてください(時間がかかります)。別々に指定されていない場合は「MINSTの元」のデータはここで

~/scikit_learn_data/mldata/mnist-original.mat 
1

にする必要がありますsklearnのために使用するMNISTデータの準備ができて取得する方法をいくつかのサンプルコードです:

def get_data(): 
    """ 
    Get MNIST data ready to learn with. 

    Returns 
    ------- 
    dict 
     With keys 'train' and 'test'. Both do have the keys 'X' (features) 
     and'y' (labels) 
    """ 
    from sklearn.datasets import fetch_mldata 
    mnist = fetch_mldata('MNIST original') 

    x = mnist.data 
    y = mnist.target 

    # Scale data to [-1, 1] - This is of mayor importance!!! 
    x = x/255.0*2 - 1 

    from sklearn.cross_validation import train_test_split 
    x_train, x_test, y_train, y_test = train_test_split(x, y, 
                 test_size=0.33, 
                 random_state=42) 
    data = {'train': {'X': x_train, 
         'y': y_train}, 
      'test': {'X': x_test, 
        'y': y_test}} 
    return data 
0

私もfetch_mldataを得ていました() "IOError:バイトを読み取れませんでした"というエラーが発生しました。ここに解決策があります。関連するコード行は、

from sklearn.datasets.mldata import fetch_mldata 
mnist = fetch_mldata('mnist-original', data_home='/media/Vancouver/apps/mnist_dataset/') 

です。希望の場所(ディレクトリ)に変更してくださいここで

はスクリプトです:あなたはdata_homeを与えていない場合は、プログラムをダウンロードし、ファイルを置くことができ/mldata/minist-original.mat

#!/usr/bin/python 
# coding: utf-8 

# Source: 
# https://stackoverflow.com/questions/19530383/how-to-use-datasets-fetch-mldata-in-sklearn 
# ... modified, below, by Victoria 

""" 
pers. comm. (Jan 27, 2016) from MLdata.org MNIST dataset contactee "Cheng Ong:" 

    The MNIST data is called 'mnist-original'. The string you pass to sklearn 
    has to match the name of the URL: 

    from sklearn.datasets.mldata import fetch_mldata 
    data = fetch_mldata('mnist-original') 
""" 

def get_data(): 

    """ 
    Get MNIST data; returns a dict with keys 'train' and 'test'. 
    Both have the keys 'X' (features) and 'y' (labels) 
    """ 

    from sklearn.datasets.mldata import fetch_mldata 

    mnist = fetch_mldata('mnist-original', data_home='/media/Vancouver/apps/mnist_dataset/') 

    x = mnist.data 
    y = mnist.target 

    # Scale data to [-1, 1] 
    x = x/255.0*2 - 1 

    from sklearn.cross_validation import train_test_split 

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

    data = {'train': {'X': x_train, 'y': y_train}, 
      'test': {'X': x_test, 'y': y_test}} 

    return data 

data = get_data() 
print '\n', data, '\n' 
0

は、プログラムは$ {yourprojectpath}を見て正しいパス

1

貧弱なWiFiを使用しているときに、同じ問題が発生し、mnist-original.matのファイルサイズが違うことがありました。私はLANに切り替えて、うまく動作します。ネットワーキングの問題かもしれません。

関連する問題