import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt from IPython.display import Image from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
1 2 3 4 5 6 7
#写入代码 # 一般先取出X和y后再切割,有些情况会使用到未切割的,这时候X和y就可以用,x是清洗好的数据,y是我们要预测的存活数据'Survived' data = pd.read_csv('clear_data.csv') train = pd.read_csv('train.csv') X = data y = train['Survived']
#写入代码 # 默认参数逻辑回归模型 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(
#写入代码 from sklearn.metrics import confusion_matrix
1 2 3 4 5
#写入代码 # 训练模型 lr = LogisticRegression(C=100) 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
#写入代码 # 模型预测结果 pred = lr.predict(X_train)
1 2 3 4
#写入代码 # 混淆矩阵 confusion_matrix(y_train, pred)
array([[355, 57],
[ 82, 174]])
1 2 3
from sklearn.metrics import classification_report # 精确率、召回率以及f1-score print(classification_report(y_train, pred))