(dataMatrix, classLabels, numIter=150)
| 37 | return weights |
| 38 | |
| 39 | def stocGradAscent1(dataMatrix, classLabels, numIter=150): |
| 40 | m, n = shape(dataMatrix) |
| 41 | weights = ones(n) |
| 42 | for i in range(numIter): |
| 43 | dataIndex = len(list(range(m))) |
| 44 | for j in range(m): |
| 45 | alpha = 4/(1.0+i+j)+0.01 |
| 46 | randIndex = int(random.uniform(0, dataIndex)) |
| 47 | h = sigmoid(sum(dataMatrix[randIndex]*weights)) |
| 48 | error = classLabels[randIndex] - h |
| 49 | weights = weights + alpha * error * dataMatrix[randIndex] |
| 50 | dataIndex -= 1 |
| 51 | return weights |
| 52 | |
| 53 | def plotBestFit(weights): |
| 54 | import matplotlib.pyplot as plt |
no test coverage detected