(weights)
| 51 | return weights |
| 52 | |
| 53 | def plotBestFit(weights): |
| 54 | import matplotlib.pyplot as plt |
| 55 | dataMat,labelMat=loadDataSet() |
| 56 | dataArr = array(dataMat) |
| 57 | n = shape(dataArr)[0] |
| 58 | xcord1 = []; ycord1 = [] |
| 59 | xcord2 = []; ycord2 = [] |
| 60 | for i in range(n): |
| 61 | if int(labelMat[i])== 1: |
| 62 | xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2]) |
| 63 | else: |
| 64 | xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2]) |
| 65 | fig = plt.figure() |
| 66 | ax = fig.add_subplot(111) |
| 67 | ax.scatter(xcord1, ycord1, s=30, c='red', marker='s') |
| 68 | ax.scatter(xcord2, ycord2, s=30, c='green') |
| 69 | if weights is not None: |
| 70 | x = arange(-3.0, 3.0, 0.1) |
| 71 | y = (-weights[0]-weights[1]*x)/weights[2] #令w0*x0 + w1*x1 + w2*x2 = 0,其中x0=1,解出x1和x2的关系 |
| 72 | ax.plot(x, y) #一个作为X一个作为Y,画出直线 |
| 73 | plt.xlabel('X1'); plt.ylabel('X2'); |
| 74 | plt.show() |
| 75 | |
| 76 | def classifyVector(inX, weights): |
| 77 | prob = sigmoid(sum(inX*weights)) |
nothing calls this directly
no test coverage detected