2017-08-03 19 views
-1

python 2.7 anacondaフレームワークの分類子としてIsolationフォレストを使用しようとしています。ここに私のサンプルコードがあります。Pythonで値エラーを取得する

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.ensemble import IsolationForest 

rng = np.random.RandomState(42) 
import pandas 
from pandas import read_csv 
from numpy import set_printoptions 

filename1 = 'path/Cleanedinput.csv' 
dataframe1 = read_csv(filename, names=names,low_memory=False) 
Xtrain = dataframe1.values 
Xtrain.shape 
(996405L, 16L) 
Xtrain[0:2] 

array([[1744121620.0, 2590000000.0, '44846', '39770', '6', '100', 1L, '5', '290', 60L, '1', 1L, '-6', '46846', 12.9833, 77.5833], 
[1724121520.0, 2260000000.0, '12337', '31772', '6', '100', 1L, '1', '54', 60L, '1', 1L, '-6', '41637', 23.4833, 24.123]], dtype=object) 

clf = IsolationForest(max_samples=10, random_state=rng) 
clf.fit(X_train) 

データ型の面で私Xtrian配列が

array([[1744121620.0, 2590000000.0, '44846', '39770', '6', '100', 1L, '5', '290', 60L, '1', 1L, '-6', '46846', 12.9833, 77.5833], 
[1724121520.0, 2260000000.0, '12337', '31772', '6', '100', 1L, '1', '54', 60L, '1', 1L, '-6', '41637', 23.4833, 24.123]], dtype=object) 

のように見えますが、私は値誤差に

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-21-0a80fca9c379> in <module>() 
----> 1 clf.fit(X_train) 

C:\Anaconda\lib\site-packages\sklearn\ensemble\iforest.pyc in fit(self, X, y, sample_weight) 
    157   # ensure_2d=False because there are actually unit test checking we fail 
    158   # for 1d. 
--> 159   X = check_array(X, accept_sparse=['csc'], ensure_2d=False) 
    160   if issparse(X): 
    161    # Pre-sort indices to avoid that each individual tree of the 

C:\Anaconda\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 
    380          force_all_finite) 
    381  else: 
--> 382   array = np.array(array, dtype=dtype, order=order, copy=copy) 
    383 
    384   if ensure_2d: 

ValueError: could not convert string to float: - 

を取得しています、私が行方不明です何かがあるの

+1

あなたのCSVは何行ですか?エラーはあなたが '' - ''をフロートに変換しようとしていると言います。おそらく、あなたのcsvに '' - "'があります。私は最初の2つの行でそれが表示されません – jacoblaw

+1

私はそれが起こったかもしれないかどうかはわかりませんが、あなたの入力文字列の1つはマイナス記号以上ではないようです。 – Prune

答えて

0

の一部あなたが持っている変数Xtrainのデータは、と表されています値はnumericalではなくです。あなたは

array([[1744121620.0, 2590000000.0, '44846', '39770', '6', '100', 1L, '5', '290', 60L, '1', 1L, '-6', '46846', 12.9833, 77.5833], [1724121520.0, 2260000000.0, '12337', '31772', '6', '100', 1L, '1', '54', 60L, '1', 1L, '-6', '41637', 23.4833, 24.123]], dtype=object) 

'44846' , '39770 ..etcを提供Xtrain

は文字列値です。

このXtraindtypeを見ると、そのobjectがdtypeをfloat/intに変換して動作するはずです。

関連する問題