Home > other >  Pycharm cannot run, there is a small red fork fork, don't know what reason
Pycharm cannot run, there is a small red fork fork, don't know what reason

Time:10-20

- * - coding: utf-8 - * -
# import in the realization of support vector machine (SVM) classification used in the process of module
The from numpy import *
The import matplotlib
The import matplotlib. Pyplot as PLT
# loading data sets, open the file and read line by line, the first two columns # dataMat features stored in the matrix, the last column will be stored in the matrix labelMat category, to get the whole data moment # matrix and each category labels,
Def loadDataSet (fileName) :
DataMat=[]
LabelMat=[]
Fr=open (fileName)
For the line in fr. Readlines () :
LineArr=line. Strip (). The split (', ')
DataMat. Append ([float (lineArr [0]), the float (lineArr [1])])
LabelMat. Append (float (lineArr [1]))
Return dataMat, labelMat
# random selection second to optimize parameter alpha
Def selectJrand (I, m) :
J=I
While (j==I) :
J=int (random uniform (0, m))
Return j
# adjustment is greater than or less than L H alpha value,
Def clipAlpha (aj, H, L) :
If aj & gt; H:
Aj=H
If L & gt; Aj:
Aj=L
Return aj
# definition contains five input parameters of the SMO function, data sets, respectively, the class label, punish parameters, allow
# error rate, maximum number of iterations,
Def smoSimple (dataMatIn classLabels, C, toler, maxIter) :
# create alpha vector, of which all elements are initialized to 0, set up the updated control count iter is 0,
DataMatrix=mat (dataMatIn)
LabelMat=mat (classLabels). Transpose ()
B=0
M, n=shape (dataMatrix)
Alphas=mat (zeros ((m, 1)))
Iter=0
# outer loop, iter less than maxIter executed when the outer loop, as successive iteration maxIter times without any alpha value change, didn't end the outer loop,
While (iter & lt; MaxIter) :
# alphaPairsChanged said count, whether there is alpha value is used to record the current loop changes,
# at the beginning, such as reduced to 0, if there are alpha value changes of the count + 1 operation,
AlphaPairsChanged=0
# inner circulation, each data vector of data set, if the data of vector calculation error is large, # show its corresponding alpha value can be optimized, and randomly choose another vector data, modify the corresponding
Alpha value to #,
For I in range (m) :
Looking=float (multiply (alphas, labelMat.) T * (dataMatrix * dataMatrix [I:]. T)) + b
Ei=looking - float (labelMat [I])
If ((labelMat [I] * Ei & lt; - toler) and (alphas [I] J=selectJrand (I, m)
FXj=float (multiply (alphas, labelMat.) T * (dataMatrix * dataMatrix [r]. J: T)) + b
Ej=fXj - float (labelMat [j])
AlphaIold=alphas [I]. Copy ()
AlphaJold=alphas [j]. Copy ()
If (labelMat [I]!=labelMat [j]) :
L=Max (0, alphas [j] - alphas [I])
H=min (C, C + alphas [j] - alphas [I])
The else:
  • Related