# 使用LogisticRegression类构建Logistic回归模型
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression(solver='saga')
# 训练Logistic回归模型
lr_model.fit(x_trainStd, y_train)
#print('训练出来的LogisticRegression模型为:\n', lr_model)
print('各特征的相关系数为:\n', lr_model.coef_)
print('模型的截距为:', lr_model.intercept_)
print('模型的迭代次数为:', lr_model.n_iter_)
各特征的相关系数为:
[[-0.5857132 -0.6671353 -0.55784394 -0.59720716 -0.14742436 0.28719422
-0.64455749 -0.73072149 0.01915743 0.55155391 -0.93286179 0.27150502
-0.68537501 -0.76677376 -0.29610366 0.56449572 -0.05649833 -0.15164115
0.48066001 0.59434489 -0.92845586 -1.03627723 -0.82060111 -0.87395882
-0.89268689 -0.09146955 -0.77352108 -0.8666452 -0.74344702 -0.29534538]]
模型的截距为: [0.65729624]
模型的迭代次数为: [100]
print('预测测试集前10个结果为:\n', lr_model.predict(x_testStd)[: 10])
print('测试集准确率为:', lr_model.score(x_testStd, y_test))
print('测试集前3个对应类别的概率为:\n', lr_model.predict_proba(x_testStd)[: 3])
print('测试集前3个对应类别的概率的log值为:\n',lr_model.predict_log_proba(x_testStd)[: 3])
print('测试集前3个的决策函数值为:\n',lr_model.decision_function(x_testStd)[: 3])
print('模型的参数为:\n', lr_model.get_params())
print('修改max_iter参数为1000后的模型为:\n', lr_model.set_params(max_iter=1000))
print('系数矩阵转为密度数组后的模型为:\n', lr_model.densify())
print('系数矩阵转为稀疏形式后的模型为:\n', lr_model.sparsify())
预测测试集前10个结果为:
[1 0 0 0 1 1 1 1 1 1]
测试集准确率为: 0.9736842105263158
测试集前3个对应类别的概率为:
[[1.38795312e-02 9.86120469e-01]
[9.99999950e-01 4.97110001e-08]
[9.99966579e-01 3.34210879e-05]]
测试集前3个对应类别的概率的log值为:
[[-4.27734010e+00 -1.39767525e-02]
[-4.97110013e-08 -1.68170396e+01]
[-3.34216464e-05 -1.03063235e+01]]
测试集前3个的决策函数值为:
[ 4.26336335 -16.81703955 -10.30629006]
模型的参数为:
{'C': 1.0, 'class_weight': None, 'dual': False, 'fit_intercept': True, 'intercept_scaling': 1, 'l1_ratio': None, 'max_iter': 100, 'multi_class': 'auto', 'n_jobs': None, 'penalty': 'l2', 'random_state': None, 'solver': 'saga', 'tol': 0.0001, 'verbose': 0, 'warm_start': False}
修改max_iter参数为1000后的模型为:
LogisticRegression(max_iter=1000, solver='saga')
系数矩阵转为密度数组后的模型为:
LogisticRegression(max_iter=1000, solver='saga')
系数矩阵转为稀疏形式后的模型为:
LogisticRegression(max_iter=1000, solver='saga')
|