使用SoapUI测试webservice接口详细步骤
255
2022-08-24
模型实例_逻辑回归(构建逻辑回归模型)
模型实例_逻辑回归
关于泰坦尼克号乘客的⽣还率 利用Python进行数据分析(第二版).pdf
Introduction to scikit-learn¶# 举个例⼦,我⽤⼀个Kaggle竞赛的经典数据集,关于泰坦尼克号# 乘客的⽣还率。我们⽤pandas加载测试和训练数据集:# 13.4 scikit-learn介绍# scikit-learn是⼀个⼴泛使⽤、⽤途多样的Python机器学习库。它# 包含多种标准监督和⾮监督机器学习⽅法和模型选择和评估、数# 据转换、数据加载和模型持久化⼯具。这些模型可以⽤于分类、# 聚合、预测和其它任务。# 举个例⼦,我⽤⼀个Kaggle竞赛的经典数据集,关于泰坦尼克号# 乘客的⽣还率。我们⽤pandas加载测试和训练数据集:train = pd.read_csv('C:/data/titanic/train.csv')test = pd.read_csv('C:/data/titanic/test.csv')train[:4]PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 Sstatsmodels和scikit-learn通常不能接收缺失数据,因此我们要查# 看列是否包含缺失值:# statsmodels和scikit-learn通常不能接收缺失数据,因此我们要查# 看列是否包含缺失值:train.isnull().sum()test.isnull().sum()PassengerId 0Pclass 0Name 0Sex 0Age 86SibSp 0Parch 0Ticket 0Fare 1Cabin 327Embarked 0dtype: int64在统计和机器学习的例⼦中,根据数据中的特征,⼀个典型的任# 务是预测乘客能否⽣还。模型现在训练数据集中拟合,然后⽤样# 本外测试数据集评估。# 我想⽤年龄作为预测值,但是它包含缺失值。缺失数据补全的⽅# 法有多种,我⽤的是⼀种简单⽅法,⽤训练数据集的中位数补全# 两个表的空值:# 在统计和机器学习的例⼦中,根据数据中的特征,⼀个典型的任# 务是预测乘客能否⽣还。模型现在训练数据集中拟合,然后⽤样# 本外测试数据集评估。# 我想⽤年龄作为预测值,但是它包含缺失值。缺失数据补全的⽅# 法有多种,我⽤的是⼀种简单⽅法,⽤训练数据集的中位数补全# 两个表的空值:impute_value = train['Age'].median()train['Age'] = train['Age'].fillna(impute_value)test['Age'] = test['Age'].fillna(impute_value)# 现在我们需要指定模型。我增加了⼀个列IsFemale,作# 为“Sex”列的编码:# 判断性别,年龄train['IsFemale'] = (train['Sex'] == 'female').astype(int)test['IsFemale'] = (test['Sex'] == 'female').astype(int)predictors = ['Pclass', 'IsFemale', 'Age']X_train = train[predictors].valuesX_test = test[predictors].valuesy_train = train['Survived'].valuesX_train[:5]y_train[:5]array([0, 1, 1, 1, 0], dtype=int64)# 我们⽤scikit•learn的LogisticRegression模型,创建⼀个模型实例:from sklearn.linear_model import LogisticRegressionmodel = LogisticRegression()# 现在,我们可以⽤model.predict,对测试数据进⾏预测:model.fit(X_train, y_train)C:\ProgramData\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)LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)现在,我们可以⽤model.predict,对测试数据进⾏预测:# 现在,我们可以⽤model.predict,对测试数据进⾏预测:y_predict = model.predict(X_test)y_predict[:10]array([0, 0, 0, 0, 1, 0, 1, 0, 1, 0], dtype=int64)(y_true == y_predict).mean()# 交叉验证通过分割训练数据来模拟样本外预测。基于模型的精度# 得分(⽐如均⽅差),可以对模型参数进⾏⽹格搜索。有些模# 型,如logistic回归,有内置的交叉验证的估计类。例如,# logisticregressioncv类可以⽤⼀个参数指定⽹格搜索对模型的正# 则化参数C的粒度:from sklearn.linear_model import LogisticRegressionCVmodel_cv = LogisticRegressionCV(10)model_cv.fit(X_train, y_train)C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py:1978: FutureWarning: The default value of cv will change from 3 to 5 in version 0.22. Specify it explicitly to silence this warning. warnings.warn(CV_WARNING, FutureWarning)LogisticRegressionCV(Cs=10, class_weight=None, cv='warn', dual=False, fit_intercept=True, intercept_scaling=1.0, l1_ratios=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, refit=True, scoring=None, solver='lbfgs', tol=0.0001, verbose=0)# 要⼿动进⾏交叉验证,你可以使⽤cross_val_score帮助函数,它# 可以处理数据分割。例如,要交叉验证我们的带有四个不重叠训# 练数据的模型,可以这样做:from sklearn.model_selection import cross_val_scoremodel = LogisticRegression(C=10)scores = cross_val_score(model, X_train, y_train, cv=4)scoresC:\ProgramData\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)C:\ProgramData\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)C:\ProgramData\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)C:\ProgramData\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)array([0.7723, 0.8027, 0.7703, 0.7883])
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~