为什么逻辑回归会抛出转换错误(valueerror)



我正在为一家公司使用Logistic回归,以使用其客户的数据找出导致流失的特定变量。

运用分析方法和评价方法。在结果中标注显示两种方法的数据

x = dF.drop("Churn", axis=1)
y = dF["Churn"]
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1)
logmodel = LogisticRegression()
logmodel.fit(x_train, y_train)

输出:

C:UsersRebeccaAnaconda3libsite-packagessklearnlinear_modellogistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
FutureWarning)
ValueError                                
Traceback (most recent call last)
<ipython-input-268-0c050c82a577> in <module>
----> 1 logmodel.fit(x_train, y_train)
~Anaconda3libsite- 
packagessklearnlinear_modellogistic.py in fit(self, X, y, sample_weight)
1530 
1531         X, y = check_X_y(X, y, 
accept_sparse='csr', dtype=_dtype, order="C",
-> 1532                          accept_large_sparse=solver != 'liblinear')
1533         check_classification_targets(y)
1534         self.classes_ = np.unique(y)
~Anaconda3libsite-packagessklearnutilsvalidation.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, warn_on_dtype, estimator)
717                     ensure_min_features=ensure_min_features,
718                     warn_on_dtype=warn_on_dtype,
--> 719                     estimator=estimator)
720     if multi_output:
721         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
~Anaconda3libsite-packagessklearnutilsvalidation.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, warn_on_dtype, estimator)
494             try:
495                 warnings.simplefilter('error', ComplexWarning)
--> 496                 array = np.asarray(array, dtype=dtype, order=order)
497             except ComplexWarning:
498                 raise ValueError("Complex data not supportedn"
~Anaconda3libsite-packagesnumpycorenumeric.py in asarray(a, dtype, order)
536 
537     """
--> 538     return array(a, dtype, copy=False, order=order)
539 
540 
ValueError: could not convert string to float: 'Bank transfer (automatic)'

也许这可以帮助

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
dF['Bank transfer (automatic)'] =  le.fit_transform(dF['Bank transfer (automatic)'])
x = dF.drop("Churn", axis=1)
y = dF["Churn"]
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=1)
logmodel = LogisticRegression()
logmodel.fit(x_train, y_train)

最新更新