-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3215b2e
commit 7330a1b
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>LogisticRegression</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.python.pydev.PyDevBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.python.pydev.pythonNature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<?eclipse-pydev version="1.0"?> | ||
|
||
<pydev_project> | ||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH"> | ||
<path>/LogisticRegression</path> | ||
</pydev_pathproperty> | ||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property> | ||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property> | ||
</pydev_project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import matplotlib.pyplot as plt | ||
from numpy import * | ||
|
||
|
||
def loadDataSet(): | ||
dataMat = []; labelMat = [] | ||
fr = open('/Users/hakuri/Desktop/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,labelMat | ||
|
||
def sigmoid(inX): | ||
return 1.0/(1+exp(-inX)) | ||
|
||
def gradAscent(dataMatIn, classLabels): | ||
dataMatrix = mat(dataMatIn) #convert to NumPy matrix | ||
labelMat = mat(classLabels).transpose() #convert to NumPy matrix | ||
|
||
m,n = shape(dataMatrix) | ||
alpha = 0.001 | ||
maxCycles = 500 | ||
weights = ones((n,1)) | ||
|
||
for k in range(maxCycles): #heavy on matrix operations | ||
h = sigmoid(dataMatrix*weights) #matrix mult | ||
error = (labelMat - h) #vector subtraction | ||
weights = weights + alpha * dataMatrix.transpose()* error #matrix mult | ||
return weights | ||
|
||
def GetResult(): | ||
dataMat,labelMat=loadDataSet() | ||
weights=gradAscent(dataMat,labelMat) | ||
print weights | ||
plotBestFit(weights) | ||
|
||
|
||
def plotBestFit(weights): | ||
|
||
dataMat,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=(0.48*x+4.12414)/(0.616) | ||
# y = (-weights[0]-weights[1]*x)/weights[2] | ||
ax.plot(x,y) | ||
plt.xlabel('X1'); plt.ylabel('X2'); | ||
plt.show() | ||
|
||
if __name__=='__main__': | ||
GetResult() |