【发布时间】:2019-01-04 06:04:02
【问题描述】:
我在 csv 文件中混合了不同类型的功能(分类字符串、0-1 二进制、浮点数)。我想用 10 倍交叉验证进行 SVM 回归。基于this post,我尝试了以下读取数据,但我得到一个错误,字符串不能转换为浮点数:
df = pd.read_csv("output.csv")
datanumpy = df.as_matrix()
x = datanumpy[:, 0:143] # select columns 1 through 41 (the features)
y = datanumpy[:, 144] # select column 42 (the labels)
clf = SVC(kernel='linear')
clf.fit(x, y)
知道如何处理这些因素吗?
错误信息是:
ValueError Traceback (most recent call last)
<ipython-input-22-731136d5a713> in <module>()
75
76 # # fitting x samples and y classes
---> 77 clf.fit(x, y)
78
79
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
149 self._sparse = sparse and not callable(self.kernel)
150
--> 151 X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
152 y = self._validate_targets(y)
153
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
519 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
520 ensure_2d, allow_nd, ensure_min_samples,
--> 521 ensure_min_features, warn_on_dtype, estimator)
522 if multi_output:
523 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py 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: '/Users/dorien/AC/Projects/memory/S1 - Stimuli/Exp1-2-Stimuli/MIDI/Stimulus9.mid'
我应该指出哪些列是因子?
【问题讨论】:
-
提供数据样本和完整的错误信息。
-
另外请注意,您正在使用当前切片删除一列。由于 Python 不包含在内,
datanumpy[:,0:143]将仅返回列 0 到 142,而datanumpy[:,144]将选择列 144,因此您将忽略列 143。 -
感谢您的提醒。我实际上想跳过第 143 列,因为它是在第二个实验中检查的第二个目标变量。
-
@AkshayNevrekar 我已更新问题以包含错误消息。
-
使用
pd.dummies()或labelEncoder()处理非数字数据。
标签: python scikit-learn svm