资源简介
逻辑斯蒂回归,用Python语言写的,比较简单,在小数据集上准确率为100%,资源中包含完整代码及测试数据
代码片段和文件信息
#Logistic.py
#2015-01-12
#Logistics终止条件w变化阈值对结果有着重大影响
import logging
import math
logging.basicConfig(level=logging.DEBUG
format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘
datefmt=‘%a %d %b %Y %H:%M:%S‘
filename=‘LosisticRegression.log‘
filemode=‘a+‘)
def Sampling(FileName TrainingSet TestSet):
fin = open(FileName ‘r‘)
fout1 = open(TrainingSet ‘w‘)
fout2 = open(TestSet ‘w‘)
i = 1
for x in fin:
if 0 == i%3:
fout2.write(x)
else:
fout1.write(x)
i = i+1
fin.close()
fout1.close()
fout2.close()
class DataProcess(object):
def __init__(self FileName):
self.__FileName = FileName
self.__Sample = []
self.__IsLoaded = False
def LoadData(self Separator = ‘‘):
try:
file = open(self.__FileName ‘r‘)
except FileNotFoundError:
logging.error(‘Can not find file %s‘ % self.__FileName )
for x in file:
self.__Sample.append(self.TypeParse(x.split(Separator)))
self.IsLoaded = True
def TypeParse(self Item):
result = []
for x in Item:
try:
result.append(float(x))
except ValueError:
if x.endswith(‘\n‘):
result.append(x.replace(‘\n‘ ‘‘))
else:
result.append(x)
return result
def PreProcess(self AttrID = [0]):
if self.__IsLoaded == False:
self.LoadData()
for x in self.__Sample:
for y in AttrID:
x.pop(y)
def GetData(self):
return self.__Sample
class LogisticRegression(object):
def __init__(self FileNameStepSize = 0.5Epsilon = 0.01):
dp = DataProcess(FileName)
dp.PreProcess([])
self.__StepSize = StepSize
self.__Epsilon = Epsilon
self.__Sample = dp.GetData()
self.__AttrNum = len(self.__Sample[0])
self.__Weight = [0.5]*(self.__AttrNum-1)
TempClassID = self.__GetClassID()
if len(TempClassID) != 2:
logging.error(‘Logistic regression is just for two classification‘)
return
self.__ClassIDMap = {TempClassID[0]:1TempClassID[1]:-1}
#暂时没啥用
def __Normalize(self):
for i in range(0self.__AttrNum-1):
Attr = [x[i] for x in self.__Sample]
MaxVal = max(Attr)
MinVal = min(Attr)
for j in range(0len(self.__Sample)):
self.__Sample[j][i] = (self.__Sample[j][i]-MinVal)/(MaxVal-MinVal)*2-1
def __GetClassID(self):
occure = []
for x in self.__Sample:
if x[self.__AttrNum-1] not in occure:
occure.append(x[self.__AttrNum-1])
return occure
def LRLeaner(self):
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4807 2015-01-16 16:11 Losisitc.py
文件 1025 2015-01-12 13:10 Iris_TestSet.txt
文件 2073 2015-01-12 13:10 Iris_TrainingSet.txt
----------- --------- ---------- ----- ----
7905 3
相关资源
- python机器学习Sebastian Raschka中文最新完
- Python-DeepMoji模型的pyTorch实现
- 《机器学习实战》源代码Python3
- Python-使用DeepFakes实现YouTube视频自动换
- Introduction to machine learning with python (
- python新浪微博爬虫,爬取微博和用户
- Python-一系列高品质的动漫人脸数据集
- Python-Insightface人脸检测识别的最小化
- 非线性回归Python代码
- 093 2018北风网人工智能视频(完结)转
- python的色情图片识别
- 贝叶斯网络程序
- 《机器学习实战》Python3代码
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
- Python-用PyTorch10实现FasterRCNN和MaskRCNN比
- Python-心脏核磁共振MRI图像分割
- Python-基于YOLOv3的行人检测
- Python-RLSeq2Seq用于SequencetoSequence模型的
- Python-PyTorch对卷积CRF的参考实现
- Python-高效准确的EAST文本检测器的一个
- Python-pytorch实现的人脸检测和人脸识别
- Python-UNet用于医学图像分割的嵌套UN
- Python-TensorFlow弱监督图像分割
- Python-基于tensorflow实现的用textcnn方法
- Python-Keras实现Inceptionv4InceptionResnetv1和
- Python-pytorch中文手册
- Python-FastSCNN的PyTorch实现快速语义分割
- Python-滑动窗口高分辨率显微镜图像分
评论
共有 条评论