logsic 回归
?????? logstic 回歸 ,從本質(zhì)上說,就是一個單層感知器,僅此而已。 一個輸入層 ,一個層激活的神經(jīng)網(wǎng)絡(luò)。
SVM ,一個輸入層 ,一個隱藏層(核函數(shù)),一個激活的神經(jīng)網(wǎng)絡(luò)。
所以,在當(dāng)下所有的過往的機器學(xué)習(xí)算法,都無法與深度學(xué)習(xí)相提并論!,你們都過時了!
但是,經(jīng)過logistic變換,自變量為負(fù)無窮到正無窮,并且輸出值即是屬于某一類的概率。數(shù)學(xué)概念清晰。在很多淺薄,SB的領(lǐng)域,智力水平的低的種族中還有一定的應(yīng)用。
from numpy import *def loadDataSet():dataMat = []; labelMat = []fr = open('testSet.txt')for line in fr.readlines():lineArr = line.strip().split()dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])labelMat.append(int(lineArr[2]))return dataMat,labelMatdef sigmoid(inX):return 1.0/(1+exp(-inX))def gradAscent(dataMatIn, classLabels):dataMatrix = mat(dataMatIn) #convert to NumPy matrixlabelMat = mat(classLabels).transpose() #convert to NumPy matrixm,n = shape(dataMatrix)alpha = 0.001maxCycles = 5000weights = ones((n,1))for k in range(maxCycles): #heavy on matrix operationsh = sigmoid(dataMatrix*weights) #matrix multerror = (labelMat - h) #vector subtractionweights = weights + alpha * dataMatrix.transpose()* error #matrix multreturn weightsdef plotBestFit(weights):import matplotlib.pyplot as pltdataMat,labelMat=loadDataSet()dataArr = array(dataMat)n = shape(dataArr)[0] xcord1 = []; ycord1 = []xcord2 = []; ycord2 = []for i in range(n):if int(labelMat[i])== 1:xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2])else:xcord2.append(dataArr[i,1]); ycord2.append(dataArr[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 = 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()def stocGradAscent0(dataMatrix, classLabels):m,n = shape(dataMatrix)alpha = 0.2weights = ones(n) #initialize to all onesfor i in range(m):h = sigmoid(dataMatrix[i].T@ weights) # #h = sigmoid(sum(dataMatrix[i]*weights)) error = classLabels[i] - hweights = weights + alpha * error * dataMatrix[i]return weightsdef stocGradAscent1(dataMatrix, classLabels, numIter=150):m,n = shape(dataMatrix)weights = ones(n) #initialize to all onesfor j in range(numIter):dataIndex = range(m)for i in range(m):alpha = 4/(1.0+j+i)+0.0001 #apha decreases with iteration, does not randIndex = int(random.uniform(0,len(dataIndex)))#go to 0 because of the constanth = sigmoid(dataMatrix[randIndex].T@ weights) #h = sigmoid(sum(dataMatrix[i]*weights)) error = classLabels[randIndex] - hweights = weights + alpha * error * dataMatrix[randIndex]del(randIndex)return weightsdef classifyVector(inX, weights):prob = sigmoid(sum(inX*weights))if prob > 0.5: return 1.0else: return 0.0def colicTest():frTrain = open('horseColicTraining.txt'); frTest = open('horseColicTest.txt')trainingSet = []; trainingLabels = []for line in frTrain.readlines():currLine = line.strip().split('\t')lineArr =[]for i in range(21):lineArr.append(float(currLine[i]))trainingSet.append(lineArr)trainingLabels.append(float(currLine[21]))trainWeights = stocGradAscent1(array(trainingSet), trainingLabels, 1000)errorCount = 0; numTestVec = 0.0for line in frTest.readlines():numTestVec += 1.0currLine = line.strip().split('\t')lineArr =[]for i in range(21):lineArr.append(float(currLine[i]))if int(classifyVector(array(lineArr), trainWeights))!= int(currLine[21]):errorCount += 1errorRate = (float(errorCount)/numTestVec)print ("the error rate of this test is: %f" % errorRate)return errorRatedef multiTest():numTests = 10; errorSum=0.0for k in range(numTests):errorSum += colicTest()print ("after %d iterations the average error rate is: %f" % (numTests, errorSum/float(numTests)))''' import logRegres dataArr,labelMat=logRegres.loadDataSet() logRegres.gradAscent(dataArr,labelMat) ''''''from numpy import *weights=logRegres.gradAscent(dataArr,labelMat)logRegres.plotBestFit(weights.getA()) '''''' from numpy import * import logRegres dataArr,labelMat=logRegres.loadDataSet() weights=logRegres.stocGradAscent0(array(dataArr),labelMat) logRegres.plotBestFit(weights) '''''' from numpy import * import logRegres dataArr,labelMat=logRegres.loadDataSet() weights=logRegres.stocGradAscent1(array(dataArr),labelMat) logRegres.plotBestFit(weights) ''' import logRegres dataArr,labelMat=logRegres.loadDataSet() logRegres.gradAscent(dataArr,labelMat)matrix([[ 4.12414349],[ 0.48007329],[-0.6168482 ]]) from numpy import * weights=logRegres.gradAscent(dataArr,labelMat) logRegres.plotBestFit(weights.getA()) from numpy import * import logRegres dataArr,labelMat=logRegres.loadDataSet() weights=logRegres.stocGradAscent0(array(dataArr),labelMat) logRegres.plotBestFit(weights.getA()) from numpy import * import logRegres dataArr,labelMat=logRegres.loadDataSet() weights=logRegres.stocGradAscent1(array(dataArr),labelMat) logRegres.plotBestFit(weights) import logRegres logRegres.multiTest() return 1.0/(1+exp(-inX)) the error rate of this test is: 0.283582 the error rate of this test is: 0.388060 the error rate of this test is: 0.313433 the error rate of this test is: 0.432836 the error rate of this test is: 0.358209 the error rate of this test is: 0.328358 the error rate of this test is: 0.208955 the error rate of this test is: 0.253731 the error rate of this test is: 0.373134 the error rate of this test is: 0.447761 after 10 iterations the average error rate is: 0.338806 ????我靠,這么好,如此有規(guī)律的數(shù)據(jù)集,你的誤差這么大,%33.8啊!!!!
正確率只有%66.2,太搞笑了!
????
這是對如此完美數(shù)據(jù)集的無恥浪費!!!!!!
代碼鏈接 提取密碼為 6noo
github下載
總結(jié)
- 上一篇: bayes
- 下一篇: 神经网络贷款风险评估(base on k