import numpy as np
import matplotlib.pyplot as plt
import pandas
import wx
class logistic:
def __sigmoid(x):
return 1.0/(1+np.exp(-x))
def Loaddata(name ="new.csv"):
with open(name,'r',newline='') as f:
lines=np.array(pandas.read_csv(f)).astype(np.float) #矩阵化
label=lines[:,4] #切片
label=np.expand_dims(label,axis=-1)
data=lines[:,0:2]/129
N,W=np.shape(data)
data=np.hstack((np.ones([N,1]),data))
return label,data
def __WEight_updata(data,label,w,alpha=0.01):
z=np.dot(data,w)
h=logistic.__sigmoid(z)
Error=label-h
w=w+alpha*np.dot(data.T,Error)
return w
def train_LR(data,label,n_epoch=2,alpha=0.01):
N,D=np.shape(data)
w=np.ones([D,1]) #生成w
for i in range (n_epoch):
w=logistic.__WEight_updata(data,label,w,alpha)
#print(w)
error_rate=logistic.__test_accuracy(data,label,w)
shuchu2.SetValue(str(error_rate))
return w
def __test_accuracy(data,label,w):
N,D=np.shape(data)
z=np.dot(data,w)
h=logistic.__sigmoid(z)
lab_det=(h>0.5).astype(np.float)
error_rate=np.sum(np.abs(label-lab_det))/N
#print(N)
return error_rate
def huitu(data,label,w,*z):
# print(w)
z=np.array(z)/129
dic_colors={0:(.8,0,0),1:(0,.8,0),2:(0,0,0)}
for i in range (2):
index=np.where(label==i)[0]
sub_data=data[index]
plt.scatter(sub_data[:,1],sub_data[:,2],color=dic_colors[i])
plt.scatter(z[:,0],z[:,1],color=dic_colors[2])
min_x=np.min(data[:,1])
max_x=np.max(data[:,1])
w=w[:,0]
x=np.arange(min_x,max_x,0.01)
y=(-x*w[1]-w[0])/w[2]
plt.plot(x,y)
plt.show()
def huitu2(data,label,w):
# print(w)
dic_colors={0:(.8,0,0),1:(0,.8,0),2:(0,0,0)}
for i in range (2):
index=np.where(label==i)[0]
sub_data=data[index]
plt.scatter(sub_data[:,1],sub_data[:,2],color=dic_colors[i])
min_x=np.min(data[:,1])
max_x=np.max(data[:,1])
w=w[:,0]
x=np.arange(min_x,max_x,0.01)
y=(-x*w[1]-w[0])/w[2]
plt.plot(x,y)
plt.show()
def ceshi1(even):
label,data=logistic.Loaddata()
weights=logistic.train_LR(data,label,500,1.2)
#print(weights)
sg=float(shuru1.GetValue())
tz=float(shuru2.GetValue())
ceshi=(sg,tz)
logistic.huitu(data,label,weights,ceshi)
def ceshi2(even):
label,data=logistic.Loaddata()
weights=logistic.train_LR(data,label,500,1.2)
logistic.huitu2(data,label,weights)
app=wx.App()
win=wx.Frame(None,title="logistic分类器",size=(600,600))
panel=wx.Panel(win)
win.Show()
huitu1Button=wx.Button(panel,label="测试",pos=(400,180))
huituButton=wx.Button(panel,label="查看样本分布",pos=(400,140))
shuchu1=wx.StaticText(panel,pos=(10,50),label=("错误率"))
shuchu2=wx.TextCtrl(panel,pos=(50,50),style=wx.TE_READONLY,size=(240,30))
shuru1=wx.TextCtrl(panel,pos=(50,100),style=wx.TE_RICH)
shuru2=wx.TextCtrl(panel,pos=(50,130),style=wx.TE_RICH)
sgshuru=wx.StaticText(panel,pos=(10,100),label="身高")
tzshuru=wx.StaticText(panel,pos=(10,130),label="体重")
tishi0=wx.StaticText(panel,pos=(10,230),label="提示:")
tishi1=wx.StaticText(panel,pos=(10,260),label="1,红色类,代表女生")
tishi1.SetForegroundColour("red")
tishi1.SetFont(wx.Font(13,wx.SWISS,wx.NORMAL,wx.NORMAL))
tishi2=wx.StaticText(panel,pos=(10,290),label="2,绿色类,代表男生")
tishi2.SetForegroundColour("green")
tishi2.SetFont(wx.Font(13,wx.SWISS,wx.NORMAL,wx.NORMAL))
tishi3=wx.StaticText(panel,pos=(10,320),label="3,黑色类,代表输入个人数据")
tishi3.SetFont(wx.Font(13,wx.SWISS,wx.NORMAL,wx.NORMAL))
tishi3.SetForegroundColour("black")
huitu1Button.Bind(wx.EVT_BUTTON,ceshi1)
huituButton.Bind(wx.EVT_BUTTON,ceshi2)
app.MainLoop()
没有合适的资源?快使用搜索试试~ 我知道了~
Sigmoid回归实现分类-带gui显示,下载就能使用
共5个文件
suo:1个
csv:1个
py:1个
需积分: 0 0 下载量 75 浏览量
2024-05-24
17:15:59
上传
评论
收藏 7KB RAR 举报
温馨提示
Sigmoid回归(Sigmoid Regression)实际上并不是一个标准的回归算法,它更常被用于解决二分类问题。Sigmoid函数(或称为逻辑函数)通常与线性回归结合使用,将线性回归的输出转换为一个介于0和1之间的概率值,从而进行二分类。
资源推荐
资源详情
资源评论
收起资源包目录
基于面向对象逻辑斯蒂回归分类器算法的男女同学分类器-带GUI显示.rar (5个子文件)
基于面向对象逻辑斯蒂回归分类器算法的男女同学分类器-带GUI显示
源代码
期末考试
new.csv 1018B
期末考试.pyproj 2KB
期末考试.py 4KB
.vs
期末考试
v15
.suo 19KB
期末考试.sln 971B
共 5 条
- 1
资源评论
赵药师
- 粉丝: 241
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功