python中的获取值错误
我试图在python 2.7 anaconda框架中使用隔离林作为分类器,下面是我的示例代码python中的获取值错误,python,python-2.7,pandas,Python,Python 2.7,Pandas,我试图在python 2.7 anaconda框架中使用隔离林作为分类器,下面是我的示例代码 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_printoptio
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: -
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1层安装(X_系列)
C:\Anaconda\lib\site packages\sklearn\employee\iforest.pyc合适(自身、X、y、样本重量)
157#确保_2d=False,因为实际上存在单元测试检查失败
158#为1d。
-->159 X=检查数组(X,接受稀疏=['csc'],确保2d=False)
160如果是(X):
161#预排序索引,以避免
检查数组中的C:\Anaconda\lib\site packages\sklearn\utils\validation.pyc(数组、接受稀疏、数据类型、顺序、复制、强制所有有限、确保2d、允许nd、确保最小样本、确保最小特征、警告数据类型、估计器)
380力(全部有限)
381其他:
-->382 array=np.array(array,dtype=dtype,order=order,copy=copy)
383
384如果确保\u 2d:
ValueError:无法将字符串转换为浮点:-
在数据类型方面,我是否缺少一些东西您拥有的
Xtrain
变量中的一些数据表示为字符串
,而不是数值
值
在您提供的Xtrain
中
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
的dtype
,它是一个对象
,将dtype转换为float/int
,它应该可以工作。您的csv有多少行?错误表示您正在尝试将“-”
转换为浮点。您的csv中可能有“-”
。在前两行中我看不到这一点,虽然我不确定这是怎么发生的,但似乎您的一个输入字符串只不过是一个减号。