標(biāo)題:
邏輯回歸算法 Python源程序
[打印本頁(yè)]
作者:
體驗(yàn)地球生活
時(shí)間:
2020-12-18 11:03
標(biāo)題:
邏輯回歸算法 Python源程序
Python源程序如下:
import pandas as pd
import numpy as np
test = pd.read_table('horseColicTest.txt',header=None)
train = pd.read_table('horseColicTraining.txt',header=None)
train.head()
train.shape
train.info()
print(train.head())
# print(test)
# print(train)
#
# def sigmoid(inX):
# s = 1/(1+np.exp(-inX))
# return s
# def classify(inX,weight):
# p = sigmoid(sum(inX*weight))
# if p <0.5:
# return 0
# else:
# return 1
# def regularize(xMat):
# inMat = xMat.copy()
# inMeans = np.mean(inMat,axis = 0)
# inVar = np.std(inMat,axis = 0)
# inMat = (inMat-inMeans)/inVar
# return inMat
# def SGD_LR(dataSet,alpha=0.001,maxCycles=500):
# dataSet = dataSet.sample(maxCycles,replace=True)
# dataSet.index = range(dataSet.shape[0])
# xMat = np.mat(dataSet.iloc[:,:-1].values)
# yMat = np.mat(dataSet.iloc[:,-1].values).T
# xMat = regularize(xMat)
# m,n = xMat.shape
# weights = np.zeros((n,1))
# for i in range(m):
# grad = xMat[i].T*(xMat[i]*weights-yMat[i])
# weights = weights-alpha*grad
# return weights
#
# def get_acc(train,test,alpha=0.001,maxCycles=5000):
# weights = SGD_LR(train,alpha,maxCycles=maxCycles)
# xMat = np.mat(test.iloc[:,:-1].values)
# xMat = regularize(xMat)
# result = []
# for inX in xMat:
# label = classify(inX,weights)
# result = []
# for inX in xMat:
# label = classify(inX,weights)
# result.append(label)
# retest = test.copy()
# retest['predict']=result
# acc= (retest.iloc[:,-1]==retest.iloc[:,-2]).mean()
# print(f'模型準(zhǔn)確率為:{acc}')
# return retest
# print(get_acc(train,test,alpha=0.001,maxCycles=5000))
#
復(fù)制代碼
全部資料51hei下載地址:
邏輯回歸算法.zip
(15.02 KB, 下載次數(shù): 12)
2020-12-18 11:02 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1