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'模型準確率為:{acc}')
- # return retest
- # print(get_acc(train,test,alpha=0.001,maxCycles=5000))
- #
復制代碼 全部資料51hei下載地址:
邏輯回歸算法.zip
(15.02 KB, 下載次數: 12)
2020-12-18 11:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|