python类实现逻辑门电路_逻辑回归python实现

本文通过Python实现逻辑回归算法,包括数据预处理、模型训练、测试集评估,并使用matplotlib展示决策边界。作者深入解析了梯度下降法求解权重参数的过程,并展示了如何用逻辑回归进行二分类问题预测。
摘要由CSDN通过智能技术生成

逻辑回归python实现

"""

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

dataset = pd.read_csv('dataset.csv', delimiter=',')

X = np.asarray(dataset.get(['x1', 'x2']))

X = np.column_stack((np.ones(len(X)), X))

y = np.asarray(dataset.get('y'))

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)

# array转换为matrix

X_matrix = np.mat(X_train)

y_matrix = np.mat(y_train).transpose()

m, n = np.shape(X_matrix)

alpha = 0.001

maxCycles = 800

weights = np.ones((n, 1))

for k in range(maxCycles):

h = 1.0 / (1 + np.exp(-(X_matrix * weights)))

cost = (y_matrix - h)

weights = weights + alpha * X_matrix.transpose() * cost

# matrix转换为array

weights = weights.getA()

def predict(x0, x1, x2):

if (1.0 / (1 + np.exp(-(weights[0] * x0 + weights[1] * x1 + weights[2] * x2)))) >= 0.5:

return 1

else:

return 0

num_true = 0.0

# 预测测试集

for i in range(len(y_test)):

if predict(X_test[i][0], X_test[i][1], X_test[i][2]) == y_test[i]:

num_true += 1

# 打印准确率

print('测试集准确率:', num_true / len(y_test))

n = np.shape(X_train)[0]

xcord1 = []

ycord1 = []

xcord2 = []

ycord2 = []

for i in range(n):

if int(y_train[i]) == 1:

xcord1.append(X_train[i, 1])

ycord1.append(X_train[i, 2])

else:

xcord2.append(X_train[i, 1])

ycord2.append(X_train[i, 2])

fig = plt.figure()

ax = fig.add_subplot(111)

ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')

ax.scatter(xcord2, ycord2, s=30, c='green')

x_ = np.arange(-3.0, 3.0, 0.1)

y_ = (-weights[0] - weights[1] * x_) / weights[2]

ax.plot(x_, y_)

plt.xlabel('x1')

plt.ylabel('x2')

plt.show()

运行结果:

9e1040bb046161f6ed676cf22313b8cd.png

3c0ae5c4f883761e01b204b5ab2bf36b.png

《来源于科技文献,经本人分析整理,以技术会友,广交天下朋友》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值