日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习实战之logistic回归分类

發(fā)布時間:2023/12/6 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习实战之logistic回归分类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

利用logistic回歸進(jìn)行分類的主要思想:根據(jù)現(xiàn)有數(shù)據(jù)對分類邊界建立回歸公式,并以此進(jìn)行分類。

?

logistic優(yōu)缺點:

優(yōu)點:計算代價不高,易于理解和實現(xiàn)。
缺點:容易欠擬合,分類精度可能不高。 .
適用數(shù)據(jù)類型:數(shù)值型和標(biāo)稱型數(shù)據(jù)。

?

sigmoid函數(shù):

?

?

梯度上升法:

梯度:

該公式將一直被迭代執(zhí)行,直至達(dá)到某個停止條件為止,比如迭代次數(shù)達(dá)到某個指定值或算
法達(dá)到某個可以允許的誤差范圍。

隨機(jī)梯度上升法:

?梯度上升算法在每次更新回歸系數(shù)時都需要遍歷整個數(shù)據(jù)集, 該方法在處理100個左右的數(shù)
據(jù)集時尚可,但如果有數(shù)十億樣本和成千上萬的特征,那么該方法的計算復(fù)雜度就太高了。一種
改進(jìn)方法是一次僅用一個樣本點來更新回歸系數(shù),該方法稱為隨機(jī)梯度上升算法。由于可以在新
樣本到來時對分類器進(jìn)行增量式更新,因而隨機(jī)梯度上升算法是一個在線學(xué)習(xí)算法。與 “ 在線學(xué)
習(xí)相對應(yīng),一次處理所有數(shù)據(jù)被稱作是批處理” 。

梯度下降法:

你最經(jīng)常聽到的應(yīng)該是梯度下降算法,它與這里的梯度上升算法是一樣的,只是公式中的
加法需要變成減法。因此,對應(yīng)的公式可以寫成:

?

梯度上升算法用來求函數(shù)的最大值,而梯度下降算法用來求函數(shù)的最小值。

?

logistic預(yù)測疝氣病預(yù)測病馬的死亡率代碼:

%matplotlib inline import matplotlib.pyplot as plt import numpy as np import random# 加載數(shù)據(jù)集 def loadDataSet():dataMat = []labelMat = []fr = open('./testSet.txt')for line in fr.readlines():lineData = line.strip().split()dataMat.append([1.0, float(lineData[0]), float(lineData[1])])labelMat.append(int(lineData[2]))return dataMat, labelMat# sigmoid 函數(shù) def sigmoid(inX):return 1.0 / (1 + np.exp(-inX))# 梯度上升 def gradAscent(dataMatIn, classLabels, maxCycles):dataMatrix = np.mat(dataMatIn)labelsMatrix = np.mat(classLabels).transpose() # 轉(zhuǎn)置,將行向量轉(zhuǎn)置為列向量m, n = np.shape(dataMatrix)alpha = 0.001W = np.ones((n, 1))for i in range(maxCycles):h = sigmoid(dataMatrix * W) # (100, 1)error = labelsMatrix - h # (100, 1)W = W + alpha * dataMatrix.transpose() * error # (3, 100) * (100, 1)return W #改進(jìn)版隨機(jī)梯度上升 def stocGradAscent1(dataMatrixIn, classLabels, numIter=150):dataMatrix = np.array(dataMatrixIn)m,n = np.shape(dataMatrix)weights = np.ones(n) #initialize to all onesfor j in range(numIter):dataIndex = list(range(m))for i in range(m):alpha = 4.0/(1.0+j+i)+0.01 #apha decreases with iteration, does not randIndex = int(random.uniform(0,len(dataIndex)))#go to 0 because of the constanth = sigmoid(sum(dataMatrix[randIndex]*weights))error = classLabels[randIndex] - hweights = weights + alpha * error * dataMatrix[randIndex]del(dataIndex[randIndex])return np.mat(weights.reshape(n, 1))def plotBestFit(weights, dataMat, labelMat):dataArr = np.array(dataMat)n = np.shape(dataArr)[0]xcord1 = []; ycord1 = []xcord2 = []; ycord2 = []for i in range(n):if 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 = np.arange(-4.0, 4.0, 0.1)y = ((np.array((-weights[0] - weights[1] * x) / weights[2]))[0]).transpose()ax.plot(x, y)plt.xlabel('X1')plt.ylabel('X2')plt.show()# 預(yù)測 def classifyVector(inX, weights):prob = sigmoid(sum(inX * weights))if prob > 0.5:return 1.0else:return 0.0# 對訓(xùn)練集進(jìn)行訓(xùn)練,并且對測試集進(jìn)行測試 def colicTest():trainFile = open('horseColicTraining.txt')testFile = open('horseColicTest.txt')trainingSet = []; trainingLabels = []for line in trainFile.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]))# 開始訓(xùn)練weights = stocGradAscent1(trainingSet, trainingLabels, 400)errorCount = 0.0numTestVec = 0.0for line in testFile.readlines():numTestVec += 1.0currLine = line.strip().split('\t')lineArr = []for i in range(21):lineArr.append(float(currLine[i]))if int(classifyVector(np.array(lineArr), weights)) != int(currLine[21]):errorCount += 1.0errorRate = errorCount / float(numTestVec)print("the error rate is:%f" % errorRate)return errorRate# 多次測試求平均值 def multiTest():testTimes = 10errorRateSum = 0.0for i in range(testTimes):errorRateSum += colicTest()print("the average error rate is:%f" % (errorRateSum / float(testTimes)))multiTest()

?

轉(zhuǎn)載于:https://www.cnblogs.com/qiang-wei/p/10770285.html

總結(jié)

以上是生活随笔為你收集整理的机器学习实战之logistic回归分类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。