逻辑回归的简单应用

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()
Unnamed: 0A0A1A2A3xyzlabel
001016.9312174782.8571434552.9629636298.9947095050881
111338.4246584920.1369864109.1780825943.56164450100881
221783.7908505033.4640523605.8823535619.15032750150881
332253.6170215218.5638303085.1595745322.81914950200881
442727.1428575370.2857142600.1428575033.23809550250881

2. 基本数据处理

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

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()
A0A1A2A3xyz
01016.9312174782.8571434552.9629636298.994709505088
11338.4246584920.1369864109.1780825943.5616445010088
21783.7908505033.4640523605.8823535619.1503275015088
32253.6170215218.5638303085.1595745322.8191495020088
42727.1428575370.2857142600.1428575033.2380955025088
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
A0A1A2A3xyz
3272008.5906044974.2281883085.5033565323.3557055020088
563933.1612902261.7419355203.1612903848.51612935015088
2426546.1214954638.7383184462.803738869.252336450450170
53212.8000005665.2000002149.9111114832.5777785030088
4495138.9763785117.7165352554.2519692446.771654250450130
........................
4912979.0070925374.6099291948.2269504928.29787250300170
5021375.7073174091.4146344167.2682935628.926829100100170
3582200.4237293178.5593224556.6949154955.67796620010088
3562788.2692313631.7307693693.7500004229.32692320020088
1323312.5454552428.2272735024.2272734202.136364300150130

486 rows × 7 columns

X_test
A0A1A2A3xyz
6233972.7272731984.6969704976.5151523870.606061350150200
3895275.6521743712.3478264345.3913041755.82608740035088
5515392.1232883570.5479454206.0273971779.041096400350170
6173764.8412702768.0158734231.7460323608.730159300200200
1303814.5283023394.5283024132.6886793104.292453300250130
........................
5681230.8474584624.2372884086.3559326122.79661050100200
4272925.4777074296.4968152860.2547774245.350318150250130
4856266.8316834565.5445544475.346535568.910891450450130
1161969.6124033129.3023264966.8992255711.04651220050130
1484686.4012742586.0191084689.6815293138.057325400250130

162 rows × 7 columns

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值