逻辑回归的简单应用

1.获取数据

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

data = pd.read_csv(r'data\distance.csv')
data.head()
image.png

2. 基本数据处理

# 2.1 缺失值处理
data = data.replace(to_replace="?", value=np.NaN) # 把data中的 to_replece 值 替换为 value.
data = data.dropna()        # 删除有空值的行,默认 axis=0      
data.head()

3.确定特征值,目标值

data.columns    # 所有的列名
Index(['Unnamed: 0', 'A0', 'A1', 'A2', 'A3', 'x', 'y', 'z', 'label'], dtype='object')
x = data.iloc[:, 1:8]
x.head()
y = data["label"]
y.head()
0    1
1    1
2    1
3    1
4    1
Name: label, dtype: int64
x.head()
x.shape[0]*0.75
486.0

4.分割数据

X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=22) # 训练集和测试集按照 0.75 : 0.25 (随机)
X_train
X_test
y_train
327    0
56     1
242    1
5      1
449    0
      ..
491    0
502    0
358    0
356    0
132    1
Name: label, Length: 486, dtype: int64
y_test
623    0
389    0
551    0
617    0
130    1
      ..
568    0
427    0
485    0
116    1
148    1
Name: label, Length: 162, dtype: int64

5.特征工程(标准化)

transfer = StandardScaler()                     #  实例化对象
X_train = transfer.fit_transform(X_train)       # 标准化
X_test = transfer.fit_transform(X_test)
X_train
array([[-1.43796501,  0.86810151, -0.54520456, ..., -1.55143148,
        -0.39370337, -1.39230246],
       [ 0.05371734, -1.15687309,  1.03849334, ...,  0.73802915,
        -0.77946917, -1.39230246],
       [ 2.07895166,  0.61764551,  0.48481435, ...,  1.50118269,
         1.53512564,  0.55690142],
       ...,
       [-1.28928037, -0.47243389,  0.55503119, ..., -0.40670117,
        -1.16523498, -1.39230246],
       [-0.83365733, -0.13412409, -0.09032518, ..., -0.40670117,
        -0.39370337, -1.39230246],
       [-0.4273051 , -1.03258542,  0.9046769 , ...,  0.35645237,
        -0.77946917, -0.39392974]])
X_test
array([[ 0.20623287, -1.62114508,  1.00301367, ...,  0.89636437,
        -0.76011455,  1.22013336],
       [ 1.18389765, -0.15251848,  0.47513551, ...,  1.30426052,
         0.80852949, -1.42227529],
       [ 1.27129309, -0.2730585 ,  0.35857018, ...,  1.30426052,
         0.80852949,  0.51234533],
       ...,
       [ 1.92764066,  0.57275967,  0.58383134, ...,  1.71215667,
         1.59285151, -0.43137205],
       [-1.29682755, -0.64814879,  0.99497082, ..., -0.32732407,
        -1.54443657, -0.43137205],
       [ 0.74174642, -1.10997832,  0.76310322, ...,  1.30426052,
         0.02420747, -0.43137205]])

6.机器学习(逻辑回归)

estimator = LogisticRegression()
estimator.fit(X_train, y_train)
estimator
LogisticRegression()

7.模型评估

y_predict = estimator.predict(X_test)
y_predict
array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0,
       1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
       0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1,
       0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1,
       0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0,
       1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
       0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0,
       1, 1, 1, 1, 0, 1, 1, 0], dtype=int64)
estimator.score(X_test, y_test)
0.5864197530864198
X_test
array([[ 0.20623287, -1.62114508,  1.00301367, ...,  0.89636437,
        -0.76011455,  1.22013336],
       [ 1.18389765, -0.15251848,  0.47513551, ...,  1.30426052,
         0.80852949, -1.42227529],
       [ 1.27129309, -0.2730585 ,  0.35857018, ...,  1.30426052,
         0.80852949,  0.51234533],
       ...,
       [ 1.92764066,  0.57275967,  0.58383134, ...,  1.71215667,
         1.59285151, -0.43137205],
       [-1.29682755, -0.64814879,  0.99497082, ..., -0.32732407,
        -1.54443657, -0.43137205],
       [ 0.74174642, -1.10997832,  0.76310322, ...,  1.30426052,
         0.02420747, -0.43137205]])
y_test
623    0
389    0
551    0
617    0
130    1
      ..
568    0
427    0
485    0
116    1
148    1
Name: label, Length: 162, dtype: int64
from sklearn.metrics import precision_score, recall_score, f1_score

precision = precision_score(np.array(y_test), np.array(y_predict))
recall = recall_score(y_test, y_predict)
f1 = f1_score(y_test, y_predict)

print(precision)
print(recall)
print(f1)
0.6049382716049383
0.5833333333333334
0.5939393939393939

总结

逻辑回归的准确率、召回率和F1-socre的分数相对较大,模型预测的结果不好。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容