我正在使用" wa_fn-usec_-telco-customer-churn.csv" telcom Customer Cretter of https://wwwwwww.kaggle.com/blastchar/blastchar/telco-custchar/telco-customer----为了使用Scikit-Learn的LogisticRecress()。
来预测流失import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
data=pd.read_csv(file)
#get rid of ID's
data=data.iloc[:,1:]
#turn categorical data to dummies
data2=pd.get_dummies(data,columns=['gender', 'Partner', 'Dependents',
'PhoneService', 'MultipleLines', 'InternetService', 'OnlineSecurity',
'OnlineBackup', 'DeviceProtection', 'TechSupport', 'StreamingTV',
'StreamingMovies', 'Contract','PaperlessBilling', 'PaymentMethod'])
#Some cleaning and adjustment
data2["TotalCharges"].replace('[^0-9.]',np.nan,inplace=True,regex=True)
data2["Churn"].replace(('Yes','No'),(1,0),inplace=True)
data2=data2.dropna()
#assign features and target
X = data2[data2.columns[:-1]] # Features
y = data2.Churn # Target variable
scores=cross_val_score(LogisticRegression(), X, y, cv=10)
print(scores)
但是,这仅打印出1个,我也尝试使用随机拆分的改组。为什么我的数据过于拟合,或者还有其他问题?
在数据清洁中,您忘了从培训数据中删除目标列。
获得假人后,'Churn'
不再是最后一列,data2.columns[:-1]
将其放在训练集中,您的模型最终从中学习。