#写入代码 from sklearn.model_selection import train_test_split # 一般先取出X和y后再切割,有些情况会使用到未切割的,这时候X和y就可以用,x是清洗好的数据,y是我们要预测的存活数据'Survived' X = data y = train['Survived'] # 对数据集进行切割 X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier
1 2 3 4 5 6
#写入代码 # 默认参数逻辑回归模型 lr = LogisticRegression() lr.fit(X_train, y_train)
/root/.pyenv/versions/3.11.1/lib/python3.11/site-packages/sklearn/linear_model/_logistic.py:465: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. OF ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
1 2 3 4 5 6
#写入代码 # 查看训练集和测试集score值 print("Training set score: {:.2f}".format(lr.score(X_train, y_train))) print("Testing set score: {:.2f}".format(lr.score(X_test, y_test)))
Training set score: 0.80
Testing set score: 0.79
1 2 3 4 5 6 7
#写入代码 # 调整参数后的逻辑回归模型 lr2 = LogisticRegression(C=100) lr2.fit(X_train, y_train) print("Training set score: {:.2f}".format(lr2.score(X_train, y_train))) print("Testing set score: {:.2f}".format(lr2.score(X_test, y_test)))
Training set score: 0.79
Testing set score: 0.78
/root/.pyenv/versions/3.11.1/lib/python3.11/site-packages/sklearn/linear_model/_logistic.py:465: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. OF ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
n_iter_i = _check_optimize_result(
1 2 3 4 5
# 默认参数的随机森林分类模型 rfc = RandomForestClassifier() rfc.fit(X_train, y_train) print("Training set score: {:.2f}".format(rfc.score(X_train, y_train))) print("Testing set score: {:.2f}".format(rfc.score(X_test, y_test)))