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

Python 为什么逻辑回归会抛出转换错误(valueerror)?,python,scikit-learn,logistic-regression,Python,Scikit Learn,Logistic Regression,我正在为一家公司使用逻辑回归,利用客户数据找出导致客户流失的具体变量 运用分析法和评价法。对调查结果中显示这两种方法的数据进行注释 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_t

我正在为一家公司使用逻辑回归,利用客户数据找出导致客户流失的具体变量

运用分析法和评价法。对调查结果中显示这两种方法的数据进行注释

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:\Users\Rebecca\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.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)

    ~\Anaconda3\lib\site- 
     packages\sklearn\linear_model\logistic.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)

    ~\Anaconda3\lib\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, 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,

    ~\Anaconda3\lib\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, 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 supported\n"

    ~\Anaconda3\lib\site-packages\numpy\core\numeric.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)'
C:\Users\Rebecca\Anaconda3\lib\site packages\sklearn\linear\u model\logistic.py:432:FutureWarning:默认解算器将在0.22中更改为“lbfgs”。指定一个解算器以消除此警告。
未来警告)
数值误差
回溯(最近一次呼叫最后一次)
在里面
---->1个logmodel.fit(x_系列、y_系列)
~\Anaconda3\lib\site-
包装\sklearn\linear\u model\logistic.py适合(自身、X、y、样品重量)
1530
1531 X,y=检查X_y(X,y,
接受\u sparse='csr',dtype=\u dtype,order=“C”,
->1532接受\u大\u稀疏=解算器!=“liblinear”)
1533检查分类目标(y)
1534 self.classes=np.unique(y)
~\Anaconda3\lib\site packages\sklearn\utils\validation.py in check\u X\u y(X,y,accept\u sparse,accept\u large\u sparse,dtype,order,copy,force\u all\u finite,sure\u 2d,allow\n,multi\u output,sure\u samples,sure\u min\u features,y\u numeric,warn\u on\u dtype,assistor)
717确保最小特征=确保最小特征,
718 warn_on_dtype=warn_on_dtype,
-->719估计员=估计员)
720如果多输出:
721 y=检查数组(y,'csr',强制所有有限=真,确保2d=假,
检查数组中的~\Anaconda3\lib\site packages\sklearn\utils\validation.py(数组、接受稀疏、接受大稀疏、数据类型、顺序、复制、强制所有有限、确保2d、允许nd、确保最小样本、确保最小特征、警告数据类型、估计器)
494尝试:
495警告。simplefilter('error',ComplexWarning)
-->496数组=np.asarray(数组,dtype=dtype,order=order)
497除复杂警告外:
498 raise VALUERROR(“不支持复杂数据\n”
asarray中的~\Anaconda3\lib\site packages\numpy\core\numeric.py(a,数据类型,顺序)
536
537     """
-->538返回数组(a,数据类型,copy=False,order=order)
539
540
ValueError:无法将字符串转换为浮动:“银行转账(自动)”
也许这会有所帮助

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)

数据框df的内容是什么?变量银行转账(自动)'是字符串格式,您应该进行标签编码。@BhaskarDhariyal您可以演示如何进行此操作吗?@Yatin customerID老年人合作伙伴家属任期InternetService Online备份合同无纸支付方式…技术支持\u无互联网服务技术支持\u是流式处理v\u无互联网服务流式处理v\u是流式处理电影\u无流电影\u无互联网服务流电影\u是的搅动\u无搅动\u是的请将其编辑到您的问题中。。。