image.png
    image.png
    image.png

    1. def loadDataSet():
    2. dataMat = []; labelMat = []
    3. fr = open('4-Logistic/testSet.txt')
    4. for line in fr.readlines():
    5. lineArr = line.strip().split()
    6. dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
    7. labelMat.append(int(lineArr[2]))
    8. fr.close()
    9. return dataMat, labelMat
    10. def sigmoid(inX):
    11. return 1.0 / (1 + np.exp(-inX))
    12. def gradAscent(dataMatIn, classLabels):
    13. dataMatrix = np.mat(dataMatIn)
    14. labelMat = np.mat(classLabels).transpose()
    15. m, n = np.shape(dataMatrix)
    16. alpha = 0.001
    17. maxCycles = 500
    18. weights = np.ones((n,1))
    19. for k in range(maxCycles):
    20. h = sigmoid(dataMatrix * weights)
    21. error = labelMat - h
    22. weights = weights + alpha * dataMatrix.transpose() * error
    23. return weights.getA()
    24. def plotBestFit(wei):
    25. dataMat, labelMat = loadDataSet()
    26. dataArr = np.array(dataMat)
    27. n = np.shape(dataMat)[0]
    28. xcord1 = []; ycord1 = []
    29. xcord2 = []; ycord2 = []
    30. for i in range(n):
    31. if int(labelMat[i]) == 1:
    32. xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2])
    33. else:
    34. xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2])
    35. fig = plt.figure()
    36. ax = fig.add_subplot(111)
    37. ax.scatter(xcord1, ycord1, s = 20, c = 'red', marker = 's',alpha=.5)
    38. ax.scatter(xcord2, ycord2, s = 20, c = 'green',alpha=.5)
    39. x = np.arange(-3.0, 3.0, 0.1)
    40. y = (-wei[0] - wei[1] * x) / wei[2]
    41. ax.plot(x, y)
    42. plt.title('BestFit')
    43. plt.xlabel('X1'); plt.ylabel('X2')
    44. plt.show()
    45. def stocGradAscentO(dataMatrix, classLabels):
    46. dataMatrix=np.array(dataMatrix)
    47. m,n = np.shape(dataMatrix)
    48. alpha = 0.01
    49. weights = np.ones(n)
    50. for i in range(m):
    51. h = sigmoid(sum(dataMatrix[i]*weights))
    52. error = classLabels[i] - h
    53. weights = weights + alpha * error * dataMatrix[i]
    54. return weights
    55. def stocGradAscent1(dataMatrix, classLabels, numIter=150):
    56. dataMatrix=np.array(dataMatrix)
    57. m,n = np.shape(dataMatrix)
    58. weights = np.ones(n)
    59. for j in range(numIter):
    60. dataIndex = list(range(m))
    61. for i in range(m):
    62. alpha = 4/(1.0+j+i)+0.01
    63. randIndex = int(random.uniform(0,len(dataIndex)))
    64. h = sigmoid(sum(dataMatrix[randIndex]*weights))
    65. error = classLabels[randIndex] - h
    66. weights = weights + alpha * error * dataMatrix[randIndex]
    67. del(dataIndex[randIndex])
    68. return weights