改寫自此python code,因為網路上流傳太多版本大陸互相亂爬導致到處都有,找不到出處故只附此code,若侵犯版權請盡速與我聯繫。
from matplotlib import pyplot as plt import numpy as np import random
def sigmoid(inX): return 1.0 / (1 + np.exp(-inX))
def gradAscent(dataMatIn, classLabels): dataMatrix = np.mat(dataMatIn) labelMat = np.mat(classLabels).transpose() m, n = dataMatrix.shape alpha = 0.01 maxCycles = 500 weights = np.ones((n, 1)) for k in range(maxCycles): h = sigmoid(dataMatrix * weights) error = (labelMat - h) weights = weights + alpha * dataMatrix.transpose() * error return weights
|
下面是改寫後的 java code,按照註解來,使用上只要動到訓練和預測的部分就可以了。
效能比不上有照論文實作優化過的,但我覺得是很好的學習範例,畢竟邏輯回歸可以當分類和回歸用,可以都玩玩看啦,希望大家玩得開心也得到一些收穫。
package afan;
import java.util.Arrays; import java.util.Random;
public class LogisticRegression {
public static double sigmoid(double num) { return 1.0 / (1 + Math.exp(-num)); }
public static double sumMatrixRowMutiWeight(double[][] datas, int inx, double[] weights) {
double res = 0;
for (int i = 0; i < datas.length; i++) { res += datas[inx][i] * weights[i]; }
return res; }
public static double[] adjustWeights(double[][] datas, int inx, double[] weights, double alpha , double error) {
double[] res = new double[weights.length];
for (int i = 0; i < weights.length; i++) { res[i] = alpha * error * datas[inx][i] + weights[i]; }
return res; }
public static double[] stocGradAscentl(double[][] datas, double[] labels) {
Random r = new Random(); double[] res = new double[datas[0].length];
Arrays.fill(res, 1);
for (int i = 0; i < 100; i++) { for (int j = 0; j < datas.length; j++) { double alpha = 4 / (1.0 + j + i) + 0.01; int randIndex = r.nextInt(datas.length); double h = sigmoid(sumMatrixRowMutiWeight(datas, randIndex, res)); double error=labels[randIndex]-h; res = adjustWeights(datas, randIndex, res, alpha,error); } }
return res; }
public static double[] stocGradAscentl(double[][] datas, double[] labels, int maxIter) {
Random r = new Random(); double[] res = new double[datas[0].length];
Arrays.fill(res, 1);
for (int i = 0; i < maxIter; i++) { for (int j = 0; j < datas.length; j++) { double alpha = 4 / (1.0 + j + i) + 0.01; int randIndex = r.nextInt(datas.length); double h = sigmoid(sumMatrixRowMutiWeight(datas, randIndex, res)); double error=labels[randIndex]-h; res = adjustWeights(datas, randIndex, res, alpha,error); } }
return res; }
public static double predictionProbability(double[] feature, double[] weights) {
double prob = 0;
for (int i = 0; i < feature.length; i++) { prob += feature[i] * weights[i]; }
prob = sigmoid(prob);
return prob; }
public static double prediction(double[] feature, double[] weights) {
double prob = 0;
for (int i = 0; i < feature.length; i++) { prob += feature[i] * weights[i]; }
prob = sigmoid(prob);
return prob>=0.5?1:0; } }
|