首页 > 解决方案 > Python 3 - ValueError:无法将字符串转换为浮点数:'-'

问题描述

所以我正在从 xlsx 文件中读取数据,当我使用“features.dtypes”检查时,所有值都是浮动的,但出现此错误:AttributeError: 'Series' object has no attribute 'reshape'

xl = pd.ExcelFile("2021we-train.xlsx") xl.sheet_names df = xl.parse("2021wordembeddingsv4-trim")

y = np.zeros([41+299]) y[0:41]=1

labels=y features=df.drop('id',axis=1)

train_features, test_features, train_labels, test_labels = train_test_split(features, labels) parameters = {'C': np.linspace(0.0001, 100, 20)} grid_search = GridSearchCV(LogisticRegression(), parameters) grid_search.fit(train_features, train_labels)

这是我得到的完整错误:

-------------------------------------------------- ------------------------- ValueError Traceback (最近一次调用最后一次) in 1 个参数 = {'C': np.linspace(0.0001, 100, 20)} 2 grid_search = GridSearchCV(LogisticRegression(), parameters) ----> 3 grid_search.fit(train_features, train_labels) 4 5 print('最佳参数:', grid_search.best_params_)

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 如果 extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/model_selection/ search.py​​ in fit(self, X, y, groups, **fit_params) 878 refit_start_time = time.time() 879 if y is not None: --> 880 self.best_estimator .fit(X, y, **fit_params) 881 else: 882 self.best_estimator_.fit(X, **fit_params)

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight) 1342 _dtype = [np.float64, np.float32] 1343 - > 1344 X, y = self._validate_data(X, y, accept_sparse='csr', dtype=_dtype, 1345 order="C",
1346 accept_large_sparse=solver != 'liblinear')

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 431 y = check_array(y, **check_y_params ) 432 else: --> 433 X, y = check_X_y(X, y, **check_params) 434 out = X, y 435

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 如果 extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator) 812 raise ValueError("y cannot be None") 813 --> 814 X = check_array(X, accept_sparse=accept_sparse, 815 accept_large_sparse=accept_large_sparse, 816 dtype=dtype, order=order, copy=复制,

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 如果 extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0

~/anaconda3/envs/project1/lib/python3.9/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator) 614 array = array.astype(dtype, cast="unsafe", copy=False) 615 else: --> 616 array = np.asarray(array, order=order, dtype=dtype) 617 除了 ComplexWarning 作为 complex_warning: 618 raise ValueError("不支持复杂数据\n"

~/anaconda3/envs/project1/lib/python3.9/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order, like) 100 return _asarray_with_like(a, dtype=dtype, order=order, like=like) 101 --> 102 返回数组(a, dtype, copy=False, order=order) 103 104

~/anaconda3/envs/project1/lib/python3.9/site-packages/pandas/core/generic.py in array (self, dtype) 1897 1898 def array (self, dtype=None) -> np.ndarray: - > 1899 返回 np.asarray(self._values, dtype=dtype) 1900 1901 def array_wrap (

~/anaconda3/envs/project1/lib/python3.9/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order, like) 100 return _asarray_with_like(a, dtype=dtype, order=order, like=like) 101 --> 102 返回数组(a, dtype, copy=False, order=order) 103 104

ValueError:无法将字符串转换为浮点数:'-'

标签: python-3.xpandasfloating-pointvalueerrorword-embedding

解决方案


推荐阅读