Repository: llp1992/MachineLearning
Branch: master
Commit: 315c00285b75
Files: 58
Total size: 120.4 KB
Directory structure:
gitextract_cc07p_s4/
├── Adaboost/
│ ├── README.md
│ ├── adaboost.py
│ └── testAdaboost.py
├── Decision-Tree/
│ ├── README.md
│ ├── TestTree.py
│ └── Tree.py
├── DeepLearning/
│ ├── CNN_cifar-10/
│ │ └── cifar.py
│ ├── CNN_mnist/
│ │ ├── cnn.py
│ │ ├── data.py
│ │ └── trainCNN.py
│ └── UFLDL/
│ ├── Vectorization_sparseae_exercise/
│ │ ├── checkNumericalGradient.m
│ │ ├── computeNumericalGradient.m
│ │ ├── display_network.m
│ │ └── initializeParameters.m
│ └── stl_exercise/
│ ├── display_network.m
│ ├── feedForwardAutoencoder.m
│ ├── initializeParameters.m
│ ├── loadMNISTImages.m
│ ├── loadMNISTLabels.m
│ ├── softmaxCost.m
│ ├── softmaxPredict.m
│ ├── softmaxTrain.m
│ ├── sparseAutoencoderCost.m
│ └── stlExercise.m
├── GMM/
│ ├── README.md
│ ├── gmm.m
│ ├── gmm.py
│ ├── testGMM.m
│ └── testSet.txt
├── KNN/
│ ├── KNN.m
│ ├── KNN.py
│ ├── KNNdatgingTest.m
│ ├── README.md
│ ├── datingTestSet2.txt
│ └── handWritingTest.m
├── Kmeans/
│ ├── README.md
│ ├── distEclud.m
│ ├── kMeans.m
│ ├── testSet.txt
│ └── testkMeans.m
├── Logistic-regression/
│ ├── ImproveStocGradAscent.m
│ ├── README.md
│ ├── gradAscent.m
│ ├── stocGradAscent.m
│ └── testSet.txt
├── MLP/
│ ├── dualperceptron.py
│ ├── perceptron.py
│ └── testSet.txt
├── PCA/
│ ├── PCA.m
│ ├── README.md
│ ├── testPCA.m
│ └── testSet.txt
├── README.md
├── bikMeans/
│ ├── README.md
│ ├── bikMeans.m
│ └── testSet.txt
└── kalmanFilter/
├── KF.m
└── kalmanFiltering.m
================================================
FILE CONTENTS
================================================
================================================
FILE: Adaboost/README.md
================================================
Adaboost 算法实现。
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: Adaboost/adaboost.py
================================================
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 12:42:48 2015
@author: liu
"""
from numpy import *
def loadSimpData():
dataMat = array([[1.,2.1],[1.5,1.6],[1.3,1.],[1.,1.],[2.,1.],[1.2,1.1],\
[1.1,0.4],[0.9,1.3],[0.86,1.2],[1.8,1.8],[1.7,1.5],[1.9,1.8]])
classLabels = array([1.0,1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,\
-1.0,1.0,1.0,1.0])
return dataMat,classLabels
# 单树桩分类器,也就是简单的单层决策树弱分类器
# 该函数根据某个最好的特征的最好划分点对数据进行分类
# demen就是特征,threshVal就是划分点,retArray就是返回的分类结果
def stumpClassify(dataMatrix,dimen,threshVal,threshIneq):
retArray = ones((shape(dataMatrix)[0],1))
if threshIneq == 'lt':
retArray[dataMatrix[:,dimen]<=threshVal] = -1.0
else:
retArray[dataMatrix[:,dimen]>threshVal] = -1.0
return retArray
# 创建树桩,返回最好的树桩和该树桩的分类最小误差以及分类结果
# 该函数用于从数据集中找到最好的划分特征以及该特征的最好划分点
def buildStump(dataArr,classLabels,D):
# 建立备份的数据
dataMatrix = mat(dataArr);
labelMat = mat(classLabels).T
# m 是行,也就是每个样本,n是列,也就是每个特征
m,n = shape(dataMatrix)
# 步数的设置,也就是在每个特征的最大值和最小值中分几次设置阈值进行数据划分
# 也就是获取这个特征的最佳划分点,步数越高,特征点寻找得越精细,但耗时更多
# 用字典来存储bestStump的数据
numSteps = 10.0;bestStump = {};bestClassEst = mat(zeros((m,1)))
minError = inf
# 对每个特征
for i in range(n):
# 获取每个特征的最小值和最大值
rangeMin = dataMatrix[:,i].min();
rangeMax = dataMatrix[:,i].max();
stepSize = (rangeMax-rangeMin)/numSteps
for j in range(-1,int(numSteps)+1):
for inequal in ['lt','gt']:
threshVal = (rangeMin + float(j) * stepSize)
# 预测值
predictedVals = stumpClassify(dataMatrix,i,threshVal,inequal)
errArr = mat(ones((m,1)))
errArr[predictedVals == labelMat] = 0
# 样本权重乘以样本误差
weightedError = D.T * errArr
# 下面的 .2f 表示浮点数小数殿后两位, .3f 表示小数点后3位
print "split:dim %d,thresh %.2f, thresh ineqal: %s,the weighted eror is %.3f" %\
(i,threshVal,inequal,weightedError)
if weightedError < minError:
minError = weightedError
# 最好的分类结果
bestClassEst = predictedVals.copy()
bestStump['dim'] = i
bestStump['thresh'] = threshVal
bestStump['ineq'] = inequal
return bestStump,minError,bestClassEst
def adaBoostTrainDS(dataArr,classLabels,numIt = 40):
weakClassArr = []
m = shape(dataArr)[0]
D = mat(ones((m,1))/m)
# 创建矩阵 mat
aggClassEst = mat(zeros((m,1)))
for i in range(numIt):
bestStump,error,classEst = buildStump(dataArr,classLabels,D)
print 'D:',D.T
# log 就是 ln 公式: a = 0.5*ln((1-e)/e)
alpha = float(0.5*log((1.0-error)/max(error,1e-16)))
bestStump['alpha'] = alpha
# 将当前的最好的树桩添加到弱分类其数组中
weakClassArr.append(bestStump)
print 'classEst:',classEst.T
# D权重的更新公式,利用原本的类别classLabels与划分的类别classEst做乘积
# 用来同时计算正确划分和错误划分的公式,也就是自动确定正负号
expon = multiply(-1*alpha*mat(classLabels).T,classEst)
# 更新权重D
D = multiply(D,exp(expon))
D = D/D.sum()
aggClassEst += alpha*classEst
print 'aggClassEst:',aggClassEst.T
aggErrors = multiply(sign(aggClassEst)!=mat(classLabels).T,ones((m,1)))
errorRate = aggErrors.sum()/m
print 'total error:',errorRate,"\n"
if errorRate == 0.0:break;
return weakClassArr
# 利用学习得到的多个级联弱分类器进行数据分类
def adaClassify(datToClass,classifierArr):
dataMatrix = mat(datToClass)
m = shape(dataMatrix)[0]
aggClassEst = mat(zeros((m,1)))
for i in range(len(classifierArr)):
classEst = stumpClassify(dataMatrix,classifierArr[i]['dim'],\
classifierArr[i]['thresh'],classifierArr[i]['ineq'])
aggClassEst += classifierArr[i]['alpha'] * classEst
print aggClassEst
return sign(aggClassEst)
================================================
FILE: Adaboost/testAdaboost.py
================================================
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 12:57:27 2015
@author: LiuLongpo
"""
import optunity
import adaboost
import matplotlib.pyplot as plt
from numpy import *
dataMat,classLabels = adaboost.loadSimpData()
#plt.scatter(dataMat[:,0],dataMat[:,1])
# D是样本的权重矩阵
D = mat(ones((5,1))/5)
#adaboost.buildStump(dataMat,classLabels,D)
print 'data train...'
classifierArr = adaboost.adaBoostTrainDS(dataMat,classLabels,30)
print 'getClassifier:',classifierArr
print 'data predict...'
# 学习得到3个分类器,predict时,每一个分类器级联分类得到的预测累加值
# aggClassEst越来越远离0,也就是正越大或负越大,也就是分类结果越来越强
adaboost.adaClassify([[1,0.8],[1.8,2]],classifierArr)
# 0,lt,1.3 1,lt,1.0 0,lt,0.9
plt.figure()
I = nonzero(classLabels>0)[0]
plt.scatter(dataMat[I,0],dataMat[I,1],s=60,c=u'r',marker=u'o')
I = nonzero(classLabels<0)[0]
plt.scatter(dataMat[I,0],dataMat[I,1],s=60,c=u'b',marker=u'o')
plt.plot([1.32,1.32],[0.5,2.5])
plt.plot([0.5,2.5],[1.42,1.42])
plt.plot([0.97,0.97],[0.5,2.5])
'''
plt.figure()
I = nonzero(classLabels>0)[0]
plt.scatter(dataMat[I,0],dataMat[I,1],s=60,c=u'r',marker=u'o')
I = nonzero(classLabels<0)[0]
plt.scatter(dataMat[I,0],dataMat[I,1],s=60,c=u'b',marker=u'o')
plt.plot([1.32*1.19,1.32*1.19],[0.5,2.5])
plt.plot([0.5,2.5],[1.42*1.52,1.42*1.52])
plt.plot([0.97*1.13,0.97*1.13],[0.5,2.5])
#plt.scatter([0,5],[0,5])
'''
================================================
FILE: Decision-Tree/README.md
================================================
决策树算法实现。
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: Decision-Tree/TestTree.py
================================================
import Tree
dataSet,label = Tree.createDataSet()
print 'dataSet:' , dataSet
#Tree.createTree(dataSet,label)
================================================
FILE: Decision-Tree/Tree.py
================================================
from math import log
# 计算熵
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
for featVec in dataSet:
currentLabel = featVec[-1]
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
shannoEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
shannoEnt -= prob * log(prob,2)
return shannoEnt
def createDataSet():
dataSet = [[1,1,0,'yes'],[1,1,1,'yes'],[1,0,1,'no'],[0,1,0,'no'],[0,1,0,'no']]
label = ['no surfacing','flippers']
return dataSet,label
# 划分数据集
# axis为特征,也就是对某一个特征进行判定,比如身高
# value为特征的值,也就是说,某一个特征的不同值,
# 比如身高这个特征有高和矮之分,高跟矮就是这个value
# 数据集为 [ [1,1,0,'yes'],[1,1,1,'yes'],[1,0,1,'no'],[0,1,0,'no'],[0,1,0,'no'] ]
# 每一个 feaVec 就是一行,也就是一个样本数据
def spiltDataSet(dataSet,axis,value):
retDataSet = []
for featVec in dataSet:
# 如果当前样本数据的特征 axis 的值 featVec[axis] 与我们要求的value相等
# 也就是我们认为当前的样本符合我们的要求
if featVec[axis] == value:
# 对于符合要求的样本数据
# 我们将这个特征剪掉,剩下的数据组成一个新的子样本并返回
redecedFeatVec = featVec[:axis]
redecedFeatVec.extend(featVec[axis+1:])
retDataSet.append(redecedFeatVec)
return retDataSet
# 寻找最优的划分特征
def chooseBestFeatureToSplit(dataSet):
# 为什么减一?
numFeatures = len(dataSet[0])-1
# 计算源数据的熵
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0
bestFeature = -1
for i in xrange(numFeatures):
# 获取每一列,因为每一列都属于一个特征的不同value
featList = [example[i] for example in dataSet]
# 确保每个值都是唯一的
uniqueVals = set(featList)
newEntorpy = 0.0
print 'spilt feature ', i
# 对每个value进行划分
for value in uniqueVals:
subDataSet = spiltDataSet(dataSet,i,value)
print 'subDataSet:' , subDataSet
prob = len(subDataSet)/float(len(dataSet))
# 计算该特征进行划分后的信息
newEntorpy += prob * calcShannonEnt(subDataSet)
# 计算该特征进行划分后的信息增益
infoGain = baseEntropy - newEntorpy
# 寻找信息增益最大的特征就是最好划分的特征
if (infoGain > bestInfoGain):
bestInfoGain = infoGain
bestFeature = i
print 'BestFeatureToSplit'
print bestFeature
return bestFeature
# 投票表决,获取概率最大的那个分类
def majorityCnt(classList):
classCount = {}
for vote in classList:
if vote not in classCount.keys():classCount[vote] = 0
classCount[vote] += 1
# 反向排序,也就是从大到小
sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0]
def createTree(dataSet,labels):
# 获取每个样本数据的分类
classList = [example[-1] for example in dataSet]
# 第一个迭代终止条件:选取第一个classList中的类,判断它出现的次数是否与
# classList的长度相等,如果相等,证明classList中的所有类已经被分为同一种类
# 则返回该类
if classList.count(classList[0]) == len(classList):
return classList[0]
#如果所有特征都迭代完了,此时还没返回,
# 说明当前仍然不能讲数据集划分为仅包含唯一类别的分组
# 此时则返回次数出现最多的那个类别最为当前数据集的类别
if len(dataSet[0]) == 1:
return majorityCnt(classList)
# 寻找最优的划分特征
bestFeat = chooseBestFeatureToSplit(dataSet)
bestFeatLabel = labels[bestFeat]
myTree = {bestFeatLabel:{}}
del(labels[bestFeat])
# 获取最优特征的各个属性
featValues = [example[bestFeat] for example in dataSet]
uniqueVals = set(featValues)
for value in uniqueVals:
subLabels = labels[:]
# 迭代创建决策树
myTree[bestFeatLabel][value] = createTree(spiltDataSet(dataSet,bestFeat,value),subLabels)
return myTree
================================================
FILE: DeepLearning/CNN_cifar-10/cifar.py
================================================
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 27 11:27:34 2015
@author: lab-liu.longpo
"""
from __future__ import absolute_import
from __future__ import print_function
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
d = sio.loadmat('data.mat')
data = d['d']
label = d['l']
data = np.reshape(data,(50000,3,32,32))
label = np_utils.to_categorical(label, 10)
print ('finish loading data')
model = Sequential()
model.add(Convolution2D(32, 3, 5, 5, border_mode='valid'))
model.add(Activation('relu'))
#model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(32, 32, 5, 5, border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 32, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64*5*5, 512, init='normal'))
model.add(Activation('tanh'))
model.add(Dense(512, 10, init='normal'))
model.add(Activation('softmax'))
sgd = SGD(l2=0.001,lr=0.0065, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd,class_mode="categorical")
#checkpointer = ModelCheckpoint(filepath="weight.hdf5",verbose=1,save_best_only=True)
#model.fit(data, label, batch_size=100,nb_epoch=10,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2,callbacks=[checkpointer])
result = model.fit(data, label, batch_size=50,nb_epoch=35,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2)
#model.save_weights(weights,accuracy=False)
# plot the result
plt.figure
plt.plot(result.epoch,result.history['acc'],label="acc")
plt.plot(result.epoch,result.history['val_acc'],label="val_acc")
plt.scatter(result.epoch,result.history['acc'],marker='*')
plt.scatter(result.epoch,result.history['val_acc'])
plt.legend(loc='under right')
plt.show()
plt.figure
plt.plot(result.epoch,result.history['loss'],label="loss")
plt.plot(result.epoch,result.history['val_loss'],label="val_loss")
plt.scatter(result.epoch,result.history['loss'],marker='*')
plt.scatter(result.epoch,result.history['val_loss'],marker='*')
plt.legend(loc='upper right')
plt.show()
================================================
FILE: DeepLearning/CNN_mnist/cnn.py
================================================
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.advanced_activations import PReLU
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from data import load_data
import matplotlib.pyplot as plt
'''
train the dataset of face 42000 image
learning rate : 0.0015
pathc size:100
dropout :0.25
l2 = 0.01
'''
def funcnn(LR,BS):
data, label = load_data()
label = np_utils.to_categorical(label, 10)
model = Sequential()
model.add(Convolution2D(4, 1, 5, 5, border_mode='valid'))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Convolution2D(8,4, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(16, 8, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(16*4*4, 256, init='normal'))
model.add(Activation('tanh'))
model.add(Dense(256, 10, init='normal'))
model.add(Activation('softmax'))
sgd = SGD(l2=0.001,lr=LR, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd,class_mode="categorical")
#checkpointer = ModelCheckpoint(filepath="weight.hdf5",verbose=1,save_best_only=True)
#model.fit(data, label, batch_size=100,nb_epoch=10,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2,callbacks=[checkpointer])
result = model.fit(data, label, batch_size=BS,nb_epoch=20,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2)
#model.save_weights(weights,accuracy=False)
# plot the result
plt.figure
plt.plot(result.epoch,result.history['acc'],label="acc")
plt.plot(result.epoch,result.history['val_acc'],label="val_acc")
plt.scatter(result.epoch,result.history['acc'],marker='*')
plt.scatter(result.epoch,result.history['val_acc'])
plt.legend(loc='under right')
plt.show()
plt.figure
plt.plot(result.epoch,result.history['loss'],label="loss")
plt.plot(result.epoch,result.history['val_loss'],label="val_loss")
plt.scatter(result.epoch,result.history['loss'],marker='*')
plt.scatter(result.epoch,result.history['val_loss'],marker='*')
plt.legend(loc='upper right')
plt.show()
================================================
FILE: DeepLearning/CNN_mnist/data.py
================================================
# -*- coding: utf-8 -*-
import os
from PIL import Image
import numpy as np
def load_data():
print 'start loading data...'
data = np.empty((42000,1,28,28),dtype="float32")
label = np.empty((42000,),dtype="uint8")
imgs = os.listdir("./mnist")
num = len(imgs)
for i in range(num):
img = Image.open("./mnist/"+imgs[i])
arr = np.asarray(img,dtype="float32")
data[i,:,:,:] = arr
label[i] = int(imgs[i].split('.')[0])
return data,label
================================================
FILE: DeepLearning/CNN_mnist/trainCNN.py
================================================
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 25 10:59:38 2015
@author: lab-liu.longpo
"""
from cnn import funcnn
import numpy as np
def floatrange(start,stop,steps):
return [start+float(i) * (stop-start)/(float(steps)-1) for i in range(steps)]
#LR = [0.03]
#LR = floatrange(0.033,0.036,10)
LR = [0.0015]
BS = [100]
#result = np.empty((3,8,10),dtype="float32")
k = 0;
for i in range(1):
for j in range(1):
print 'test',k
k = k+1
tmp = funcnn(LR[i],BS[j])
#result[i,j,:] = tmp.history['val_acc']
print 'learning rate:',LR[i],'batch size:',BS[j]
================================================
FILE: DeepLearning/UFLDL/Vectorization_sparseae_exercise/checkNumericalGradient.m
================================================
function [] = checkNumericalGradient()
% This code can be used to check your numerical gradient implementation
% in computeNumericalGradient.m
% It analytically evaluates the gradient of a very simple function called
% simpleQuadraticFunction (see below) and compares the result with your numerical
% solution. Your numerical gradient implementation is incorrect if
% your numerical solution deviates too much from the analytical solution.
% Evaluate the function and gradient at x = [4; 10]; (Here, x is a 2d vector.)
x = [4; 10];
[value, grad] = simpleQuadraticFunction(x);
% Use your code to numerically compute the gradient of simpleQuadraticFunction at x.
% (The notation "@simpleQuadraticFunction" denotes a pointer to a function.)
numgrad = computeNumericalGradient(@simpleQuadraticFunction, x);
% Visually examine the two gradient computations. The two columns
% you get should be very similar.
disp([numgrad grad]);
fprintf('The above two columns you get should be very similar.\n(Left-Your Numerical Gradient, Right-Analytical Gradient)\n\n');
% Evaluate the norm of the difference between two solutions.
% If you have a correct implementation, and assuming you used EPSILON = 0.0001
% in computeNumericalGradient.m, then diff below should be 2.1452e-12
diff = norm(numgrad-grad)/norm(numgrad+grad);
disp(diff);
fprintf('Norm of the difference between numerical and analytical gradient (should be < 1e-9)\n\n');
end
function [value,grad] = simpleQuadraticFunction(x)
% this function accepts a 2D vector as input.
% Its outputs are:
% value: h(x1, x2) = x1^2 + 3*x1*x2
% grad: A 2x1 vector that gives the partial derivatives of h with respect to x1 and x2
% Note that when we pass @simpleQuadraticFunction(x) to computeNumericalGradients, we're assuming
% that computeNumericalGradients will use only the first returned value of this function.
value = x(1)^2 + 3*x(1)*x(2);
grad = zeros(2, 1);
% 对X1 求偏导
grad(1) = 2*x(1) + 3*x(2);
% 对X2 求偏导
grad(2) = 3*x(1);
end
================================================
FILE: DeepLearning/UFLDL/Vectorization_sparseae_exercise/computeNumericalGradient.m
================================================
function numgrad = computeNumericalGradient(J, theta)
% numgrad = computeNumericalGradient(J, theta)
% theta: a vector of parameters
% J: a function that outputs a real-number. Calling y = J(theta) will return the
% function value at theta.
% Initialize numgrad with zeros 2x1 这是输入X的行数,其实也就是参数W的个数
numgrad = zeros(size(theta));
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions:
% Implement numerical gradient checking, and return the result in numgrad.
% (See Section 2.3 of the lecture notes.)
% You should write code so that numgrad(i) is (the numerical approximation to) the
% partial derivative of J with respect to the i-th input argument, evaluated at theta.
% I.e., numgrad(i) should be the (approximately) the partial derivative of J with
% respect to theta(i).
%
% Hint: You will probably want to compute the elements of numgrad one at a time.
epsilon = 0.000001;
% 2 x2 得到参数的个数
numW = size(theta,1);
I = eye(numW);
I = I * epsilon;
for j = 1:numW
numgrad(j) = (J(theta+I(:,j)) - J(theta-I(:,j))) / (2 * epsilon);
end
%% ---------------------------------------------------------------
end
================================================
FILE: DeepLearning/UFLDL/Vectorization_sparseae_exercise/display_network.m
================================================
function [h, array] = display_network(A, opt_normalize, opt_graycolor, cols, opt_colmajor)
% This function visualizes filters in matrix A. Each column of A is a
% filter. We will reshape each column into a square image and visualizes
% on each cell of the visualization panel.
% All other parameters are optional, usually you do not need to worry
% about it.
% opt_normalize: whether we need to normalize the filter so that all of
% them can have similar contrast. Default value is true.
% opt_graycolor: whether we use gray as the heat map. Default is true.
% cols: how many columns are there in the display. Default value is the
% squareroot of the number of columns in A.
% opt_colmajor: you can switch convention to row major for A. In that
% case, each row of A is a filter. Default value is false.
warning off all
if ~exist('opt_normalize', 'var') || isempty(opt_normalize)
opt_normalize= true;
end
if ~exist('opt_graycolor', 'var') || isempty(opt_graycolor)
opt_graycolor= true;
end
if ~exist('opt_colmajor', 'var') || isempty(opt_colmajor)
opt_colmajor = false;
end
% rescale
A = A - mean(A(:));
if opt_graycolor, colormap(gray); end
% compute rows, cols
[L M]=size(A);
sz=sqrt(L);
buf=1;
if ~exist('cols', 'var')
if floor(sqrt(M))^2 ~= M
n=ceil(sqrt(M));
while mod(M, n)~=0 && n<1.2*sqrt(M), n=n+1; end
m=ceil(M/n);
else
n=sqrt(M);
m=n;
end
else
n = cols;
m = ceil(M/n);
end
array=-ones(buf+m*(sz+buf),buf+n*(sz+buf));
if ~opt_graycolor
array = 0.1.* array;
end
if ~opt_colmajor
k=1;
for i=1:m
for j=1:n
if k>M,
continue;
end
clim=max(abs(A(:,k)));
if opt_normalize
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/clim;
else
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/max(abs(A(:)));
end
k=k+1;
end
end
else
k=1;
for j=1:n
for i=1:m
if k>M,
continue;
end
clim=max(abs(A(:,k)));
if opt_normalize
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/clim;
else
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz);
end
k=k+1;
end
end
end
if opt_graycolor
h=imagesc(array,'EraseMode','none',[-1 1]);
else
h=imagesc(array,'EraseMode','none',[-1 1]);
end
axis image off
drawnow;
warning on all
================================================
FILE: DeepLearning/UFLDL/Vectorization_sparseae_exercise/initializeParameters.m
================================================
function theta = initializeParameters(hiddenSize, visibleSize)
%% Initialize parameters randomly based on layer sizes.
r = sqrt(6) / sqrt(hiddenSize+visibleSize+1); % we'll choose weights uniformly from the interval [-r, r]
W1 = rand(hiddenSize, visibleSize) * 2 * r - r;
W2 = rand(visibleSize, hiddenSize) * 2 * r - r;
b1 = zeros(hiddenSize, 1);
b2 = zeros(visibleSize, 1);
% Convert weights and bias gradients to the vector form.
% This step will "unroll" (flatten and concatenate together) all
% your parameters into a vector, which can then be used with minFunc.
theta = [W1(:) ; W2(:) ; b1(:) ; b2(:)];
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/display_network.m
================================================
function [h, array] = display_network(A, opt_normalize, opt_graycolor, cols, opt_colmajor)
% This function visualizes filters in matrix A. Each column of A is a
% filter. We will reshape each column into a square image and visualizes
% on each cell of the visualization panel.
% All other parameters are optional, usually you do not need to worry
% about it.
% opt_normalize: whether we need to normalize the filter so that all of
% them can have similar contrast. Default value is true.
% opt_graycolor: whether we use gray as the heat map. Default is true.
% cols: how many columns are there in the display. Default value is the
% squareroot of the number of columns in A.
% opt_colmajor: you can switch convention to row major for A. In that
% case, each row of A is a filter. Default value is false.
warning off all
if ~exist('opt_normalize', 'var') || isempty(opt_normalize)
opt_normalize= true;
end
if ~exist('opt_graycolor', 'var') || isempty(opt_graycolor)
opt_graycolor= true;
end
if ~exist('opt_colmajor', 'var') || isempty(opt_colmajor)
opt_colmajor = false;
end
% rescale
A = A - mean(A(:));
if opt_graycolor, colormap(gray); end
% compute rows, cols
[L M]=size(A);
sz=sqrt(L);
buf=1;
if ~exist('cols', 'var')
if floor(sqrt(M))^2 ~= M
n=ceil(sqrt(M));
while mod(M, n)~=0 && n<1.2*sqrt(M), n=n+1; end
m=ceil(M/n);
else
n=sqrt(M);
m=n;
end
else
n = cols;
m = ceil(M/n);
end
array=-ones(buf+m*(sz+buf),buf+n*(sz+buf));
if ~opt_graycolor
array = 0.1.* array;
end
if ~opt_colmajor
k=1;
for i=1:m
for j=1:n
if k>M,
continue;
end
clim=max(abs(A(:,k)));
if opt_normalize
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/clim;
else
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/max(abs(A(:)));
end
k=k+1;
end
end
else
k=1;
for j=1:n
for i=1:m
if k>M,
continue;
end
clim=max(abs(A(:,k)));
if opt_normalize
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz)/clim;
else
array(buf+(i-1)*(sz+buf)+(1:sz),buf+(j-1)*(sz+buf)+(1:sz))=reshape(A(:,k),sz,sz);
end
k=k+1;
end
end
end
if opt_graycolor
h=imagesc(array,'EraseMode','none',[-1 1]);
else
h=imagesc(array,'EraseMode','none',[-1 1]);
end
axis image off
drawnow;
warning on all
================================================
FILE: DeepLearning/UFLDL/stl_exercise/feedForwardAutoencoder.m
================================================
function [activation] = feedForwardAutoencoder(theta, hiddenSize, visibleSize, data)
% theta: trained weights from the autoencoder
% visibleSize: the number of input units (probably 64)
% hiddenSize: the number of hidden units (probably 25)
% data: Our matrix containing the training data as columns. So, data(:,i) is the i-th training example.
% We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this
% follows the notation convention of the lecture notes.
%200 x 784
W1 = reshape(theta(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
% 200 x 1
b1 = theta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute the activation of the hidden layer for the Sparse Autoencoder.
activation = sigmoid(bsxfun(@plus,W1 * data,b1)) ;
%-------------------------------------------------------------------
end
%-------------------------------------------------------------------
% Here's an implementation of the sigmoid function, which you may find useful
% in your computation of the costs and the gradients. This inputs a (row or
% column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)).
function sigm = sigmoid(x)
sigm = 1 ./ (1 + exp(-x));
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/initializeParameters.m
================================================
function theta = initializeParameters(hiddenSize, visibleSize)
%% Initialize parameters randomly based on layer sizes.
r = sqrt(6) / sqrt(hiddenSize+visibleSize+1); % we'll choose weights uniformly from the interval [-r, r]
W1 = rand(hiddenSize, visibleSize) * 2 * r - r;
W2 = rand(visibleSize, hiddenSize) * 2 * r - r;
b1 = zeros(hiddenSize, 1);
b2 = zeros(visibleSize, 1);
% Convert weights and bias gradients to the vector form.
% This step will "unroll" (flatten and concatenate together) all
% your parameters into a vector, which can then be used with minFunc.
theta = [W1(:) ; W2(:) ; b1(:) ; b2(:)];
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/loadMNISTImages.m
================================================
function images = loadMNISTImages(filename)
%loadMNISTImages returns a 28x28x[number of MNIST images] matrix containing
%the raw MNIST images
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);
magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2051, ['Bad magic number in ', filename, '']);
numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
numCols = fread(fp, 1, 'int32', 0, 'ieee-be');
images = fread(fp, inf, 'unsigned char');
images = reshape(images, numCols, numRows, numImages);
images = permute(images,[2 1 3]);
fclose(fp);
% Reshape to #pixels x #examples
images = reshape(images, size(images, 1) * size(images, 2), size(images, 3));
% Convert to double and rescale to [0,1]
images = double(images) / 255;
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/loadMNISTLabels.m
================================================
function labels = loadMNISTLabels(filename)
%loadMNISTLabels returns a [number of MNIST images]x1 matrix containing
%the labels for the MNIST images
fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);
magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2049, ['Bad magic number in ', filename, '']);
numLabels = fread(fp, 1, 'int32', 0, 'ieee-be');
labels = fread(fp, inf, 'unsigned char');
assert(size(labels,1) == numLabels, 'Mismatch in label count');
fclose(fp);
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/softmaxCost.m
================================================
function [cost, grad] = softmaxCost(theta, numClasses, inputSize, lambda, data, labels)
% numClasses - the number of classes
% inputSize - the size N of the input vector
% lambda - weight decay parameter
% data - the N x M input matrix, where each column data(:, i) corresponds to
% a single test set
% labels - an M x 1 matrix containing the labels corresponding for the input data
%
% Unroll the parameters from theta
% 10x8
theta = reshape(theta, numClasses, inputSize);
%100 data 8x100
numCases = size(data, 2);
%sparse(r,c,v) = [r(i),c(i)] = v(i)
% 每一列的第 label 行 设置为i,也就是,每个样本属于哪个label,该位置就是1,其他的均为0
% 10 x 100
groundTruth = full(sparse(labels, 1:numCases, 1));
cost = 0;
%10 x 8
thetagrad = zeros(numClasses, inputSize);
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute the cost and gradient for softmax regression.
% You need to compute thetagrad and cost.
% The groundTruth matrix might come in handy.
% 10x100 10x8 8x100 100个样本,每个样本属于每个label的概率
M = theta * data;
M = exp(bsxfun(@minus,M,max(M,[],1)));
H = bsxfun(@rdivide,M,sum(M));
cost = - sum(sum((groundTruth .* log(H)))) / size(data,2) + lambda * sum(sum(theta.^2)) / 2 ;
% 10x8 10x100 100x8
thetagrad = - ((groundTruth - H)) * data' /size(data,2) +lambda * theta;
% ------------------------------------------------------------------
% Unroll the gradient matrices into a vector for minFunc
grad = [thetagrad(:)];
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/softmaxPredict.m
================================================
function [pred] = softmaxPredict(softmaxModel, data)
% softmaxModel - model trained using softmaxTrain
% data - the N x M input matrix, where each column data(:, i) corresponds to
% a single test set
%
% Your code should produce the prediction matrix
% pred, where pred(i) is argmax_c P(y(c) | x(i)).
% Unroll the parameters from theta
theta = softmaxModel.optTheta; % this provides a numClasses x inputSize matrix
pred = zeros(1, size(data, 2));
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute pred using theta assuming that the labels start
% from 1.
[Val , pred] = max(theta*data);
% ---------------------------------------------------------------------
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/softmaxTrain.m
================================================
function [softmaxModel] = softmaxTrain(inputSize, numClasses, lambda, inputData, labels, options)
%softmaxTrain Train a softmax model with the given parameters on the given
% data. Returns softmaxOptTheta, a vector containing the trained parameters
% for the model.
%
% inputSize: the size of an input vector x^(i)
% numClasses: the number of classes
% lambda: weight decay parameter
% inputData: an N by M matrix containing the input data, such that
% inputData(:, c) is the cth input
% labels: M by 1 matrix containing the class labels for the
% corresponding inputs. labels(c) is the class label for
% the cth input
% options (optional): options
% options.maxIter: number of iterations to train for
if ~exist('options', 'var')
options = struct;
end
if ~isfield(options, 'maxIter')
options.maxIter = 400;
end
% initialize parameters
theta = 0.005 * randn(numClasses * inputSize, 1);
% Use minFunc to minimize the function
addpath minFunc/
options.Method = 'lbfgs'; % Here, we use L-BFGS to optimize our cost
% function. Generally, for minFunc to work, you
% need a function pointer with two outputs: the
% function value and the gradient. In our problem,
% softmaxCost.m satisfies this.
minFuncOptions.display = 'on';
[softmaxOptTheta, cost] = minFunc( @(p) softmaxCost(p, ...
numClasses, inputSize, lambda, ...
inputData, labels), ...
theta, options);
% Fold softmaxOptTheta into a nicer format
softmaxModel.optTheta = reshape(softmaxOptTheta, numClasses, inputSize);
softmaxModel.inputSize = inputSize;
softmaxModel.numClasses = numClasses;
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/sparseAutoencoderCost.m
================================================
function [cost,grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, ...
lambda, sparsityParam, beta, data)
%lambda = 0;
%beta = 0;
% visibleSize: the number of input units (probably 64)
% hiddenSize: the number of hidden units (probably 25)
% lambda: weight decay parameter
% sparsityParam: The desired average activation for the hidden units (denoted in the lecture
% notes by the greek alphabet rho, which looks like a lower-case "p").
% beta: weight of sparsity penalty term
% data: Our 64x10000 matrix containing the training data. So, data(:,i) is the i-th training example.
% The input theta is a vector (because minFunc expects the parameters to be a vector).
% We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this
% follows the notation convention of the lecture notes.
% 学习率 自己定义的
alpha = 0.03;
% 计算隐藏层神经元的激活度
p = zeros(hiddenSize,1);
W1 = reshape(theta(1:hiddenSize*visibleSize), hiddenSize, visibleSize);
W2 = reshape(theta(hiddenSize*visibleSize+1:2*hiddenSize*visibleSize), visibleSize, hiddenSize);
b1 = theta(2*hiddenSize*visibleSize+1:2*hiddenSize*visibleSize+hiddenSize);
b2 = theta(2*hiddenSize*visibleSize+hiddenSize+1:end);
% Cost and gradient variables (your code needs to compute these values).
% Here, we initialize them to zeros.
%% ---------- YOUR CODE HERE --------------------------------------
% Instructions: Compute the cost/optimization objective J_sparse(W,b) for the Sparse Autoencoder,
% and the corresponding gradients W1grad, W2grad, b1grad, b2grad.
%
% W1grad, W2grad, b1grad and b2grad should be computed using backpropagation.
% Note that W1grad has the same dimensions as W1, b1grad has the same dimensions
% as b1, etc. Your code should set W1grad to be the partial derivative of J_sparse(W,b) with
% respect to W1. I.e., W1grad(i,j) should be the partial derivative of J_sparse(W,b)
% with respect to the input parameter W1(i,j). Thus, W1grad should be equal to the term
% [(1/m) \Delta W^{(1)} + \lambda W^{(1)}] in the last block of pseudo-code in Section 2.2
% of the lecture notes (and similarly for W2grad, b1grad, b2grad).
%
% Stated differently, if we were using batch gradient descent to optimize the parameters,
% the gradient descent update to W1 would be W1 := W1 - alpha * W1grad, and similarly for W2, b1, b2.
%
numPatches = size(data,2);
KLdist = 0;
%% 向前传输
a2 = sigmoid(W1*data+repmat(b1,[1,numPatches]));
p = sum(a2,2);
a3 = sigmoid(W2 * a2 + repmat(b2,[1,numPatches]));
J_sparse = 0.5 * sum(sum((a3-data).^2));
%% 计算 隐藏层的平均激活度
p = p / numPatches ;
%% 向后传输
residual3 = -(data-a3).*a3.*(1-a3);
tmp = beta * ( - sparsityParam ./ p + (1-sparsityParam) ./ (1-p));
residual2 = (W2' * residual3 + repmat(tmp,[1,numPatches])) .* a2.*(1-a2);
W2grad = residual3 * a2' / numPatches + lambda * W2 ;
W1grad = residual2 * data' / numPatches + lambda * W1 ;
b2grad = sum(residual3,2) / numPatches;
b1grad = sum(residual2,2) / numPatches;
%% 更新权重参数 加上 lambda 权重衰减
W2 = W2 - alpha * ( W2grad );
W1 = W1 - alpha * ( W1grad );
b2 = b2 - alpha * (b2grad );
b1 = b1 - alpha * (b1grad );
%% 计算KL相对熵
for j = 1:hiddenSize
KLdist = KLdist + sparsityParam *log( sparsityParam / p(j) ) + (1 - sparsityParam) * log((1-sparsityParam) / (1 - p(j)));
end
%% costFunction 加上 lambda 权重衰减
cost = J_sparse / numPatches + (sum(sum(W1.^2)) + sum(sum(W2.^2))) * lambda / 2 + beta * KLdist;
%-------------------------------------------------------------------
% After computing the cost and gradient, we will convert the gradients back
% to a vector format (suitable for minFunc). Specifically, we will unroll
% your gradient matrices into a vector.
grad = [W1grad(:) ; W2grad(:) ; b1grad(:) ; b2grad(:)];
end
%-------------------------------------------------------------------
% Here's an implementation of the sigmoid function, which you may find useful
% in your computation of the costs and the gradients. This inputs a (row or
% column) vector (say (z1, z2, z3)) and returns (f(z1), f(z2), f(z3)).
function sigm = sigmoid(x)
sigm = 1 ./ (1 + exp(-x));
end
================================================
FILE: DeepLearning/UFLDL/stl_exercise/stlExercise.m
================================================
%% CS294A/CS294W Self-taught Learning Exercise
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% self-taught learning. You will need to complete code in feedForwardAutoencoder.m
% You will also need to have implemented sparseAutoencoderCost.m and
% softmaxCost.m from previous exercises.
%
%% ======================================================================
% STEP 0: Here we provide the relevant parameters values that will
% allow your sparse autoencoder to get good filters; you do not need to
% change the parameters below.
clc
clear
inputSize = 28 * 28;
numLabels = 5;
hiddenSize = 200;
sparsityParam = 0.1; % desired average activation of the hidden units.
% (This was denoted by the Greek alphabet rho, which looks like a lower-case "p",
% in the lecture notes).
lambda = 3e-3; % weight decay parameter
beta = 3; % weight of sparsity penalty term
maxIter = 400;
%% ======================================================================
% STEP 1: Load data from the MNIST database
%
% This loads our training and test data from the MNIST database files.
% We have sorted the data for you in this so that you will not have to
% change it.
% Load MNIST database files
mnistData = loadMNISTImages('train-images.idx3-ubyte');
mnistLabels = loadMNISTLabels('train-labels.idx1-ubyte');
% Set Unlabeled Set (All Images)
% Simulate a Labeled and Unlabeled set
labeledSet = find(mnistLabels >= 0 & mnistLabels <= 4);
unlabeledSet = find(mnistLabels >= 5);
numTrain = round(numel(labeledSet)/2);
trainSet = labeledSet(1:numTrain);
testSet = labeledSet(numTrain+1:end);
unlabeledData = mnistData(:, unlabeledSet);
trainData = mnistData(:, trainSet);
trainLabels = mnistLabels(trainSet)' + 1; % Shift Labels to the Range 1-5
testData = mnistData(:, testSet);
testLabels = mnistLabels(testSet)' + 1; % Shift Labels to the Range 1-5
% Output Some Statistics
fprintf('# examples in unlabeled set: %d\n', size(unlabeledData, 2));
fprintf('# examples in supervised training set: %d\n\n', size(trainData, 2));
fprintf('# examples in supervised testing set: %d\n\n', size(testData, 2));
%% ======================================================================
% STEP 2: Train the sparse autoencoder
% This trains the sparse autoencoder on the unlabeled training
% images.
% Randomly initialize the parameters
theta = initializeParameters(hiddenSize, inputSize);
%% ----------------- YOUR CODE HERE ----------------------
% Find opttheta by running the sparse autoencoder on
% unlabeledTrainingImages
addpath minFunc/
options.Method = 'lbfgs';
options.maxIter = 400;
options.display = 'on';
visibleSize = inputSize;
[opttheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...
visibleSize, hiddenSize, ...
lambda, sparsityParam, ...
beta, unlabeledData), ...
theta, options);
%% -----------------------------------------------------
% Visualize weights
W1 = reshape(opttheta(1:hiddenSize * inputSize), hiddenSize, inputSize);
display_network(W1');
%%======================================================================
%% STEP 3: Extract Features from the Supervised Dataset
%
% You need to complete the code in feedForwardAutoencoder.m so that the
% following command will extract features from the data.
trainFeatures = feedForwardAutoencoder(opttheta, hiddenSize, inputSize, ...
trainData);
testFeatures = feedForwardAutoencoder(opttheta, hiddenSize, inputSize, ...
testData);
%%======================================================================
%% STEP 4: Train the softmax classifier
softmaxModel = struct;
%% ----------------- YOUR CODE HERE ----------------------
% Use softmaxTrain.m from the previous exercise to train a multi-class
% classifier.
options.maxIter = 100;
lambda = 1e-4;
numClasses = 5;
inputData = trainFeatures;
labels = trainLabels ;
inputSize = hiddenSize;
softmaxModel = softmaxTrain(inputSize, numClasses, lambda, inputData, labels, options);
% Use lambda = 1e-4 for the weight regularization for softmax
% You need to compute softmaxModel using softmaxTrain on trainFeatures and
% trainLabels
%% -----------------------------------------------------
%%======================================================================
%% STEP 5: Testing
%% ----------------- YOUR CODE HERE ----------------------
% Compute Predictions on the test set (testFeatures) using softmaxPredict
% and softmaxModel
[pred] = softmaxPredict(softmaxModel, testFeatures);
%% -----------------------------------------------------
% Classification Score
fprintf('Test Accuracy: %f%%\n', 100*mean(pred(:) == testLabels(:)));
% (note that we shift the labels by 1, so that digit 0 now corresponds to
% label 1)
%
% Accuracy is the proportion of correctly classified images
% The results for our implementation was:
%
% Accuracy: 98.3%
%
%
================================================
FILE: GMM/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/47058109
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: GMM/gmm.m
================================================
function varargout = gmm(X, K_or_centroids)
% ============================================================
% Expectation-Maximization iteration implementation of
% Gaussian Mixture Model.
%
% PX = GMM(X, K_OR_CENTROIDS)
% [PX MODEL] = GMM(X, K_OR_CENTROIDS)
%
% - X: N-by-D data matrix.
% - K_OR_CENTROIDS: either K indicating the number of
% components or a K-by-D matrix indicating the
% choosing of the initial K centroids.
%
% - PX: N-by-K matrix indicating the probability of each
% component generating each point.
% - MODEL: a structure containing the parameters for a GMM:
% MODEL.Miu: a K-by-D matrix.
% MODEL.Sigma: a D-by-D-by-K matrix.
% MODEL.Pi: a 1-by-K vector.
% ============================================================
threshold = 1e-15;
[N, D] = size(X);
% isscalar 判断是否为标量
if isscalar(K_or_centroids)
K = K_or_centroids;
% randomly pick centroids
rndp = randperm(N);
centroids = X(rndp(1:K), :);
else % 矩阵,给出每一类的初始化
K = size(K_or_centroids, 1);
centroids = K_or_centroids;
end
% initial values
[pMiu pPi pSigma] = init_params();
Lprev = -inf;
while true
%% Estiamtion Step
Px = calc_prob();
% new value for pGamma
pGamma = Px .* repmat(pPi, N, 1);
pGamma = pGamma ./ repmat(sum(pGamma, 2), 1, K);
%% Maximization Step
% new value for parameters of each Component
Nk = sum(pGamma, 1);
pMiu = diag(1./Nk) * pGamma' * X;
pPi = Nk/N;
for kk = 1:K
Xshift = X-repmat(pMiu(kk, :), N, 1);
pSigma(:, :, kk) = (Xshift' * (diag(pGamma(:, kk)) * Xshift)) / Nk(kk);
end
%% check for convergence
L = sum(log(Px*pPi'));
if L-Lprev < threshold
break;
end
Lprev = L;
end
% 输出参数判定
if nargout == 1
varargout = {Px};
else
model = [];
model.Miu = pMiu;
model.Sigma = pSigma;
model.Pi = pPi;
varargout = {Px, model};
end
function [pMiu pPi pSigma] = init_params()
pMiu = centroids; % 均值,也就是K类的中心
pPi = zeros(1, K); % 概率
pSigma = zeros(D, D, K); %协方差矩阵,每个都是 D*D
% hard assign x to each centroids
% (X - pMiu)^2 = X^2 + pMiu^2 - 2*X*pMiu
distmat = repmat(sum(X.*X, 2), 1, K) + repmat(sum(pMiu.*pMiu, 2)', N, 1) - 2*X*pMiu';
[dummy labels] = min(distmat, [], 2);
for k=1:K %初始化参数
Xk = X(labels == k, :);
pPi(k) = size(Xk, 1)/N;
pSigma(:, :, k) = cov(Xk);
end
end
% 计算概率
function Px = calc_prob()
Px = zeros(N, K);
for k = 1:K
Xshift = X-repmat(pMiu(k, :), N, 1);
inv_pSigma = inv(pSigma(:, :, k)+diag(repmat(threshold,1,size(pSigma(:, :, k),1)))); % 方差矩阵求逆
tmp = sum((Xshift*inv_pSigma) .* Xshift, 2);
coef = (2*pi)^(-D/2) * sqrt(det(inv_pSigma)); % det 求方差矩阵的行列式
Px(:, k) = coef * exp(-0.5*tmp);
end
end
end
================================================
FILE: GMM/gmm.py
================================================
# -*- coding: utf-8 -*-
'''
Description :GMM in Python
Author : LiuLongpo
Time : 2015年7月26日16:54:48
Source :From pluskid
'''
import sys;
sys.path.append("E:\Python\MachineLearning\PythonTools")
import PythonUtils as pu
import matplotlib.pyplot as plt
import numpy as np
'矩阵的逆矩阵需要的库'
from numpy.linalg import *
def gmm(X,K):
threshold = 1e-15
N,D = np.shape(X)
randV = pu.randIntList(1,N,K)
centroids = X[randV]
pMiu,pPi,pSigma = inti_params(centroids,K,X,N,D);
Lprev = -np.inf
while True:
'Estiamtion Step'
Px = calc_prop(X,N,K,pMiu,pSigma,threshold,D)
pGamma = Px * np.tile(pPi,(N,1))
pGamma = pGamma / np.tile((np.sum(pGamma,axis=1)),(K,1)).T
'Maximization Step'
Nk = np.sum(pGamma,axis=0)
pMiu = np.dot(np.dot(np.diag(1 / Nk),pGamma.T),X)
pPi = Nk / N
for kk in range(K):
Xshift = X - np.tile(pMiu[kk],(N,1))
pSigma[:,:,kk] = (np.dot(np.dot(Xshift.T,np.diag(pGamma[:,kk])),Xshift)) / Nk[kk]
'check for convergence'
L = np.sum(np.log(np.dot(Px,pPi.T)))
if L-Lprev<threshold:
break
Lprev = L
return Px
def inti_params(centroids,K,X,N,D):
pMiu = centroids
pPi = np.zeros((1,K))
pSigma = np.zeros((D,D,K))
distmat = np.tile(np.sum(X * X,axis=1),(K,1)).T \
+ np.tile(np.sum(pMiu * pMiu,axis = 1).T,(N,1)) \
- 2 * np.dot(X,pMiu.T)
labels = np.argmin(distmat,axis=1)
for k in range(K):
Xk = X[labels==k]
pPi[0][k] = float(np.shape(Xk)[0]) / N # 样本数除以 N 得到概率
pSigma[:,:,k] = np.cov(Xk.T)
return pMiu,pPi,pSigma
'计算概率'
def calc_prop(X,N,K,pMiu,pSigma,threshold,D):
Px = np.zeros((N,K))
for k in range(K):
Xshift = X - np.tile(pMiu[k],(N,1))
inv_pSigma = inv(pSigma[:,:,k]) \
+ np.diag(np.tile(threshold,(1,np.ndim(pSigma[:,:,k]))))
tmp = np.sum(np.dot(Xshift,inv_pSigma) * Xshift,axis=1)
coef = (2*np.pi)**(-D/2) * np.sqrt(np.linalg.det(inv_pSigma))
Px[:,k] = coef * np.exp(-0.5 * tmp)
return Px
def test():
X = pu.readDataFromTxt('testSet.txt')
num = np.size(X)
X = np.reshape(X,(num/2,2))
ppx = gmm(X,4)
index = np.argmax(ppx,axis=1)
plt.figure()
plt.scatter(X[index==0][:,0],X[index==0][:,1],s=60,c=u'r',marker=u'o')
plt.scatter(X[index==1][:,0],X[index==1][:,1],s=60,c=u'b',marker=u'o')
plt.scatter(X[index==2][:,0],X[index==2][:,1],s=60,c=u'y',marker=u'o')
plt.scatter(X[index==3][:,0],X[index==3][:,1],s=60,c=u'g',marker=u'o')
if __name__ == '__main__':
test()
================================================
FILE: GMM/testGMM.m
================================================
clear all
clc
data = load('testSet.txt');
[PX,Model] = GMM(data,4);
[~,index] = max(PX');
cent = Model.Miu;
figure
I = find(index == 1);
scatter(data(I,1),data(I,2))
hold on
scatter(cent(1,1),cent(1,2),150,'filled')
hold on
I = find(index == 2);
scatter(data(I,1),data(I,2))
hold on
scatter(cent(2,1),cent(2,2),150,'filled')
hold on
I = find(index == 3);
scatter(data(I,1),data(I,2))
hold on
scatter(cent(3,1),cent(3,2),150,'filled')
hold on
I = find(index == 4);
scatter(data(I,1),data(I,2))
hold on
scatter(cent(4,1),cent(4,2),150,'filled')
================================================
FILE: GMM/testSet.txt
================================================
1.658985 4.285136
-3.453687 3.424321
4.838138 -1.151539
-5.379713 -3.362104
0.972564 2.924086
-3.567919 1.531611
0.450614 -3.302219
-3.487105 -1.724432
2.668759 1.594842
-3.156485 3.191137
3.165506 -3.999838
-2.786837 -3.099354
4.208187 2.984927
-2.123337 2.943366
0.704199 -0.479481
-0.392370 -3.963704
2.831667 1.574018
-0.790153 3.343144
2.943496 -3.357075
-3.195883 -2.283926
2.336445 2.875106
-1.786345 2.554248
2.190101 -1.906020
-3.403367 -2.778288
1.778124 3.880832
-1.688346 2.230267
2.592976 -2.054368
-4.007257 -3.207066
2.257734 3.387564
-2.679011 0.785119
0.939512 -4.023563
-3.674424 -2.261084
2.046259 2.735279
-3.189470 1.780269
4.372646 -0.822248
-2.579316 -3.497576
1.889034 5.190400
-0.798747 2.185588
2.836520 -2.658556
-3.837877 -3.253815
2.096701 3.886007
-2.709034 2.923887
3.367037 -3.184789
-2.121479 -4.232586
2.329546 3.179764
-3.284816 3.273099
3.091414 -3.815232
-3.762093 -2.432191
3.542056 2.778832
-1.736822 4.241041
2.127073 -2.983680
-4.323818 -3.938116
3.792121 5.135768
-4.786473 3.358547
2.624081 -3.260715
-4.009299 -2.978115
2.493525 1.963710
-2.513661 2.642162
1.864375 -3.176309
-3.171184 -3.572452
2.894220 2.489128
-2.562539 2.884438
3.491078 -3.947487
-2.565729 -2.012114
3.332948 3.983102
-1.616805 3.573188
2.280615 -2.559444
-2.651229 -3.103198
2.321395 3.154987
-1.685703 2.939697
3.031012 -3.620252
-4.599622 -2.185829
4.196223 1.126677
-2.133863 3.093686
4.668892 -2.562705
-2.793241 -2.149706
2.884105 3.043438
-2.967647 2.848696
4.479332 -1.764772
-4.905566 -2.911070
================================================
FILE: KNN/KNN.m
================================================
function relustLabel = KNN(inx,data,labels,k)
%%
% inx 为 输入测试数据,data为样本数据,labels为样本标签
%%
[datarow , datacol] = size(data);
diffMat = repmat(inx,[datarow,1]) - data ;
distanceMat = sqrt(sum(diffMat.^2,2));
[B , IX] = sort(distanceMat,'ascend');
len = min(k,length(B));
relustLabel = mode(labels(IX(1:len)));
end
================================================
FILE: KNN/KNN.py
================================================
from numpy import *
import operator
def createDataSet():
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
lables = ['A','A','B','B']
return group,lables
# KNN 分类算法
def classify0(inx,dataSet,labels,k):
dataSetSize = dataSet.shape[0] # shape[0]获取行 shape[1] 获取列
# 第一步,计算欧式距离
diffMat = tile(inx,(dataSetSize,1)) - dataSet #tile类似于matlab中的repmat,复制矩阵
sqDiffMat = diffMat ** 2
sqDistances = sqDiffMat.sum(axis=1)
distance = sqDistances ** 0.5
sortedDistIndecies = distance.argsort() # 增序排序
classCount = {}
for i in range(k):
# 获取类别
voteIlabel = labels[sortedDistIndecies[i]]
#字典的get方法,查找classCount中是否包含voteIlabel,是则返回该值,不是则返回defValue,这里是0
# 其实这也就是计算K临近点中出现的类别的频率,以次数体现
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
# 对字典中的类别出现次数进行排序,classCount中存储的事 key-value,其中key就是label,value就是出现的次数
# 所以key=operator.itemgetter(1)选中的事value,也就是对次数进行排序
sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
#sortedClassCount[0][0]也就是排序后的次数最大的那个label
return sortedClassCount[0][0]
================================================
FILE: KNN/KNNdatgingTest.m
================================================
function KNNdatgingTest
%%
clc
clear
close all
%%
data = load('datingTestSet2.txt');
dataMat = data(:,1:3);
labels = data(:,4);
len = size(dataMat,1);
k = 4;
error = 0;
% 测试数据比例
Ratio = 0.1;
numTest = Ratio * len;
% 归一化处理
maxV = max(dataMat);
minV = min(dataMat);
range = maxV-minV;
newdataMat = (dataMat-repmat(minV,[len,1]))./(repmat(range,[len,1]));
% 测试
for i = 1:numTest
classifyresult = KNN(newdataMat(i,:),newdataMat(numTest:len,:),labels(numTest:len,:),k);
fprintf('测试结果为:%d 真实结果为:%d\n',[classifyresult labels(i)])
if(classifyresult~=labels(i))
error = error+1;
end
end
fprintf('准确率为:%f\n',1-error/(numTest))
end
================================================
FILE: KNN/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/45040685
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: KNN/datingTestSet2.txt
================================================
40920 8.326976 0.953952 3
14488 7.153469 1.673904 2
26052 1.441871 0.805124 1
75136 13.147394 0.428964 1
38344 1.669788 0.134296 1
72993 10.141740 1.032955 1
35948 6.830792 1.213192 3
42666 13.276369 0.543880 3
67497 8.631577 0.749278 1
35483 12.273169 1.508053 3
50242 3.723498 0.831917 1
63275 8.385879 1.669485 1
5569 4.875435 0.728658 2
51052 4.680098 0.625224 1
77372 15.299570 0.331351 1
43673 1.889461 0.191283 1
61364 7.516754 1.269164 1
69673 14.239195 0.261333 1
15669 0.000000 1.250185 2
28488 10.528555 1.304844 3
6487 3.540265 0.822483 2
37708 2.991551 0.833920 1
22620 5.297865 0.638306 2
28782 6.593803 0.187108 3
19739 2.816760 1.686209 2
36788 12.458258 0.649617 3
5741 0.000000 1.656418 2
28567 9.968648 0.731232 3
6808 1.364838 0.640103 2
41611 0.230453 1.151996 1
36661 11.865402 0.882810 3
43605 0.120460 1.352013 1
15360 8.545204 1.340429 3
63796 5.856649 0.160006 1
10743 9.665618 0.778626 2
70808 9.778763 1.084103 1
72011 4.932976 0.632026 1
5914 2.216246 0.587095 2
14851 14.305636 0.632317 3
33553 12.591889 0.686581 3
44952 3.424649 1.004504 1
17934 0.000000 0.147573 2
27738 8.533823 0.205324 3
29290 9.829528 0.238620 3
42330 11.492186 0.263499 3
36429 3.570968 0.832254 1
39623 1.771228 0.207612 1
32404 3.513921 0.991854 1
27268 4.398172 0.975024 1
5477 4.276823 1.174874 2
14254 5.946014 1.614244 2
68613 13.798970 0.724375 1
41539 10.393591 1.663724 3
7917 3.007577 0.297302 2
21331 1.031938 0.486174 2
8338 4.751212 0.064693 2
5176 3.692269 1.655113 2
18983 10.448091 0.267652 3
68837 10.585786 0.329557 1
13438 1.604501 0.069064 2
48849 3.679497 0.961466 1
12285 3.795146 0.696694 2
7826 2.531885 1.659173 2
5565 9.733340 0.977746 2
10346 6.093067 1.413798 2
1823 7.712960 1.054927 2
9744 11.470364 0.760461 3
16857 2.886529 0.934416 2
39336 10.054373 1.138351 3
65230 9.972470 0.881876 1
2463 2.335785 1.366145 2
27353 11.375155 1.528626 3
16191 0.000000 0.605619 2
12258 4.126787 0.357501 2
42377 6.319522 1.058602 1
25607 8.680527 0.086955 3
77450 14.856391 1.129823 1
58732 2.454285 0.222380 1
46426 7.292202 0.548607 3
32688 8.745137 0.857348 3
64890 8.579001 0.683048 1
8554 2.507302 0.869177 2
28861 11.415476 1.505466 3
42050 4.838540 1.680892 1
32193 10.339507 0.583646 3
64895 6.573742 1.151433 1
2355 6.539397 0.462065 2
0 2.209159 0.723567 2
70406 11.196378 0.836326 1
57399 4.229595 0.128253 1
41732 9.505944 0.005273 3
11429 8.652725 1.348934 3
75270 17.101108 0.490712 1
5459 7.871839 0.717662 2
73520 8.262131 1.361646 1
40279 9.015635 1.658555 3
21540 9.215351 0.806762 3
17694 6.375007 0.033678 2
22329 2.262014 1.022169 1
46570 5.677110 0.709469 1
42403 11.293017 0.207976 3
33654 6.590043 1.353117 1
9171 4.711960 0.194167 2
28122 8.768099 1.108041 3
34095 11.502519 0.545097 3
1774 4.682812 0.578112 2
40131 12.446578 0.300754 3
13994 12.908384 1.657722 3
77064 12.601108 0.974527 1
11210 3.929456 0.025466 2
6122 9.751503 1.182050 3
15341 3.043767 0.888168 2
44373 4.391522 0.807100 1
28454 11.695276 0.679015 3
63771 7.879742 0.154263 1
9217 5.613163 0.933632 2
69076 9.140172 0.851300 1
24489 4.258644 0.206892 1
16871 6.799831 1.221171 2
39776 8.752758 0.484418 3
5901 1.123033 1.180352 2
40987 10.833248 1.585426 3
7479 3.051618 0.026781 2
38768 5.308409 0.030683 3
4933 1.841792 0.028099 2
32311 2.261978 1.605603 1
26501 11.573696 1.061347 3
37433 8.038764 1.083910 3
23503 10.734007 0.103715 3
68607 9.661909 0.350772 1
27742 9.005850 0.548737 3
11303 0.000000 0.539131 2
0 5.757140 1.062373 2
32729 9.164656 1.624565 3
24619 1.318340 1.436243 1
42414 14.075597 0.695934 3
20210 10.107550 1.308398 3
33225 7.960293 1.219760 3
54483 6.317292 0.018209 1
18475 12.664194 0.595653 3
33926 2.906644 0.581657 1
43865 2.388241 0.913938 1
26547 6.024471 0.486215 3
44404 7.226764 1.255329 3
16674 4.183997 1.275290 2
8123 11.850211 1.096981 3
42747 11.661797 1.167935 3
56054 3.574967 0.494666 1
10933 0.000000 0.107475 2
18121 7.937657 0.904799 3
11272 3.365027 1.014085 2
16297 0.000000 0.367491 2
28168 13.860672 1.293270 3
40963 10.306714 1.211594 3
31685 7.228002 0.670670 3
55164 4.508740 1.036192 1
17595 0.366328 0.163652 2
1862 3.299444 0.575152 2
57087 0.573287 0.607915 1
63082 9.183738 0.012280 1
51213 7.842646 1.060636 3
6487 4.750964 0.558240 2
4805 11.438702 1.556334 3
30302 8.243063 1.122768 3
68680 7.949017 0.271865 1
17591 7.875477 0.227085 2
74391 9.569087 0.364856 1
37217 7.750103 0.869094 3
42814 0.000000 1.515293 1
14738 3.396030 0.633977 2
19896 11.916091 0.025294 3
14673 0.460758 0.689586 2
32011 13.087566 0.476002 3
58736 4.589016 1.672600 1
54744 8.397217 1.534103 1
29482 5.562772 1.689388 1
27698 10.905159 0.619091 3
11443 1.311441 1.169887 2
56117 10.647170 0.980141 3
39514 0.000000 0.481918 1
26627 8.503025 0.830861 3
16525 0.436880 1.395314 2
24368 6.127867 1.102179 1
22160 12.112492 0.359680 3
6030 1.264968 1.141582 2
6468 6.067568 1.327047 2
22945 8.010964 1.681648 3
18520 3.791084 0.304072 2
34914 11.773195 1.262621 3
6121 8.339588 1.443357 2
38063 2.563092 1.464013 1
23410 5.954216 0.953782 1
35073 9.288374 0.767318 3
52914 3.976796 1.043109 1
16801 8.585227 1.455708 3
9533 1.271946 0.796506 2
16721 0.000000 0.242778 2
5832 0.000000 0.089749 2
44591 11.521298 0.300860 3
10143 1.139447 0.415373 2
21609 5.699090 1.391892 2
23817 2.449378 1.322560 1
15640 0.000000 1.228380 2
8847 3.168365 0.053993 2
50939 10.428610 1.126257 3
28521 2.943070 1.446816 1
32901 10.441348 0.975283 3
42850 12.478764 1.628726 3
13499 5.856902 0.363883 2
40345 2.476420 0.096075 1
43547 1.826637 0.811457 1
70758 4.324451 0.328235 1
19780 1.376085 1.178359 2
44484 5.342462 0.394527 1
54462 11.835521 0.693301 3
20085 12.423687 1.424264 3
42291 12.161273 0.071131 3
47550 8.148360 1.649194 3
11938 1.531067 1.549756 2
40699 3.200912 0.309679 1
70908 8.862691 0.530506 1
73989 6.370551 0.369350 1
11872 2.468841 0.145060 2
48463 11.054212 0.141508 3
15987 2.037080 0.715243 2
70036 13.364030 0.549972 1
32967 10.249135 0.192735 3
63249 10.464252 1.669767 1
42795 9.424574 0.013725 3
14459 4.458902 0.268444 2
19973 0.000000 0.575976 2
5494 9.686082 1.029808 3
67902 13.649402 1.052618 1
25621 13.181148 0.273014 3
27545 3.877472 0.401600 1
58656 1.413952 0.451380 1
7327 4.248986 1.430249 2
64555 8.779183 0.845947 1
8998 4.156252 0.097109 2
11752 5.580018 0.158401 2
76319 15.040440 1.366898 1
27665 12.793870 1.307323 3
67417 3.254877 0.669546 1
21808 10.725607 0.588588 3
15326 8.256473 0.765891 2
20057 8.033892 1.618562 3
79341 10.702532 0.204792 1
15636 5.062996 1.132555 2
35602 10.772286 0.668721 3
28544 1.892354 0.837028 1
57663 1.019966 0.372320 1
78727 15.546043 0.729742 1
68255 11.638205 0.409125 1
14964 3.427886 0.975616 2
21835 11.246174 1.475586 3
7487 0.000000 0.645045 2
8700 0.000000 1.424017 2
26226 8.242553 0.279069 3
65899 8.700060 0.101807 1
6543 0.812344 0.260334 2
46556 2.448235 1.176829 1
71038 13.230078 0.616147 1
47657 0.236133 0.340840 1
19600 11.155826 0.335131 3
37422 11.029636 0.505769 3
1363 2.901181 1.646633 2
26535 3.924594 1.143120 1
47707 2.524806 1.292848 1
38055 3.527474 1.449158 1
6286 3.384281 0.889268 2
10747 0.000000 1.107592 2
44883 11.898890 0.406441 3
56823 3.529892 1.375844 1
68086 11.442677 0.696919 1
70242 10.308145 0.422722 1
11409 8.540529 0.727373 2
67671 7.156949 1.691682 1
61238 0.720675 0.847574 1
17774 0.229405 1.038603 2
53376 3.399331 0.077501 1
30930 6.157239 0.580133 1
28987 1.239698 0.719989 1
13655 6.036854 0.016548 2
7227 5.258665 0.933722 2
40409 12.393001 1.571281 3
13605 9.627613 0.935842 2
26400 11.130453 0.597610 3
13491 8.842595 0.349768 3
30232 10.690010 1.456595 3
43253 5.714718 1.674780 3
55536 3.052505 1.335804 1
8807 0.000000 0.059025 2
25783 9.945307 1.287952 3
22812 2.719723 1.142148 1
77826 11.154055 1.608486 1
38172 2.687918 0.660836 1
31676 10.037847 0.962245 3
74038 12.404762 1.112080 1
44738 10.237305 0.633422 3
17410 4.745392 0.662520 2
5688 4.639461 1.569431 2
36642 3.149310 0.639669 1
29956 13.406875 1.639194 3
60350 6.068668 0.881241 1
23758 9.477022 0.899002 3
25780 3.897620 0.560201 2
11342 5.463615 1.203677 2
36109 3.369267 1.575043 1
14292 5.234562 0.825954 2
11160 0.000000 0.722170 2
23762 12.979069 0.504068 3
39567 5.376564 0.557476 1
25647 13.527910 1.586732 3
14814 2.196889 0.784587 2
73590 10.691748 0.007509 1
35187 1.659242 0.447066 1
49459 8.369667 0.656697 3
31657 13.157197 0.143248 3
6259 8.199667 0.908508 2
33101 4.441669 0.439381 3
27107 9.846492 0.644523 3
17824 0.019540 0.977949 2
43536 8.253774 0.748700 3
67705 6.038620 1.509646 1
35283 6.091587 1.694641 3
71308 8.986820 1.225165 1
31054 11.508473 1.624296 3
52387 8.807734 0.713922 3
40328 0.000000 0.816676 1
34844 8.889202 1.665414 3
11607 3.178117 0.542752 2
64306 7.013795 0.139909 1
32721 9.605014 0.065254 3
33170 1.230540 1.331674 1
37192 10.412811 0.890803 3
13089 0.000000 0.567161 2
66491 9.699991 0.122011 1
15941 0.000000 0.061191 2
4272 4.455293 0.272135 2
48812 3.020977 1.502803 1
28818 8.099278 0.216317 3
35394 1.157764 1.603217 1
71791 10.105396 0.121067 1
40668 11.230148 0.408603 3
39580 9.070058 0.011379 3
11786 0.566460 0.478837 2
19251 0.000000 0.487300 2
56594 8.956369 1.193484 3
54495 1.523057 0.620528 1
11844 2.749006 0.169855 2
45465 9.235393 0.188350 3
31033 10.555573 0.403927 3
16633 6.956372 1.519308 2
13887 0.636281 1.273984 2
52603 3.574737 0.075163 1
72000 9.032486 1.461809 1
68497 5.958993 0.023012 1
35135 2.435300 1.211744 1
26397 10.539731 1.638248 3
7313 7.646702 0.056513 2
91273 20.919349 0.644571 1
24743 1.424726 0.838447 1
31690 6.748663 0.890223 3
15432 2.289167 0.114881 2
58394 5.548377 0.402238 1
33962 6.057227 0.432666 1
31442 10.828595 0.559955 3
31044 11.318160 0.271094 3
29938 13.265311 0.633903 3
9875 0.000000 1.496715 2
51542 6.517133 0.402519 3
11878 4.934374 1.520028 2
69241 10.151738 0.896433 1
37776 2.425781 1.559467 1
68997 9.778962 1.195498 1
67416 12.219950 0.657677 1
59225 7.394151 0.954434 1
29138 8.518535 0.742546 3
5962 2.798700 0.662632 2
10847 0.637930 0.617373 2
70527 10.750490 0.097415 1
9610 0.625382 0.140969 2
64734 10.027968 0.282787 1
25941 9.817347 0.364197 3
2763 0.646828 1.266069 2
55601 3.347111 0.914294 1
31128 11.816892 0.193798 3
5181 0.000000 1.480198 2
69982 10.945666 0.993219 1
52440 10.244706 0.280539 3
57350 2.579801 1.149172 1
57869 2.630410 0.098869 1
56557 11.746200 1.695517 3
42342 8.104232 1.326277 3
15560 12.409743 0.790295 3
34826 12.167844 1.328086 3
8569 3.198408 0.299287 2
77623 16.055513 0.541052 1
78184 7.138659 0.158481 1
7036 4.831041 0.761419 2
69616 10.082890 1.373611 1
21546 10.066867 0.788470 3
36715 8.129538 0.329913 3
20522 3.012463 1.138108 2
42349 3.720391 0.845974 1
9037 0.773493 1.148256 2
26728 10.962941 1.037324 3
587 0.177621 0.162614 2
48915 3.085853 0.967899 1
9824 8.426781 0.202558 2
4135 1.825927 1.128347 2
9666 2.185155 1.010173 2
59333 7.184595 1.261338 1
36198 0.000000 0.116525 1
34909 8.901752 1.033527 3
47516 2.451497 1.358795 1
55807 3.213631 0.432044 1
14036 3.974739 0.723929 2
42856 9.601306 0.619232 3
64007 8.363897 0.445341 1
59428 6.381484 1.365019 1
13730 0.000000 1.403914 2
41740 9.609836 1.438105 3
63546 9.904741 0.985862 1
30417 7.185807 1.489102 3
69636 5.466703 1.216571 1
64660 0.000000 0.915898 1
14883 4.575443 0.535671 2
7965 3.277076 1.010868 2
68620 10.246623 1.239634 1
8738 2.341735 1.060235 2
7544 3.201046 0.498843 2
6377 6.066013 0.120927 2
36842 8.829379 0.895657 3
81046 15.833048 1.568245 1
67736 13.516711 1.220153 1
32492 0.664284 1.116755 1
39299 6.325139 0.605109 3
77289 8.677499 0.344373 1
33835 8.188005 0.964896 3
71890 9.414263 0.384030 1
32054 9.196547 1.138253 3
38579 10.202968 0.452363 3
55984 2.119439 1.481661 1
72694 13.635078 0.858314 1
42299 0.083443 0.701669 1
26635 9.149096 1.051446 3
8579 1.933803 1.374388 2
37302 14.115544 0.676198 3
22878 8.933736 0.943352 3
4364 2.661254 0.946117 2
4985 0.988432 1.305027 2
37068 2.063741 1.125946 1
41137 2.220590 0.690754 1
67759 6.424849 0.806641 1
11831 1.156153 1.613674 2
34502 3.032720 0.601847 1
4088 3.076828 0.952089 2
15199 0.000000 0.318105 2
17309 7.750480 0.554015 3
42816 10.958135 1.482500 3
43751 10.222018 0.488678 3
58335 2.367988 0.435741 1
75039 7.686054 1.381455 1
42878 11.464879 1.481589 3
42770 11.075735 0.089726 3
8848 3.543989 0.345853 2
31340 8.123889 1.282880 3
41413 4.331769 0.754467 3
12731 0.120865 1.211961 2
22447 6.116109 0.701523 3
33564 7.474534 0.505790 3
48907 8.819454 0.649292 3
8762 6.802144 0.615284 2
46696 12.666325 0.931960 3
36851 8.636180 0.399333 3
67639 11.730991 1.289833 1
171 8.132449 0.039062 2
26674 10.296589 1.496144 3
8739 7.583906 1.005764 2
66668 9.777806 0.496377 1
68732 8.833546 0.513876 1
69995 4.907899 1.518036 1
82008 8.362736 1.285939 1
25054 9.084726 1.606312 3
33085 14.164141 0.560970 3
41379 9.080683 0.989920 3
39417 6.522767 0.038548 3
12556 3.690342 0.462281 2
39432 3.563706 0.242019 1
38010 1.065870 1.141569 1
69306 6.683796 1.456317 1
38000 1.712874 0.243945 1
46321 13.109929 1.280111 3
66293 11.327910 0.780977 1
22730 4.545711 1.233254 1
5952 3.367889 0.468104 2
72308 8.326224 0.567347 1
60338 8.978339 1.442034 1
13301 5.655826 1.582159 2
27884 8.855312 0.570684 3
11188 6.649568 0.544233 2
56796 3.966325 0.850410 1
8571 1.924045 1.664782 2
4914 6.004812 0.280369 2
10784 0.000000 0.375849 2
39296 9.923018 0.092192 3
13113 2.389084 0.119284 2
70204 13.663189 0.133251 1
46813 11.434976 0.321216 3
11697 0.358270 1.292858 2
44183 9.598873 0.223524 3
2225 6.375275 0.608040 2
29066 11.580532 0.458401 3
4245 5.319324 1.598070 2
34379 4.324031 1.603481 1
44441 2.358370 1.273204 1
2022 0.000000 1.182708 2
26866 12.824376 0.890411 3
57070 1.587247 1.456982 1
32932 8.510324 1.520683 3
51967 10.428884 1.187734 3
44432 8.346618 0.042318 3
67066 7.541444 0.809226 1
17262 2.540946 1.583286 2
79728 9.473047 0.692513 1
14259 0.352284 0.474080 2
6122 0.000000 0.589826 2
76879 12.405171 0.567201 1
11426 4.126775 0.871452 2
2493 0.034087 0.335848 2
19910 1.177634 0.075106 2
10939 0.000000 0.479996 2
17716 0.994909 0.611135 2
31390 11.053664 1.180117 3
20375 0.000000 1.679729 2
26309 2.495011 1.459589 1
33484 11.516831 0.001156 3
45944 9.213215 0.797743 3
4249 5.332865 0.109288 2
6089 0.000000 1.689771 2
7513 0.000000 1.126053 2
27862 12.640062 1.690903 3
39038 2.693142 1.317518 1
19218 3.328969 0.268271 2
62911 7.193166 1.117456 1
77758 6.615512 1.521012 1
27940 8.000567 0.835341 3
2194 4.017541 0.512104 2
37072 13.245859 0.927465 3
15585 5.970616 0.813624 2
25577 11.668719 0.886902 3
8777 4.283237 1.272728 2
29016 10.742963 0.971401 3
21910 12.326672 1.592608 3
12916 0.000000 0.344622 2
10976 0.000000 0.922846 2
79065 10.602095 0.573686 1
36759 10.861859 1.155054 3
50011 1.229094 1.638690 1
1155 0.410392 1.313401 2
71600 14.552711 0.616162 1
30817 14.178043 0.616313 3
54559 14.136260 0.362388 1
29764 0.093534 1.207194 1
69100 10.929021 0.403110 1
47324 11.432919 0.825959 3
73199 9.134527 0.586846 1
44461 5.071432 1.421420 1
45617 11.460254 1.541749 3
28221 11.620039 1.103553 3
7091 4.022079 0.207307 2
6110 3.057842 1.631262 2
79016 7.782169 0.404385 1
18289 7.981741 0.929789 3
43679 4.601363 0.268326 1
22075 2.595564 1.115375 1
23535 10.049077 0.391045 3
25301 3.265444 1.572970 2
32256 11.780282 1.511014 3
36951 3.075975 0.286284 1
31290 1.795307 0.194343 1
38953 11.106979 0.202415 3
35257 5.994413 0.800021 1
25847 9.706062 1.012182 3
32680 10.582992 0.836025 3
62018 7.038266 1.458979 1
9074 0.023771 0.015314 2
33004 12.823982 0.676371 3
44588 3.617770 0.493483 1
32565 8.346684 0.253317 3
38563 6.104317 0.099207 1
75668 16.207776 0.584973 1
9069 6.401969 1.691873 2
53395 2.298696 0.559757 1
28631 7.661515 0.055981 3
71036 6.353608 1.645301 1
71142 10.442780 0.335870 1
37653 3.834509 1.346121 1
76839 10.998587 0.584555 1
9916 2.695935 1.512111 2
38889 3.356646 0.324230 1
39075 14.677836 0.793183 3
48071 1.551934 0.130902 1
7275 2.464739 0.223502 2
41804 1.533216 1.007481 1
35665 12.473921 0.162910 3
67956 6.491596 0.032576 1
41892 10.506276 1.510747 3
38844 4.380388 0.748506 1
74197 13.670988 1.687944 1
14201 8.317599 0.390409 2
3908 0.000000 0.556245 2
2459 0.000000 0.290218 2
32027 10.095799 1.188148 3
12870 0.860695 1.482632 2
9880 1.557564 0.711278 2
72784 10.072779 0.756030 1
17521 0.000000 0.431468 2
50283 7.140817 0.883813 3
33536 11.384548 1.438307 3
9452 3.214568 1.083536 2
37457 11.720655 0.301636 3
17724 6.374475 1.475925 3
43869 5.749684 0.198875 3
264 3.871808 0.552602 2
25736 8.336309 0.636238 3
39584 9.710442 1.503735 3
31246 1.532611 1.433898 1
49567 9.785785 0.984614 3
7052 2.633627 1.097866 2
35493 9.238935 0.494701 3
10986 1.205656 1.398803 2
49508 3.124909 1.670121 1
5734 7.935489 1.585044 2
65479 12.746636 1.560352 1
77268 10.732563 0.545321 1
28490 3.977403 0.766103 1
13546 4.194426 0.450663 2
37166 9.610286 0.142912 3
16381 4.797555 1.260455 2
10848 1.615279 0.093002 2
35405 4.614771 1.027105 1
15917 0.000000 1.369726 2
6131 0.608457 0.512220 2
67432 6.558239 0.667579 1
30354 12.315116 0.197068 3
69696 7.014973 1.494616 1
33481 8.822304 1.194177 3
43075 10.086796 0.570455 3
38343 7.241614 1.661627 3
14318 4.602395 1.511768 2
5367 7.434921 0.079792 2
37894 10.467570 1.595418 3
36172 9.948127 0.003663 3
40123 2.478529 1.568987 1
10976 5.938545 0.878540 2
12705 0.000000 0.948004 2
12495 5.559181 1.357926 2
35681 9.776654 0.535966 3
46202 3.092056 0.490906 1
11505 0.000000 1.623311 2
22834 4.459495 0.538867 1
49901 8.334306 1.646600 3
71932 11.226654 0.384686 1
13279 3.904737 1.597294 2
49112 7.038205 1.211329 3
77129 9.836120 1.054340 1
37447 1.990976 0.378081 1
62397 9.005302 0.485385 1
0 1.772510 1.039873 2
15476 0.458674 0.819560 2
40625 10.003919 0.231658 3
36706 0.520807 1.476008 1
28580 10.678214 1.431837 3
25862 4.425992 1.363842 1
63488 12.035355 0.831222 1
33944 10.606732 1.253858 3
30099 1.568653 0.684264 1
13725 2.545434 0.024271 2
36768 10.264062 0.982593 3
64656 9.866276 0.685218 1
14927 0.142704 0.057455 2
43231 9.853270 1.521432 3
66087 6.596604 1.653574 1
19806 2.602287 1.321481 2
41081 10.411776 0.664168 3
10277 7.083449 0.622589 2
7014 2.080068 1.254441 2
17275 0.522844 1.622458 2
31600 10.362000 1.544827 3
59956 3.412967 1.035410 1
42181 6.796548 1.112153 3
51743 4.092035 0.075804 1
5194 2.763811 1.564325 2
30832 12.547439 1.402443 3
7976 5.708052 1.596152 2
14602 4.558025 0.375806 2
41571 11.642307 0.438553 3
55028 3.222443 0.121399 1
5837 4.736156 0.029871 2
39808 10.839526 0.836323 3
20944 4.194791 0.235483 2
22146 14.936259 0.888582 3
42169 3.310699 1.521855 1
7010 2.971931 0.034321 2
3807 9.261667 0.537807 2
29241 7.791833 1.111416 3
52696 1.480470 1.028750 1
42545 3.677287 0.244167 1
24437 2.202967 1.370399 1
16037 5.796735 0.935893 2
8493 3.063333 0.144089 2
68080 11.233094 0.492487 1
59016 1.965570 0.005697 1
11810 8.616719 0.137419 2
68630 6.609989 1.083505 1
7629 1.712639 1.086297 2
71992 10.117445 1.299319 1
13398 0.000000 1.104178 2
26241 9.824777 1.346821 3
11160 1.653089 0.980949 2
76701 18.178822 1.473671 1
32174 6.781126 0.885340 3
45043 8.206750 1.549223 3
42173 10.081853 1.376745 3
69801 6.288742 0.112799 1
41737 3.695937 1.543589 1
46979 6.726151 1.069380 3
79267 12.969999 1.568223 1
4615 2.661390 1.531933 2
32907 7.072764 1.117386 3
37444 9.123366 1.318988 3
569 3.743946 1.039546 2
8723 2.341300 0.219361 2
6024 0.541913 0.592348 2
52252 2.310828 1.436753 1
8358 6.226597 1.427316 2
26166 7.277876 0.489252 3
18471 0.000000 0.389459 2
3386 7.218221 1.098828 2
41544 8.777129 1.111464 3
10480 2.813428 0.819419 2
5894 2.268766 1.412130 2
7273 6.283627 0.571292 2
22272 7.520081 1.626868 3
31369 11.739225 0.027138 3
10708 3.746883 0.877350 2
69364 12.089835 0.521631 1
37760 12.310404 0.259339 3
13004 0.000000 0.671355 2
37885 2.728800 0.331502 1
52555 10.814342 0.607652 3
38997 12.170268 0.844205 3
69698 6.698371 0.240084 1
11783 3.632672 1.643479 2
47636 10.059991 0.892361 3
15744 1.887674 0.756162 2
69058 8.229125 0.195886 1
33057 7.817082 0.476102 3
28681 12.277230 0.076805 3
34042 10.055337 1.115778 3
29928 3.596002 1.485952 1
9734 2.755530 1.420655 2
7344 7.780991 0.513048 2
7387 0.093705 0.391834 2
33957 8.481567 0.520078 3
9936 3.865584 0.110062 2
36094 9.683709 0.779984 3
39835 10.617255 1.359970 3
64486 7.203216 1.624762 1
0 7.601414 1.215605 2
39539 1.386107 1.417070 1
66972 9.129253 0.594089 1
15029 1.363447 0.620841 2
44909 3.181399 0.359329 1
38183 13.365414 0.217011 3
37372 4.207717 1.289767 1
0 4.088395 0.870075 2
17786 3.327371 1.142505 2
39055 1.303323 1.235650 1
37045 7.999279 1.581763 3
6435 2.217488 0.864536 2
72265 7.751808 0.192451 1
28152 14.149305 1.591532 3
25931 8.765721 0.152808 3
7538 3.408996 0.184896 2
1315 1.251021 0.112340 2
12292 6.160619 1.537165 2
49248 1.034538 1.585162 1
9025 0.000000 1.034635 2
13438 2.355051 0.542603 2
69683 6.614543 0.153771 1
25374 10.245062 1.450903 3
55264 3.467074 1.231019 1
38324 7.487678 1.572293 3
69643 4.624115 1.185192 1
44058 8.995957 1.436479 3
41316 11.564476 0.007195 3
29119 3.440948 0.078331 1
51656 1.673603 0.732746 1
3030 4.719341 0.699755 2
35695 10.304798 1.576488 3
1537 2.086915 1.199312 2
9083 6.338220 1.131305 2
47744 8.254926 0.710694 3
71372 16.067108 0.974142 1
37980 1.723201 0.310488 1
42385 3.785045 0.876904 1
22687 2.557561 0.123738 1
39512 9.852220 1.095171 3
11885 3.679147 1.557205 2
4944 9.789681 0.852971 2
73230 14.958998 0.526707 1
17585 11.182148 1.288459 3
68737 7.528533 1.657487 1
13818 5.253802 1.378603 2
31662 13.946752 1.426657 3
86686 15.557263 1.430029 1
43214 12.483550 0.688513 3
24091 2.317302 1.411137 1
52544 10.069724 0.766119 3
61861 5.792231 1.615483 1
47903 4.138435 0.475994 1
37190 12.929517 0.304378 3
6013 9.378238 0.307392 2
27223 8.361362 1.643204 3
69027 7.939406 1.325042 1
78642 10.735384 0.705788 1
30254 11.592723 0.286188 3
21704 10.098356 0.704748 3
34985 9.299025 0.545337 3
31316 11.158297 0.218067 3
76368 16.143900 0.558388 1
27953 10.971700 1.221787 3
152 0.000000 0.681478 2
9146 3.178961 1.292692 2
75346 17.625350 0.339926 1
26376 1.995833 0.267826 1
35255 10.640467 0.416181 3
19198 9.628339 0.985462 3
12518 4.662664 0.495403 2
25453 5.754047 1.382742 2
12530 0.000000 0.037146 2
62230 9.334332 0.198118 1
9517 3.846162 0.619968 2
71161 10.685084 0.678179 1
1593 4.752134 0.359205 2
33794 0.697630 0.966786 1
39710 10.365836 0.505898 3
16941 0.461478 0.352865 2
69209 11.339537 1.068740 1
4446 5.420280 0.127310 2
9347 3.469955 1.619947 2
55635 8.517067 0.994858 3
65889 8.306512 0.413690 1
10753 2.628690 0.444320 2
7055 0.000000 0.802985 2
7905 0.000000 1.170397 2
53447 7.298767 1.582346 3
9194 7.331319 1.277988 2
61914 9.392269 0.151617 1
15630 5.541201 1.180596 2
79194 15.149460 0.537540 1
12268 5.515189 0.250562 2
33682 7.728898 0.920494 3
26080 11.318785 1.510979 3
19119 3.574709 1.531514 2
30902 7.350965 0.026332 3
63039 7.122363 1.630177 1
51136 1.828412 1.013702 1
35262 10.117989 1.156862 3
42776 11.309897 0.086291 3
64191 8.342034 1.388569 1
15436 0.241714 0.715577 2
14402 10.482619 1.694972 2
6341 9.289510 1.428879 2
14113 4.269419 0.134181 2
6390 0.000000 0.189456 2
8794 0.817119 0.143668 2
43432 1.508394 0.652651 1
38334 9.359918 0.052262 3
34068 10.052333 0.550423 3
30819 11.111660 0.989159 3
22239 11.265971 0.724054 3
28725 10.383830 0.254836 3
57071 3.878569 1.377983 1
72420 13.679237 0.025346 1
28294 10.526846 0.781569 3
9896 0.000000 0.924198 2
65821 4.106727 1.085669 1
7645 8.118856 1.470686 2
71289 7.796874 0.052336 1
5128 2.789669 1.093070 2
13711 6.226962 0.287251 2
22240 10.169548 1.660104 3
15092 0.000000 1.370549 2
5017 7.513353 0.137348 2
10141 8.240793 0.099735 2
35570 14.612797 1.247390 3
46893 3.562976 0.445386 1
8178 3.230482 1.331698 2
55783 3.612548 1.551911 1
1148 0.000000 0.332365 2
10062 3.931299 0.487577 2
74124 14.752342 1.155160 1
66603 10.261887 1.628085 1
11893 2.787266 1.570402 2
50908 15.112319 1.324132 3
39891 5.184553 0.223382 3
65915 3.868359 0.128078 1
65678 3.507965 0.028904 1
62996 11.019254 0.427554 1
36851 3.812387 0.655245 1
36669 11.056784 0.378725 3
38876 8.826880 1.002328 3
26878 11.173861 1.478244 3
46246 11.506465 0.421993 3
12761 7.798138 0.147917 3
35282 10.155081 1.370039 3
68306 10.645275 0.693453 1
31262 9.663200 1.521541 3
34754 10.790404 1.312679 3
13408 2.810534 0.219962 2
30365 9.825999 1.388500 3
10709 1.421316 0.677603 2
24332 11.123219 0.809107 3
45517 13.402206 0.661524 3
6178 1.212255 0.836807 2
10639 1.568446 1.297469 2
29613 3.343473 1.312266 1
22392 5.400155 0.193494 1
51126 3.818754 0.590905 1
53644 7.973845 0.307364 3
51417 9.078824 0.734876 3
24859 0.153467 0.766619 1
61732 8.325167 0.028479 1
71128 7.092089 1.216733 1
27276 5.192485 1.094409 3
30453 10.340791 1.087721 3
18670 2.077169 1.019775 2
70600 10.151966 0.993105 1
12683 0.046826 0.809614 2
81597 11.221874 1.395015 1
69959 14.497963 1.019254 1
8124 3.554508 0.533462 2
18867 3.522673 0.086725 2
80886 14.531655 0.380172 1
55895 3.027528 0.885457 1
31587 1.845967 0.488985 1
10591 10.226164 0.804403 3
70096 10.965926 1.212328 1
53151 2.129921 1.477378 1
11992 0.000000 1.606849 2
33114 9.489005 0.827814 3
7413 0.000000 1.020797 2
10583 0.000000 1.270167 2
58668 6.556676 0.055183 1
35018 9.959588 0.060020 3
70843 7.436056 1.479856 1
14011 0.404888 0.459517 2
35015 9.952942 1.650279 3
70839 15.600252 0.021935 1
3024 2.723846 0.387455 2
5526 0.513866 1.323448 2
5113 0.000000 0.861859 2
20851 7.280602 1.438470 2
40999 9.161978 1.110180 3
15823 0.991725 0.730979 2
35432 7.398380 0.684218 3
53711 12.149747 1.389088 3
64371 9.149678 0.874905 1
9289 9.666576 1.370330 2
60613 3.620110 0.287767 1
18338 5.238800 1.253646 2
22845 14.715782 1.503758 3
74676 14.445740 1.211160 1
34143 13.609528 0.364240 3
14153 3.141585 0.424280 2
9327 0.000000 0.120947 2
18991 0.454750 1.033280 2
9193 0.510310 0.016395 2
2285 3.864171 0.616349 2
9493 6.724021 0.563044 2
2371 4.289375 0.012563 2
13963 0.000000 1.437030 2
2299 3.733617 0.698269 2
5262 2.002589 1.380184 2
4659 2.502627 0.184223 2
17582 6.382129 0.876581 2
27750 8.546741 0.128706 3
9868 2.694977 0.432818 2
18333 3.951256 0.333300 2
3780 9.856183 0.329181 2
18190 2.068962 0.429927 2
11145 3.410627 0.631838 2
68846 9.974715 0.669787 1
26575 10.650102 0.866627 3
48111 9.134528 0.728045 3
43757 7.882601 1.332446 3
================================================
FILE: KNN/handWritingTest.m
================================================
function handWritingTest
%%
clc
clear
close all
%% 获取目录下的所有txt文件名称
d = dir(['digits/trainingDigits/' '*.txt']); % struct 类型
dircell = struct2cell(d); %cell 类型
trainSetLen = size(dircell,2);
K = 4;
dataSize = 1024;
trainLabels = zeros(trainSetLen,1);
trainSet = [];
simpleTrainSet = zeros(1,dataSize);
simpleTestSet = zeros(1,dataSize);
%% 加载数据
fprintf('loading data...')
for i = 1:trainSetLen
trainName = dircell(1,i);
trainFilename = cell2mat(trainName);
trainLabels(i) = str2num(trainFilename(1));
fid = fopen(['digits/trainingDigits/' trainFilename],'r');
traindata = fscanf(fid,'%s');
for j = 1:dataSize
simpleTrainSet(j) = str2num(traindata(j));
end
trainSet = [trainSet ; simpleTrainSet];
fclose(fid);
end
d = dir(['digits/testDigits/' '*.txt']); % struct 类型
dircell = struct2cell(d); %cell 类型
testSetLen = size(dircell,2);
error = 0;
%% 测试数据
for k = 1:testSetLen
testName = dircell(1,k);
testFilename = cell2mat(testName);
testLabels = str2num(testFilename(1));
fid = fopen(['digits/testDigits/' testFilename],'r');
testdata = fscanf(fid,'%s');
for j = 1:dataSize
simpleTestSet(j) = str2num(testdata(j));
end
classifyResult = KNN(simpleTestSet,trainSet,trainLabels,K);
fprintf('识别数字为:%d 真实数字为:%d\n' , [classifyResult , testLabels])
if(classifyResult~=testLabels)
error = error+1;
end
fclose(fid);
end
fprintf('识别准确率为:%f\n',1-error/testSetLen)
end
================================================
FILE: Kmeans/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/45095935
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: Kmeans/distEclud.m
================================================
% 计算欧式距离
function dist = distEclud(vecA,vecB)
dist = sum(power((vecA-vecB),2));
end
function dist = softDist(vecA,vecB)
dist = sum(abs(vecB-vecA));
end
================================================
FILE: Kmeans/kMeans.m
================================================
% K-means算法
function [centSet,clusterAssment] = kMeans(dataSet,K)
[row,col] = size(dataSet);
% 存储质心矩阵
centSet = zeros(K,col);
% 随机初始化质心
for i= 1:col
minV = min(dataSet(:,i));
rangV = max(dataSet(:,i)) - minV;
centSet(:,i) = bsxfun(@plus,minV,rangV*rand(K,1));
%centSet(:,i) = repmat(minV,[K,1]) + rangV*rand(K,1);
end
% 用于存储每个点被分配的cluster以及到质心的距离
clusterAssment = zeros(row,2);
clusterChange = true;
while clusterChange
clusterChange = false;
% 计算每个点应该被分配的cluster
for i = 1:row
% 这部分可能可以优化
minDist = inf;
minIndex = 0;
for j = 1:K
distCal = distEclud(dataSet(i,:) , centSet(j,:));
if (distCal < minDist)
minDist = distCal;
minIndex = j;
end
end
if minIndex ~= clusterAssment(i,1)
clusterChange = true;
end
clusterAssment(i,1) = minIndex;
clusterAssment(i,2) = minDist;
end
% 更新每个cluster 的质心
for j = 1:K
simpleCluster = find(clusterAssment(:,1) == j);
centSet(j,:) = mean(dataSet(simpleCluster',:));
end
end
end
================================================
FILE: Kmeans/testSet.txt
================================================
1.658985 4.285136
-3.453687 3.424321
4.838138 -1.151539
-5.379713 -3.362104
0.972564 2.924086
-3.567919 1.531611
0.450614 -3.302219
-3.487105 -1.724432
2.668759 1.594842
-3.156485 3.191137
3.165506 -3.999838
-2.786837 -3.099354
4.208187 2.984927
-2.123337 2.943366
0.704199 -0.479481
-0.392370 -3.963704
2.831667 1.574018
-0.790153 3.343144
2.943496 -3.357075
-3.195883 -2.283926
2.336445 2.875106
-1.786345 2.554248
2.190101 -1.906020
-3.403367 -2.778288
1.778124 3.880832
-1.688346 2.230267
2.592976 -2.054368
-4.007257 -3.207066
2.257734 3.387564
-2.679011 0.785119
0.939512 -4.023563
-3.674424 -2.261084
2.046259 2.735279
-3.189470 1.780269
4.372646 -0.822248
-2.579316 -3.497576
1.889034 5.190400
-0.798747 2.185588
2.836520 -2.658556
-3.837877 -3.253815
2.096701 3.886007
-2.709034 2.923887
3.367037 -3.184789
-2.121479 -4.232586
2.329546 3.179764
-3.284816 3.273099
3.091414 -3.815232
-3.762093 -2.432191
3.542056 2.778832
-1.736822 4.241041
2.127073 -2.983680
-4.323818 -3.938116
3.792121 5.135768
-4.786473 3.358547
2.624081 -3.260715
-4.009299 -2.978115
2.493525 1.963710
-2.513661 2.642162
1.864375 -3.176309
-3.171184 -3.572452
2.894220 2.489128
-2.562539 2.884438
3.491078 -3.947487
-2.565729 -2.012114
3.332948 3.983102
-1.616805 3.573188
2.280615 -2.559444
-2.651229 -3.103198
2.321395 3.154987
-1.685703 2.939697
3.031012 -3.620252
-4.599622 -2.185829
4.196223 1.126677
-2.133863 3.093686
4.668892 -2.562705
-2.793241 -2.149706
2.884105 3.043438
-2.967647 2.848696
4.479332 -1.764772
-4.905566 -2.911070
================================================
FILE: Kmeans/testkMeans.m
================================================
function testkMeans
clc
clear all
close all
K = 4;
dataSet = load('testSet.txt');
figure
scatter(dataSet(:,1),dataSet(:,2))
[row,col] = size(dataSet);
% 存储质心矩阵
centSet = zeros(K,col);
% 随机初始化质心
for i= 1:col
minV = min(dataSet(:,i));
rangV = max(dataSet(:,i)) - minV;
centSet(:,i) = repmat(minV,[K,1]) + rangV*rand(K,1);
end
% 用于存储每个点被分配的cluster以及到质心的距离
clusterAssment = zeros(row,2);
clusterChange = true;
while clusterChange
clusterChange = false;
% 计算每个点应该被分配的cluster
for i = 1:row
% 这部分可能可以优化
minDist = 10000;
minIndex = 0;
for j = 1:K
distCal = distEclud(dataSet(i,:) , centSet(j,:));
if (distCal < minDist)
minDist = distCal;
minIndex = j;
end
end
if minIndex ~= clusterAssment(i,1)
clusterChange = true;
end
clusterAssment(i,1) = minIndex;
clusterAssment(i,2) = minDist;
end
% 更新每个cluster 的质心
for j = 1:K
simpleCluster = find(clusterAssment(:,1) == j);
centSet(j,:) = mean(dataSet(simpleCluster',:));
end
centSet
end
figure
%scatter(dataSet(:,1),dataSet(:,2),5)
for i = 1:K
pointCluster = find(clusterAssment(:,1) == i);
scatter(dataSet(pointCluster,1),dataSet(pointCluster,2),5)
hold on
end
%hold on
scatter(centSet(:,1),centSet(:,2),300,'+')
hold off
end
% 计算欧式距离
function dist = distEclud(vecA,vecB)
dist = sum(power((vecA-vecB),2));
end
================================================
FILE: Logistic-regression/ImproveStocGradAscent.m
================================================
function ImproveStocGradAscent
%%
%
% Description : LogisticRegression using stocGradAsscent
% Author : Liulongpo
% Time:2015-4-18 10:57:25
%
%%
clc
clear
close all
%%
data = load('testSet.txt');
[row , col] = size(data);
dataMat = [ones(row,1) data(:,1:col-1)];
%alpha = 0.01;
numIter = 20;
labelMat = data(:,col);
weightVal = zeros(3,numIter*row);
weight = ones(col,1);
j = 0;
for k = 1:numIter
randIndex = randperm(row);
for i = 1:row
% 改进点 1
alpha = 4/(1.0+i+k)+0.01;
j = j+1;
% 改进点 2
h = sigmoid(dataMat(randIndex(i),:)*weight);
% 改进点 2
error = labelMat(randIndex(i)) - h;
% 改进点 2
weight = weight + alpha * error * dataMat(randIndex(i),:)';
weightVal(1,j) = weight(1);
weightVal(2,j) = weight(2);
weightVal(3,j) = weight(3);
end
end
figure
i = 1:numIter*row;
subplot(3,1,1)
plot(i,weightVal(1,:)),title('weight0')%,axis([0 numIter*row 0.8 7])
j = 1:numIter*row;
subplot(3,1,2)
plot(j,weightVal(2,:)),title('weight1')%,axis([0 numIter*row 0.3 1.2])
k = 1:numIter*row;
subplot(3,1,3)
plot(k,weightVal(3,:)),title('weight2')%,axis([0 numIter*row -1.2 -0.1])
figure
scatter(dataMat(find(labelMat(:)==0),2),dataMat(find(labelMat(:)==0),3),5);
hold on
scatter(dataMat(find(labelMat(:) == 1),2),dataMat(find(labelMat(:) == 1),3),5);
hold on
x = -3:0.1:3;
y = -(weight(1)+weight(2)*x)/weight(3);
plot(x,y,'r')
hold off
end
function returnVals = sigmoid(inX)
% 注意这里的sigmoid函数要用点除
returnVals = 1.0./(1.0+exp(-inX));
end
================================================
FILE: Logistic-regression/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/45114421
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: Logistic-regression/gradAscent.m
================================================
function weight = gradAscent
%%
%
% Description : LogisticRegression using gradAsscent
% Author : Liulongpo
% Time:2015-4-18 10:57:25
%
%%
clc
close all
clear
%%
data = load('testSet.txt');
[row , col] = size(data);
dataMat = data(:,1:col-1);
dataMat = [ones(row,1) dataMat] ;
labelMat = data(:,col);
alpha = 0.001;
maxCycle = 500;
weight = ones(col,1);
for i = 1:maxCycle
h = sigmoid((dataMat * weight)');
error = (labelMat - h');
weight = weight + alpha * dataMat' * error;
end
figure
scatter(dataMat(find(labelMat(:) == 0),2),dataMat(find(labelMat(:) == 0),3),3);
hold on
scatter(dataMat(find(labelMat(:) == 1),2),dataMat(find(labelMat(:) == 1),3),5);
hold on
x = -3:0.1:3;
y = (-weight(1)-weight(2)*x)/weight(3);
plot(x,y)
hold off
end
function returnVals = sigmoid(inX)
% 注意这里的sigmoid函数要用点除
returnVals = 1.0./(1.0+exp(-inX));
end
================================================
FILE: Logistic-regression/stocGradAscent.m
================================================
function stocGradAscent
%%
%
% Description : LogisticRegression using stocGradAsscent
% Author : Liulongpo
% Time:2015-4-18 10:57:25
%
%%
clc
clear
close all
%%
data = load('testSet.txt');
[row , col] = size(data);
dataMat = [ones(row,1) data(:,1:col-1)];
alpha = 0.01;
labelMat = data(:,col);
weight = ones(col,1);
for i = 1:row
h = sigmoid(dataMat(i,:)*weight);
error = labelMat(i) - h;
dataMat(i,:)
weight = weight + alpha * error * dataMat(i,:)'
end
figure
scatter(dataMat(find(labelMat(:)==0),2),dataMat(find(labelMat(:)==0),3),5);
hold on
scatter(dataMat(find(labelMat(:) == 1),2),dataMat(find(labelMat(:) == 1),3),5);
hold on
x = -3:0.1:3;
y = -(weight(1)+weight(2)*x)/weight(3);
plot(x,y)
hold off
end
function returnVals = sigmoid(inX)
% 注意这里的sigmoid函数要用点除
returnVals = 1.0./(1.0+exp(-inX));
end
================================================
FILE: Logistic-regression/testSet.txt
================================================
-0.017612 14.053064 0
-1.395634 4.662541 1
-0.752157 6.538620 0
-1.322371 7.152853 0
0.423363 11.054677 0
0.406704 7.067335 1
0.667394 12.741452 0
-2.460150 6.866805 1
0.569411 9.548755 0
-0.026632 10.427743 0
0.850433 6.920334 1
1.347183 13.175500 0
1.176813 3.167020 1
-1.781871 9.097953 0
-0.566606 5.749003 1
0.931635 1.589505 1
-0.024205 6.151823 1
-0.036453 2.690988 1
-0.196949 0.444165 1
1.014459 5.754399 1
1.985298 3.230619 1
-1.693453 -0.557540 1
-0.576525 11.778922 0
-0.346811 -1.678730 1
-2.124484 2.672471 1
1.217916 9.597015 0
-0.733928 9.098687 0
-3.642001 -1.618087 1
0.315985 3.523953 1
1.416614 9.619232 0
-0.386323 3.989286 1
0.556921 8.294984 1
1.224863 11.587360 0
-1.347803 -2.406051 1
1.196604 4.951851 1
0.275221 9.543647 0
0.470575 9.332488 0
-1.889567 9.542662 0
-1.527893 12.150579 0
-1.185247 11.309318 0
-0.445678 3.297303 1
1.042222 6.105155 1
-0.618787 10.320986 0
1.152083 0.548467 1
0.828534 2.676045 1
-1.237728 10.549033 0
-0.683565 -2.166125 1
0.229456 5.921938 1
-0.959885 11.555336 0
0.492911 10.993324 0
0.184992 8.721488 0
-0.355715 10.325976 0
-0.397822 8.058397 0
0.824839 13.730343 0
1.507278 5.027866 1
0.099671 6.835839 1
-0.344008 10.717485 0
1.785928 7.718645 1
-0.918801 11.560217 0
-0.364009 4.747300 1
-0.841722 4.119083 1
0.490426 1.960539 1
-0.007194 9.075792 0
0.356107 12.447863 0
0.342578 12.281162 0
-0.810823 -1.466018 1
2.530777 6.476801 1
1.296683 11.607559 0
0.475487 12.040035 0
-0.783277 11.009725 0
0.074798 11.023650 0
-1.337472 0.468339 1
-0.102781 13.763651 0
-0.147324 2.874846 1
0.518389 9.887035 0
1.015399 7.571882 0
-1.658086 -0.027255 1
1.319944 2.171228 1
2.056216 5.019981 1
-0.851633 4.375691 1
-1.510047 6.061992 0
-1.076637 -3.181888 1
1.821096 10.283990 0
3.010150 8.401766 1
-1.099458 1.688274 1
-0.834872 -1.733869 1
-0.846637 3.849075 1
1.400102 12.628781 0
1.752842 5.468166 1
0.078557 0.059736 1
0.089392 -0.715300 1
1.825662 12.693808 0
0.197445 9.744638 0
0.126117 0.922311 1
-0.679797 1.220530 1
0.677983 2.556666 1
0.761349 10.693862 0
-2.168791 0.143632 1
1.388610 9.341997 0
0.317029 14.739025 0
================================================
FILE: MLP/dualperceptron.py
================================================
# -*- coding: utf-8 -*-
'''
@Description : dualperceptron by python
@Author: Liu_Longpo
@Time: Sun Dec 20 12:57:00 2015
'''
import matplotlib.pyplot as plt
import numpy as np
import time
trainSet = []
w = []
a = []
b = 0
lens = 0
alpha = 0
Gram = []
trainLoss = []
def calInnerProduct(i, j):
global lens
res = 0
for p in range(lens):
res += trainSet[i][0][p] * trainSet[j][0][p]
return res
def AddVector(vec1, vec2):
retvec = []
for i in range(len(vec1)):
retvec.append(vec1[i] + vec2[i])
return retvec
def NumProduct(num, vec):
retvec = []
for i in range(len(vec)):
retvec.append(num * vec[i])
return retvec
def createGram():
global lens
for i in range(len(trainSet)):
tmp = []
for j in range(0, len(trainSet)):
tmp.append(calInnerProduct(i, j))
Gram.append(tmp)
# update parameters using stochastic gradient descent
def updateParm(k):
global a, b, alpha
a[k] += alpha
b = b + alpha * trainSet[k][1]
#print a, b # you can uncomment this line to check the process of stochastic gradient descent
def calDistance(k):
global a, b
res = 0
for i in range(len(trainSet)):
res += a[i] * int(trainSet[i][1]) * Gram[i][k]
res += b
res *= trainSet[k][1]
return res
def trainModel(Iter):
print "training MLP..."
print "-"*40
epoch = 0
for i in range(Iter):
train_loss = 0
global w, a
update = False
print "epoch",epoch, " w: ",w,"b:",b,
for j in range(len(trainSet)):
res = calDistance(j)
if res <= 0:
train_loss += -res
update = True
updateParm(j)
print 'train loss:',train_loss
trainLoss.append(train_loss)
if update:
epoch = epoch+1
else:
for k in range(len(trainSet)):
w = AddVector(w, NumProduct(a[k] * int(trainSet[k][1]), trainSet[k][0]))
print "result: w: ", w, " b: ", b
update = False
if epoch==Iter:
print 'reach max trian epoch'
for j in range(len(trainSet)):
w = AddVector(w, NumProduct(a[j] * int(trainSet[j][1]), trainSet[j][0]))
print "RESULT: w: ", w, " b: ", b
if __name__=="__main__":
if len(sys.argv)!=4:
print "Usage: python MLP.py trainFile modelFile"
exit(0)
alpha = float(sys.argv[1])
trainFile = open(sys.argv[2])
modelPath = sys.argv[3]
#modelPath = 'model'
lens = 0
# load data trainSet[i][0]:data,trainSet[i][1]:label
for line in trainFile:
data = line.strip().split('\t')
lens = len(data) - 1
sample_all = []
sample_data = []
for i in range(0,lens):
sample_data.append(float(data[i]))
sample_all.append(sample_data) # add data
if int(data[lens]) == 1:
sample_all.append(int(data[lens])) # add label
else:
sample_all.append(-1) # add label
trainSet.append(sample_all)
trainFile.close()
createGram()
for i in range(len(trainSet)):
a.append(0)
for i in range(lens):
w.append(0)
start = time.clock()
trainModel(500)
end = time.clock()
print 'train time is %f s' % (end - start)
x = np.linspace(-5,5,10)
plt.figure()
for i in range(len(trainSet)):
if trainSet[i][1] == 1:
plt.scatter(trainSet[i][0][0],trainSet[i][0][1],c=u'b')
else:
plt.scatter(trainSet[i][0][0],trainSet[i][0][1],c=u'r')
plt.plot(x,-(w[0]*x+b)/w[1],c=u'r')
plt.show()
trainIter = range(len(trainLoss))
plt.figure()
plt.scatter(trainIter,trainLoss,c=u'r')
plt.plot(trainIter,trainLoss)
plt.xlabel('Epoch')
plt.ylabel('trainLoss')
plt.show()
================================================
FILE: MLP/perceptron.py
================================================
# -*- coding: utf-8 -*-
"""
@Description : perceptron by python
@Author: Liu_Longpo
@Time: Sun Dec 20 12:57:00 2015
"""
import matplotlib.pyplot as plt
import numpy as np
import time
trainSet = []
w = []
b = 0
lens = 0
alpha = 0 # learn rate , default 1
trainLoss = []
def updateParm(sample):
global w,b,lens,alpha
for i in range(lens):
w[i] = w[i] + alpha*sample[1]*sample[0][i]
b = b + alpha*sample[1]
def calDistance(sample):
global w,b
res = 0
for i in range(len(sample[0])):
res += sample[0][i] * w[i]
res += b
res *= int(sample[1])
return res
def trainMLP(Iter):
print "training MLP..."
print "-"*40
epoch = 0
for i in range(Iter):
train_loss = 0
update = False
print "epoch",epoch, " w: ",w,"b:",b,
for sample in trainSet:
res = calDistance(sample)
if res <= 0:
train_loss += -res
update = True
updateParm(sample)
print 'train loss:',train_loss
trainLoss.append(train_loss)
if update:
epoch = epoch+1
else:
print "The training have convergenced,stop trianing "
print "Optimum W:",w," Optimum b:",b
#os._exit(0)
break # early stop
update = False
if __name__=="__main__":
if len(sys.argv)!=4:
print "Usage: python MLP.py trainFile modelFile"
exit(0)
alpha = float(sys.argv[1])
trainFile = open(sys.argv[2])
modelPath = sys.argv[3]
#modelPath = 'model'
lens = 0
# load data trainSet[i][0]:data,trainSet[i][1]:label
for line in trainFile:
data = line.strip().split(' ') # train ' ' ,testSet '/t'
lens = len(data) - 1
sample_all = []
sample_data = []
for i in range(0,lens):
sample_data.append(float(data[i]))
sample_all.append(sample_data) # add data
if int(data[lens]) == 1:
sample_all.append(int(data[lens])) # add label
else:
sample_all.append(-1) # add label
trainSet.append(sample_all)
trainFile.close()
# initialize w by 0
for i in range(lens):
w.append(0)
# train model for max 100 Iteration
start = time.clock()
trainMLP(500)
end = time.clock()
print 'train time is %f s.' % (end - start)
x = np.linspace(-5,5,10)
plt.figure()
for i in range(len(trainSet)):
if trainSet[i][1] == 1:
plt.scatter(trainSet[i][0][0],trainSet[i][0][1],c=u'b')
else:
plt.scatter(trainSet[i][0][0],trainSet[i][0][1],c=u'r')
plt.plot(x,-(w[0]*x+b)/w[1],c=u'r')
plt.show()
trainIter = range(len(trainLoss))
plt.figure()
plt.scatter(trainIter,trainLoss,c=u'r')
plt.plot(trainIter,trainLoss)
plt.xlabel('Epoch')
plt.ylabel('trainLoss')
plt.show()
================================================
FILE: MLP/testSet.txt
================================================
-0.017612 14.053064 0
-1.395634 4.662541 1
-0.752157 6.538620 0
-1.322371 7.152853 0
0.423363 11.054677 0
0.406704 7.067335 1
0.667394 12.741452 0
-2.460150 6.866805 1
0.569411 9.548755 0
-0.026632 10.427743 0
0.850433 6.920334 1
1.347183 13.175500 0
1.176813 3.167020 1
-1.781871 9.097953 0
-0.566606 5.749003 1
0.931635 1.589505 1
-0.024205 6.151823 1
-0.036453 2.690988 1
-0.196949 0.444165 1
1.014459 5.754399 1
1.985298 3.230619 1
-1.693453 -0.557540 1
-0.576525 11.778922 0
-0.346811 -1.678730 1
-2.124484 2.672471 1
1.217916 9.597015 0
-0.733928 9.098687 0
-3.642001 -1.618087 1
0.315985 3.523953 1
1.416614 9.619232 0
-0.386323 3.989286 1
0.556921 8.294984 1
1.224863 11.587360 0
-1.347803 -2.406051 1
1.196604 4.951851 1
0.275221 9.543647 0
0.470575 9.332488 0
-1.889567 9.542662 0
-1.527893 12.150579 0
-1.185247 11.309318 0
-0.445678 3.297303 1
1.042222 6.105155 1
-0.618787 10.320986 0
1.152083 0.548467 1
0.828534 2.676045 1
-1.237728 10.549033 0
-0.683565 -2.166125 1
0.229456 5.921938 1
-0.959885 11.555336 0
0.492911 10.993324 0
0.184992 8.721488 0
-0.355715 10.325976 0
-0.397822 8.058397 0
0.824839 13.730343 0
1.507278 5.027866 1
0.099671 6.835839 1
-0.344008 10.717485 0
1.785928 7.718645 1
-0.918801 11.560217 0
-0.364009 4.747300 1
-0.841722 4.119083 1
0.490426 1.960539 1
-0.007194 9.075792 0
0.356107 12.447863 0
0.342578 12.281162 0
-0.810823 -1.466018 1
2.530777 6.476801 1
1.296683 11.607559 0
0.475487 12.040035 0
-0.783277 11.009725 0
0.074798 11.023650 0
-1.337472 0.468339 1
-0.102781 13.763651 0
-0.147324 2.874846 1
0.518389 9.887035 0
1.015399 7.571882 0
-1.658086 -0.027255 1
1.319944 2.171228 1
2.056216 5.019981 1
-0.851633 4.375691 1
-1.510047 6.061992 0
-1.076637 -3.181888 1
1.821096 10.283990 0
3.010150 8.401766 1
-1.099458 1.688274 1
-0.834872 -1.733869 1
-0.846637 3.849075 1
1.400102 12.628781 0
1.752842 5.468166 1
0.078557 0.059736 1
0.089392 -0.715300 1
1.825662 12.693808 0
0.197445 9.744638 0
0.126117 0.922311 1
-0.679797 1.220530 1
0.677983 2.556666 1
0.761349 10.693862 0
-2.168791 0.143632 1
1.388610 9.341997 0
0.317029 14.739025 0
================================================
FILE: PCA/PCA.m
================================================
function [lowData,reconMat] = PCA(data,K)
[row , col] = size(data);
meanValue = mean(data);
%varData = var(data,1,1);
normData = data - repmat(meanValue,[row,1]);
covMat = cov(normData(:,1),normData(:,2));%求取协方差矩阵
[eigVect,eigVal] = eig(covMat);%求取特征值和特征向量
[sortMat, sortIX] = sort(eigVal,'descend');
[B,IX] = sort(sortMat(1,:),'descend');
len = min(K,length(IX));
eigVect(:,IX(1:1:len));
lowData = normData * eigVect(:,IX(1:1:len));
reconMat = (lowData * eigVect(:,IX(1:1:len))') + repmat(meanValue,[row,1]); % 将降维后的数据转换到新空间
end
================================================
FILE: PCA/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/45065609
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: PCA/testPCA.m
================================================
function testPCA
%%
clc
clear
close all
%%
filename = 'testSet.txt';
K = 1;
data = load(filename);
[lowData,reconMat] = PCA(data,K);
figure
scatter(data(:,1),data(:,2),5,'r')
hold on
scatter(reconMat(:,1),reconMat(:,2),5)
hold off
end
================================================
FILE: PCA/testSet.txt
================================================
10.235186 11.321997
10.122339 11.810993
9.190236 8.904943
9.306371 9.847394
8.330131 8.340352
10.152785 10.123532
10.408540 10.821986
9.003615 10.039206
9.534872 10.096991
9.498181 10.825446
9.875271 9.233426
10.362276 9.376892
10.191204 11.250851
7.720499 6.476300
9.334379 8.471268
7.963186 6.731333
8.244973 9.013785
9.569196 10.568949
8.854793 9.076536
9.382171 7.240862
8.179055 8.944502
8.267896 8.797017
9.047165 8.725068
8.741043 7.901385
7.190216 7.804587
8.081227 9.314431
8.047460 5.720780
7.917584 7.543254
8.676942 10.102220
9.210251 9.424717
7.732998 9.840202
7.681754 8.609897
7.925863 10.079159
8.261509 8.242080
8.514872 7.527561
10.324450 10.804481
7.856710 7.931543
7.858608 7.995340
9.196808 6.577598
9.644415 10.935081
9.579833 9.085021
7.888484 5.976428
9.072624 9.703344
8.914184 9.298515
7.822869 7.086663
10.538554 11.061464
8.280187 8.709012
8.884223 8.670105
9.359927 10.575055
9.078611 9.710833
7.935134 8.586173
8.805945 10.575145
9.584316 9.614076
11.269714 11.717254
9.120444 9.019774
7.977520 8.313923
8.104648 9.456128
8.617126 7.331723
9.033811 9.469706
8.327680 5.122092
8.532272 10.100909
9.295434 8.933824
9.905202 9.027559
10.585764 10.912733
10.427584 11.532578
9.072767 9.960144
9.164275 8.645121
9.746058 10.717080
9.286072 9.340024
8.188233 7.432415
7.948598 8.445419
7.563350 5.656178
8.972405 8.801869
9.980868 8.788996
7.753490 7.714248
7.431143 9.032819
8.943403 8.359354
10.481890 9.988969
9.150454 10.278760
8.123894 9.060351
8.626164 8.469342
7.354185 7.631252
11.323046 11.015032
8.190008 6.860792
8.412598 7.661358
9.258404 8.580382
11.007915 11.443881
8.279403 8.347003
8.931149 10.105221
10.239245 10.077473
8.129346 7.096877
8.485823 9.373561
10.703640 11.651618
9.500728 8.150228
9.712414 9.910445
9.333374 9.407557
8.787865 10.168021
9.238180 10.253478
9.577388 8.895150
10.447753 10.318227
9.303944 9.223136
9.883268 11.662945
9.471921 10.443792
10.007753 9.579912
8.110298 7.106263
6.964069 6.585040
10.413499 9.649309
8.032629 7.053254
8.015549 9.166753
10.462924 8.656612
9.530788 10.134130
9.202658 9.314222
10.103241 10.235159
7.849264 6.624856
9.059071 7.992555
10.172889 10.724789
9.528439 6.420990
7.190422 6.789792
9.085716 9.846328
9.452887 8.735386
7.417322 7.348594
8.468639 8.715086
8.303642 9.463231
9.939052 10.026771
8.701989 7.516978
9.737541 10.587281
8.280233 7.852444
10.648386 10.259203
9.173893 10.520372
9.135397 10.751406
7.594580 8.488833
8.587520 8.463406
8.581887 7.888644
9.448768 8.707422
7.882664 7.772030
10.050635 9.859720
9.012078 9.533899
8.770020 8.882996
9.428804 9.446306
8.504209 8.319693
9.800003 10.964667
8.069660 7.683099
10.012217 10.320644
8.704677 8.918146
8.198722 7.297786
9.868322 9.901657
9.426997 11.480353
9.228767 9.262976
8.952359 9.528471
8.186847 8.600587
9.026371 8.705143
9.483364 9.807079
7.826587 7.975401
11.197846 10.959298
7.632421 8.769745
8.761605 8.309365
9.353670 8.728758
6.466637 6.038996
8.370634 9.178830
10.337451 11.075600
8.917679 8.288367
9.076621 8.487626
7.278948 4.634097
10.153017 11.219183
7.132603 5.853118
9.338644 9.805940
9.878602 9.187000
10.009505 10.924505
9.384438 10.691860
7.535322 8.160481
6.808732 8.268469
8.302965 8.075009
8.345379 8.305356
9.517530 8.249839
9.267825 9.999109
10.291511 11.032664
8.605909 8.705207
8.331145 7.812295
8.632412 10.574287
8.766397 8.712107
9.407070 9.732756
9.709495 9.729569
10.422201 11.070360
6.831495 6.466763
8.187122 8.405929
8.523093 9.041844
7.952394 6.801220
10.490780 10.001468
10.813791 9.802494
7.861113 7.541475
8.800399 8.738974
7.542152 6.612838
9.446981 9.378659
8.281684 7.358572
8.473801 8.208343
11.736767 11.022029
8.379578 8.714348
8.313718 8.832381
9.342589 10.416659
7.560710 6.889648
9.295344 9.739040
9.176612 9.718781
8.614385 10.150521
9.079373 8.839794
10.333289 10.921255
9.453502 7.335134
10.174590 10.292500
9.693713 9.793636
7.474925 7.751391
10.107905 10.156997
9.257241 7.854266
10.209794 11.410157
7.248050 6.433676
10.150091 9.288597
10.077713 10.321500
8.191122 8.931519
8.791469 10.287216
9.229434 9.095193
8.682571 8.546005
7.524099 7.709751
8.442410 8.326037
9.364851 9.095989
9.061222 7.557899
7.989999 8.555363
8.801275 8.868732
10.351932 9.497796
10.230710 10.496151
9.783163 9.891408
10.651481 9.431617
8.387393 6.400507
9.003921 7.050003
8.483723 8.314886
9.020501 7.545771
9.329105 11.095661
9.583687 9.271929
8.908705 8.407529
8.835406 8.083517
9.736362 8.296735
10.030302 9.737178
8.287142 6.993460
9.173211 9.306335
9.026355 9.696531
9.128391 9.921247
11.486346 12.910777
11.519458 11.472111
9.027707 10.263974
9.351935 8.542200
9.421701 11.403201
9.005687 8.100969
7.015279 6.614278
8.213607 8.340948
8.226646 8.718997
8.144753 8.366877
10.133642 12.790169
10.763481 10.847016
10.003622 10.337716
9.007955 9.792482
8.670506 10.782931
10.386414 9.956162
10.104761 10.123044
8.079502 8.304075
9.945424 11.855409
8.642497 9.998066
9.349722 8.690328
9.034991 8.826490
8.738746 7.518464
8.919532 9.740312
9.464136 10.444588
10.710057 12.666857
10.042007 10.532091
8.447996 7.426363
9.509351 9.030516
11.946359 10.553075
9.981617 9.912651
9.853876 9.632967
10.560648 11.881714
8.370952 9.989491
8.323209 10.102529
9.828359 11.702462
8.515623 8.426754
9.004363 9.628036
10.529847 10.458031
10.028765 10.624880
9.448114 9.313227
8.332617 7.382295
8.323006 8.276608
7.740771 8.799750
8.379615 8.146192
8.340764 9.184458
9.863614 8.254694
9.969563 9.405134
9.164394 9.182127
10.622098 9.722592
9.592072 10.029446
8.212027 7.477366
9.080225 8.244448
8.555774 7.842325
9.958046 9.696221
8.972573 9.797128
9.213223 7.128437
8.737239 9.385138
10.333907 10.994856
8.797511 8.643075
11.044848 9.623160
8.539260 9.097113
11.582163 11.884333
7.863848 7.176199
6.218103 5.283562
9.120602 7.250190
9.001166 9.635203
8.081476 8.844224
9.369802 8.230911
8.768925 8.666987
9.841098 8.543896
10.451522 9.549511
9.755402 9.117522
7.988961 6.869854
8.872507 9.787118
10.363980 10.716608
6.315671 5.765953
9.638879 9.202355
8.588126 8.037966
8.947408 9.144386
9.051130 7.195132
9.321709 8.380668
10.146531 9.754745
9.843373 8.891437
9.213148 11.700632
7.630078 7.294753
8.093088 7.967590
7.488915 6.090652
8.126036 8.586472
8.760350 7.268987
10.201347 9.141013
7.838208 7.307700
6.155653 5.563997
7.767841 6.254528
8.425656 8.615832
10.362168 10.886815
10.180024 10.378934
9.794665 10.047812
9.970394 9.668279
7.030217 7.060471
9.275414 9.095738
10.314911 10.456539
9.259774 8.204851
10.023919 9.558307
8.887540 9.866704
9.851608 9.410989
8.710882 7.268012
9.017007 10.217673
7.976369 9.000979
8.738332 8.664734
8.344510 8.977600
8.959613 12.324240
9.169982 8.624635
7.487451 8.154859
8.706316 7.719455
9.564832 8.940403
8.327775 9.044509
9.734032 10.195255
8.021343 6.445092
9.081048 11.024397
7.626651 6.549263
10.725858 8.575374
8.731381 8.307788
10.394237 10.596874
7.029311 7.658832
9.517907 7.509904
10.394064 10.060898
10.752500 9.431601
9.692431 10.332130
9.651897 7.876862
8.592329 10.096837
10.212801 10.827496
9.045043 9.265524
8.901643 8.036115
10.794525 9.318830
11.040915 12.021746
8.390836 9.672469
9.840166 11.226568
10.806810 12.205633
8.924285 10.934056
8.411251 8.289672
7.808891 9.663290
9.733437 8.486958
8.300026 7.477374
8.221756 10.278308
9.096867 9.619677
9.410116 9.289188
10.097176 9.768470
9.387954 8.844855
9.376134 7.704630
8.231599 9.101203
9.910738 10.694855
8.645689 7.764589
8.090245 7.109596
9.253483 9.813672
9.331546 8.039386
9.843256 10.208792
9.713131 9.247665
9.259369 10.704622
10.243948 9.695883
6.396262 6.456390
8.936289 8.703871
8.750846 9.347273
6.497155 4.130251
9.516552 10.164848
9.125766 8.858775
8.374387 7.300114
8.132816 7.621107
10.099505 9.159134
9.356477 6.869999
8.112934 7.587547
7.265396 6.987031
11.950505 13.715109
10.745959 10.822171
8.893270 7.887332
6.003473 4.960219
7.498851 6.451334
10.162072 9.935954
8.732617 9.177679
9.300827 9.952360
11.908436 12.256801
9.371215 9.188645
9.943640 9.245037
7.386450 7.046819
8.410374 8.293218
7.830419 6.440253
8.263140 8.279446
11.448164 12.192363
8.216533 9.186628
9.316128 10.046697
8.156927 6.834792
9.951421 11.240598
9.059607 8.458446
10.476339 10.560461
7.548200 7.227127
9.432204 7.236705
9.402750 9.126413
11.188095 13.853426
9.520201 11.028131
8.884154 9.764071
8.961105 8.833117
8.549663 8.865765
10.111708 10.515462
9.024761 9.169368
7.904149 8.048756
9.240995 7.796142
8.126538 6.116125
7.442148 7.931335
9.486821 10.091359
9.834289 11.694720
9.009714 11.599170
9.761314 11.344083
6.993941 6.562988
8.659524 8.410107
7.685363 8.097297
7.793217 6.519109
8.883454 9.257347
8.781821 9.231980
7.946281 7.658978
8.523959 10.646480
9.031525 8.649648
8.317140 7.758978
9.192417 11.151218
8.408486 8.282182
10.327702 11.459048
8.389687 8.548727
8.642250 7.056870
8.833447 9.267638
8.805261 8.320281
9.726211 9.095997
8.477631 9.507530
9.738838 9.652110
8.272108 7.582696
9.258089 8.495931
8.334144 8.810766
8.150904 6.486032
7.259669 7.270156
11.034180 11.519954
10.705432 10.642527
8.388814 7.159137
8.559369 7.846284
7.187988 6.519313
8.811453 7.765900
8.492762 7.992941
8.739752 8.502909
10.150752 10.420295
7.062378 5.365289
8.448195 7.480000
10.224333 11.592750
9.533795 9.212845
9.519492 7.690501
9.661847 10.376189
7.963877 8.597193
10.184486 9.136709
8.505234 9.159210
8.187646 8.518690
9.167590 9.405917
8.612162 8.518755
10.970868 10.392229
9.603649 9.141095
9.704263 8.830178
9.657506 8.132449
9.337882 11.045306
9.521722 9.537764
8.954197 8.728179
8.635658 10.352662
8.910816 9.020317
9.900933 9.392002
10.247105 8.289649
9.571690 8.171237
7.388627 7.668071
8.354008 10.074590
9.775598 8.835696
8.768913 7.983604
8.330199 8.474098
8.169356 9.361172
10.346522 10.086434
7.976144 9.266702
8.429648 7.865824
11.261674 11.788587
10.051066 10.112425
8.954626 9.789343
8.382220 8.121012
9.820642 9.426441
8.125950 9.695087
8.646465 7.291808
8.190202 8.003737
8.773887 7.306175
8.731000 10.300436
9.163098 7.816769
9.456346 9.223922
9.645180 9.324053
8.835060 8.966915
9.325950 10.943248
9.941912 9.548535
9.282799 10.119488
9.567591 9.462164
8.529019 9.768001
9.314824 10.153727
8.264439 8.273860
8.307262 8.214036
9.122041 8.657861
8.404258 8.389365
7.828355 8.419433
9.803180 10.108286
8.662439 8.581953
8.883265 8.978377
8.012330 8.262451
9.420258 8.974878
7.015415 6.365940
9.888832 11.163036
9.677549 10.346431
8.410158 7.912899
9.464147 10.762900
7.067227 7.035717
9.320923 10.583089
9.056917 8.771241
8.110004 8.387789
10.310021 10.970014
8.211185 8.809627
8.942883 8.840746
9.479958 8.328700
8.973982 8.702291
8.519257 8.764855
9.424556 8.956911
7.222919 8.177787
8.257007 9.700619
9.778795 9.296134
8.028806 8.575974
9.886464 9.965076
9.090552 6.978930
9.605548 10.256751
9.959004 9.610229
8.308701 9.509124
7.748293 9.685933
8.311108 9.428114
9.697068 10.217956
9.582991 9.478773
9.167265 10.198412
10.329753 10.406602
8.908819 7.428789
10.072908 10.393294
7.992905 9.226629
8.907696 7.269366
8.421948 9.342968
7.481399 7.225033
10.358408 10.166130
8.786556 10.279943
9.658701 11.379367
10.167807 9.417552
8.653449 8.656681
8.020304 8.671270
8.364348 10.004068
9.119183 9.788199
8.405504 9.740580
11.020930 11.904350
9.755232 9.515713
10.059542 9.589748
8.727131 9.777998
7.666182 6.028642
8.870733 8.367501
9.340446 7.707269
9.919283 10.796813
7.905837 8.326034
10.181187 10.089865
8.797328 8.981988
8.466272 7.765032
10.335914 12.620539
9.365003 8.609115
8.011017 7.249489
10.923993 13.901513
7.074631 7.558720
9.824598 8.851297
8.861026 8.370857
10.127296 10.861535
10.548377 10.855695
8.880470 7.948761
8.901619 9.674705
7.813710 9.246912
10.128808 10.560668
11.096699 10.911644
8.551471 6.871514
8.907241 8.677815
10.571647 10.294838
8.815314 8.810725
8.453396 8.339296
9.594819 11.487580
10.714211 9.628908
7.428788 7.712869
10.892119 12.747752
9.024071 11.112692
7.803375 7.847038
8.521558 8.881848
9.742818 11.520203
9.832836 9.180396
8.703132 10.028498
9.905029 11.347606
10.037536 8.882688
8.629995 8.392863
9.583497 9.219663
8.781687 9.650598
9.344119 9.537024
10.407510 9.223929
7.244488 6.559021
10.643616 10.288383
8.757557 6.947901
10.784590 11.233350
10.028427 11.330033
7.968361 6.830308
8.925954 8.539113
7.738692 7.114987
8.192398 8.352016
10.412017 12.431122
8.208801 5.777678
7.820077 7.790720
9.542754 11.542541
6.817938 7.429229
7.365218 7.956797
9.274391 7.932700
9.546475 8.803412
7.471734 6.797870
8.016969 7.848070
8.852701 8.458114
8.215012 8.468330
6.975507 6.846980
9.435134 10.609700
9.228075 9.342622
8.388410 7.637856
7.111456 9.289163
9.403508 8.482654
9.133894 8.343575
10.670801 9.750821
9.983542 10.074537
10.012865 8.537017
8.929895 8.951909
7.666951 7.473615
9.493839 7.821783
8.894081 7.059413
9.593382 9.859732
9.126847 8.395700
9.532945 9.850696
9.459384 9.384213
8.982743 8.217062
10.107798 8.790772
10.563574 9.044890
8.278963 9.518790
8.734960 10.494129
9.597940 9.530895
10.025478 9.508270
10.335922 10.974063
8.404390 8.146748
7.108699 6.038469
8.873951 7.474227
8.731459 8.154455
8.795146 7.534687
6.407165 6.810352
9.979312 10.287430
8.786715 8.396736
10.753339 10.360567
10.508031 10.321976
10.636925 10.193797
10.614322 11.215420
8.916411 8.965286
8.112756 8.304769
10.833109 10.497542
8.319758 9.727691
9.945336 11.820097
10.150461 9.914715
10.185024 10.388722
9.793569 9.079955
10.590128 11.811596
8.505584 6.884282
10.461428 10.745439
8.755781 9.418427
7.488249 7.172072
10.238905 10.428659
9.887827 10.427821
8.529971 8.838217
8.375208 10.242837
8.901724 8.398304
8.607694 9.173198
8.691369 9.964261
9.584578 9.641546
10.265792 11.405078
7.592968 6.683355
8.692791 9.389031
7.589852 6.005793
10.550386 11.736584
8.578351 7.227055
7.526931 6.875134
8.577081 9.877115
9.272136 11.050928
10.300809 10.653059
8.642013 9.006681
9.720491 10.265202
9.029005 9.646928
8.736201 7.975603
8.672886 9.070759
8.370633 8.412170
9.483776 9.183341
6.790842 7.594992
9.842146 10.156810
9.563336 7.962532
8.724669 9.870732
9.012145 9.171326
9.116948 9.791167
6.219094 7.988420
9.468422 8.359975
8.825231 8.475208
9.572224 9.696428
9.609128 8.488175
9.428590 10.468998
8.293266 8.617701
9.423584 10.355688
9.240796 9.517228
10.915423 13.026252
10.854684 11.130866
9.226816 9.391796
9.580264 10.359235
7.289907 6.898208
9.338857 10.374025
9.523176 11.332190
10.162233 10.357396
8.873930 9.207398
8.607259 7.794804
8.852325 8.215797
8.077272 6.501042
8.169273 8.269613
6.806421 7.544423
8.793151 9.691549
11.640981 11.365702
9.544082 11.576545
9.009266 9.605596
9.726552 9.426719
9.495888 10.626624
8.683982 9.337864
8.322105 8.631099
8.887895 8.644931
8.662659 11.373025
9.263321 7.536016
7.802624 7.171625
8.773183 8.561565
8.730443 10.197596
8.942915 7.758383
8.057618 8.774996
8.112081 8.202349
10.378884 12.103755
9.248876 8.637249
9.739599 9.708576
8.126345 8.278487
8.894788 7.966117
9.683165 9.019221
10.886957 12.053843
9.668852 10.902132
7.486692 6.471138
8.794850 9.173609
8.835915 8.296727
9.443984 11.375344
8.696621 6.434580
9.645560 9.233722
9.623857 7.915590
10.840632 12.620268
7.298135 7.356141
9.639644 8.902389
9.849802 7.682624
10.609964 10.259615
9.768229 11.382811
7.646351 7.571849
10.230300 9.470859
8.224402 8.496866
6.879671 8.393648
7.976247 8.667221
9.183268 8.694550
11.471853 12.786280
10.428349 10.615726
8.090828 5.902504
9.738627 8.485792
8.139709 8.396333
9.508055 8.990529
8.857260 8.497732
8.902558 7.014433
9.660607 11.040833
8.772221 10.512150
11.020038 9.354134
7.918527 7.742062
7.630835 7.756260
11.043272 11.041613
9.299376 8.674157
9.795087 8.431837
9.415683 8.312101
7.942037 6.942913
9.724790 11.766496
10.222032 11.550876
8.894163 8.306020
8.394309 8.070420
9.012776 6.880548
9.661093 10.138921
9.896472 9.762372
9.135628 8.759928
8.762656 10.306028
8.602473 8.861956
10.085297 10.464774
10.644983 10.945767
9.034571 8.391668
8.602920 8.501944
8.224766 7.402758
8.755050 9.431085
9.669937 8.641049
10.693530 10.287124
9.462806 7.611153
9.287707 10.082363
10.941260 10.783728
9.263080 7.913328
10.167111 10.225338
8.783830 9.465345
8.958624 8.662136
9.841649 9.926781
7.205691 6.790638
8.629089 9.135461
7.469440 8.450442
8.179133 7.790434
8.083984 7.875520
9.271300 8.135359
8.652349 8.254397
7.983920 6.609684
7.836860 9.785238
7.418535 7.011256
8.458288 10.095364
9.387605 9.726911
8.663951 8.206705
10.146507 11.698577
8.937103 10.990924
11.218687 11.141945
8.363142 9.106936
7.877643 7.122922
9.620978 9.905689
9.509649 10.773209
6.748743 6.705385
9.300919 8.085029
9.332257 9.818791
7.898610 8.366643
9.841914 9.480675
6.920484 8.959501
8.544713 9.563136
8.162266 6.715277
8.659552 9.282008
10.673398 13.174824
9.024000 10.379238
8.183292 6.647572
10.544919 10.649602
7.201266 6.529605
9.557407 11.096821
8.304605 6.940929
9.742855 9.920897
10.024587 9.645222
10.002296 9.998940
8.965876 8.665419
7.823136 6.949572
8.125088 7.654065
6.569589 6.046863
10.195497 8.689129
11.730011 10.374221
8.739105 7.457571
9.820059 10.278526
9.547456 10.398198
8.375072 8.416302
8.889533 8.308929
8.861201 9.290408
12.677687 12.788463
9.100735 8.620537
7.728350 6.328219
7.955373 8.355028
8.733352 8.645414
10.257527 11.191813
9.246413 9.497014
9.745302 9.642035
7.785652 8.147621
7.431673 8.566399
8.654384 8.466701
8.475392 6.744677
9.968440 10.765192
10.163616 10.806963
10.238135 10.036636
9.902889 10.746730
9.523850 8.749708
9.214363 9.149178
9.266040 10.841502
8.494292 7.770942
10.821158 10.410192
8.645888 7.970308
9.885204 10.098080
9.084990 10.886349
9.277874 8.871449
8.135131 7.137064
7.917379 9.080522
9.685586 8.822850
8.558141 7.848112
9.502917 10.061255
6.409004 5.164774
10.149235 10.579951
7.847304 8.411351
8.846930 6.819939
8.675153 9.411147
9.476276 9.061508
11.099184 10.644263
8.792411 10.379405
8.400418 7.072706
8.555713 7.923805
8.024763 8.426993
8.642696 10.453412
7.906117 7.920408
8.793393 9.722878
8.280364 7.669854
9.387766 9.706245
9.626853 10.762499
10.163631 10.919007
9.375543 11.513524
9.309440 8.575699
10.055329 10.297255
8.706241 9.097172
10.032934 11.951897
10.812974 11.311435
10.352603 10.819865
8.276870 9.055403
8.397389 7.944434
9.371741 10.395790
10.825710 10.144099
9.158483 11.385382
10.658639 11.389856
8.091762 6.631039
10.734892 10.054598
11.535880 11.604912
9.799077 11.371677
8.478725 9.078455
9.399902 8.947744
7.305377 8.144973
7.613377 6.668798
10.681308 10.830845
9.973855 10.004133
9.369918 7.855433
8.838223 7.429033
9.521831 10.623930
9.724419 10.447452
8.890224 9.275923
9.932763 11.589953
10.839337 9.051250
8.497708 7.521701
8.440236 8.705670
9.063566 9.755744
8.449647 8.929485
8.554576 8.063231
10.348606 10.550718
5.985254 5.186844
9.931937 10.175582
9.854922 9.201393
9.114580 9.134215
10.334899 8.543604
================================================
FILE: README.md
================================================
该分支主要存放的是个人的MachineLearning 以及DeepLearning代码以及数据
后续将添加 Caffe model.
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: bikMeans/README.md
================================================
博客链接:http://blog.csdn.net/llp1992/article/details/45096063
更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)
================================================
FILE: bikMeans/bikMeans.m
================================================
function bikMeans
%%
clc
clear
close all
%%
biK = 4;
biDataSet = load('testSet.txt');
[row,col] = size(biDataSet);
% 存储质心矩阵
biCentSet = zeros(biK,col);
% 初始化设定cluster数量为1
numCluster = 1;
%第一列存储每个点被分配的质心,第二列存储点到质心的距离
biClusterAssume = zeros(row,2);
%初始化质心
biCentSet(1,:) = mean(biDataSet);
for i = 1:row
biClusterAssume(i,1) = numCluster;
biClusterAssume(i,2) = distEclud(biDataSet(i,:),biCentSet(1,:));
end
while numCluster < biK
minSSE = 10000;
%寻找对哪个cluster进行划分最好,也就是寻找SSE最小的那个cluster
for j = 1:numCluster
curCluster = biDataSet(find(biClusterAssume(:,1) == j),:);
[spiltCentSet,spiltClusterAssume] = kMeans(curCluster,2);
spiltSSE = sum(spiltClusterAssume(:,2));
noSpiltSSE = sum(biClusterAssume(find(biClusterAssume(:,1)~=j),2));
curSSE = spiltSSE + noSpiltSSE;
fprintf('第%d个cluster被划分后的误差为:%f \n' , [j, curSSE])
if (curSSE < minSSE)
minSSE = curSSE;
bestClusterToSpilt = j;
bestClusterAssume = spiltClusterAssume;
bestCentSet = spiltCentSet;
end
end
%更新cluster的数目
numCluster = numCluster + 1;
% 必须先更新2的为新的类,再更新1的为原来的类,不然的话,当对第二个cluster进行划分的时候,就会被全部分为同一个类
bestClusterAssume(find(bestClusterAssume(:,1) == 2),1) = numCluster;
bestClusterAssume(find(bestClusterAssume(:,1) == 1),1) = bestClusterToSpilt;
% 更新和添加质心坐标 第一行为更新质心,第二行为添加质心
biCentSet(bestClusterToSpilt,:) = bestCentSet(1,:);
biCentSet(numCluster,:) = bestCentSet(2,:);
% 更新被划分的cluster的每个点的质心分配以及误差
biClusterAssume(find(biClusterAssume(:,1) == bestClusterToSpilt),:) = bestClusterAssume;
end
figure
%scatter(dataSet(:,1),dataSet(:,2),5)
for i = 1:biK
pointCluster = find(biClusterAssume(:,1) == i);
scatter(biDataSet(pointCluster,1),biDataSet(pointCluster,2),5)
hold on
end
%hold on
scatter(biCentSet(:,1),biCentSet(:,2),300,'+')
hold off
biCentSet
end
% 计算欧式距离
function dist = distEclud(vecA,vecB)
dist = sum(power((vecA-vecB),2));
end
% K-means算法
function [centSet,clusterAssment] = kMeans(dataSet,K)
[row,col] = size(dataSet);
% 存储质心矩阵
centSet = zeros(K,col);
% 随机初始化质心
for i= 1:col
minV = min(dataSet(:,i));
if isempty(minV)
break;
end
rangV = max(dataSet(:,i)) - minV;
centSet(:,i) = bsxfun(@plus,minV,rangV*rand(K,1));
end
% 用于存储每个点被分配的cluster以及到质心的距离
clusterAssment = zeros(row,2);
clusterChange = true;
while clusterChange
clusterChange = false;
% 计算每个点应该被分配的cluster
for i = 1:row
% 这部分可能可以优化
minDist = 10000;
minIndex = 0;
for j = 1:K
distCal = distEclud(dataSet(i,:) , centSet(j,:));
if (distCal < minDist)
minDist = distCal;
minIndex = j;
end
end
if minIndex ~= clusterAssment(i,1)
clusterChange = true;
end
clusterAssment(i,1) = minIndex;
clusterAssment(i,2) = minDist;
end
% 更新每个cluster 的质心
for j = 1:K
simpleCluster = find(clusterAssment(:,1) == j);
centSet(j,:) = mean(dataSet(simpleCluster',:));
end
end
end
================================================
FILE: bikMeans/testSet.txt
================================================
1.658985 4.285136
-3.453687 3.424321
4.838138 -1.151539
-5.379713 -3.362104
0.972564 2.924086
-3.567919 1.531611
0.450614 -3.302219
-3.487105 -1.724432
2.668759 1.594842
-3.156485 3.191137
3.165506 -3.999838
-2.786837 -3.099354
4.208187 2.984927
-2.123337 2.943366
0.704199 -0.479481
-0.392370 -3.963704
2.831667 1.574018
-0.790153 3.343144
2.943496 -3.357075
-3.195883 -2.283926
2.336445 2.875106
-1.786345 2.554248
2.190101 -1.906020
-3.403367 -2.778288
1.778124 3.880832
-1.688346 2.230267
2.592976 -2.054368
-4.007257 -3.207066
2.257734 3.387564
-2.679011 0.785119
0.939512 -4.023563
-3.674424 -2.261084
2.046259 2.735279
-3.189470 1.780269
4.372646 -0.822248
-2.579316 -3.497576
1.889034 5.190400
-0.798747 2.185588
2.836520 -2.658556
-3.837877 -3.253815
2.096701 3.886007
-2.709034 2.923887
3.367037 -3.184789
-2.121479 -4.232586
2.329546 3.179764
-3.284816 3.273099
3.091414 -3.815232
-3.762093 -2.432191
3.542056 2.778832
-1.736822 4.241041
2.127073 -2.983680
-4.323818 -3.938116
3.792121 5.135768
-4.786473 3.358547
2.624081 -3.260715
-4.009299 -2.978115
2.493525 1.963710
-2.513661 2.642162
1.864375 -3.176309
-3.171184 -3.572452
2.894220 2.489128
-2.562539 2.884438
3.491078 -3.947487
-2.565729 -2.012114
3.332948 3.983102
-1.616805 3.573188
2.280615 -2.559444
-2.651229 -3.103198
2.321395 3.154987
-1.685703 2.939697
3.031012 -3.620252
-4.599622 -2.185829
4.196223 1.126677
-2.133863 3.093686
4.668892 -2.562705
-2.793241 -2.149706
2.884105 3.043438
-2.967647 2.848696
4.479332 -1.764772
-4.905566 -2.911070
================================================
FILE: kalmanFilter/KF.m
================================================
function [predData,dataX] = KF(dataZ)
%%
%
% Description : kalmanFiltering
% Author : Liulongpo
% Time:2015-4-29 16:42:34
%
%%
Z = dataZ';
len = length(Z);
%Z=(1:2:200); %观测值 汽车的位置 也就是我们要修改的量
noise=randn(1,len); %方差为1的高斯噪声
dataX = zeros(2,len);
Z=Z+noise;
X=[Z(1) ; Z(2)-Z(1) ]; %初始状态 分别为 位置 和速度
P=[1 0;0 1]; %状态协方差矩阵
F=[1 1;0 1]; %状态转移矩阵
Q=[0.0001,0;0 , 0.0001]; %状态转移协方差矩阵
H=[1,0]; %观测矩阵
R=1; %观测噪声协方差矩阵
%figure;
%hold on;
for i = 1:len
%基于上一状态预测当前状态
% 2x1 2x1
X_ = F*X;
% 更新协方差 Q系统过程的协方差 这两个公式是对系统的预测
% 2x1 2x1 1x2 2x2
P_ = F*P*F'+Q;
% 计算卡尔曼增益
K = P_*H'/(H*P_*H'+R);
% 得到当前状态的最优化估算值 增益乘以残差
X = X_+K*(Z(i)-H*X_);
%更新K状态的协方差
P = (eye(2)-K*H)*P_;
dataX(:,i) = [X(1);X(2)];
%scatter(X(1), X(2),4); %画点,横轴表示位置,纵轴表示速度
end
predData = F*X;
end
================================================
FILE: kalmanFilter/kalmanFiltering.m
================================================
function kalmanFiltering
%%
clc
close all
%%
%
% Description : kalmanFiltering
% Author : Liulongpo
% Time:2015-4-29 16:42:34
%
%%
Z=(1:2:200); %观测值 汽车的位置 也就是我们要修改的量
noise=randn(1,100); %方差为1的高斯噪声
Z=Z+noise;
X=[0 ; 0 ]; %初始状态
P=[1 0;0 1]; %状态协方差矩阵
F=[1 1;0 1]; %状态转移矩阵
Q=[0.0001,0;0 , 0.0001]; %状态转移协方差矩阵
H=[1,0]; %观测矩阵
R=1; %观测噪声协方差矩阵
figure;
hold on;
for i = 1:100
%基于上一状态预测当前状态
X_ = F*X;
% 更新协方差 Q系统过程的协方差 这两个公式是对系统的预测
P_ = F*P*F'+Q;
% 计算卡尔曼增益
K = P_*H'/(H*P_*H'+R);
% 得到当前状态的最优化估算值 增益乘以残差
X = X_+K*(Z(i)-H*X_);
%更新K状态的协方差
P = (eye(2)-K*H)*P_;
scatter(X(1), X(2),4); %画点,横轴表示位置,纵轴表示速度
end
end
gitextract_cc07p_s4/
├── Adaboost/
│ ├── README.md
│ ├── adaboost.py
│ └── testAdaboost.py
├── Decision-Tree/
│ ├── README.md
│ ├── TestTree.py
│ └── Tree.py
├── DeepLearning/
│ ├── CNN_cifar-10/
│ │ └── cifar.py
│ ├── CNN_mnist/
│ │ ├── cnn.py
│ │ ├── data.py
│ │ └── trainCNN.py
│ └── UFLDL/
│ ├── Vectorization_sparseae_exercise/
│ │ ├── checkNumericalGradient.m
│ │ ├── computeNumericalGradient.m
│ │ ├── display_network.m
│ │ └── initializeParameters.m
│ └── stl_exercise/
│ ├── display_network.m
│ ├── feedForwardAutoencoder.m
│ ├── initializeParameters.m
│ ├── loadMNISTImages.m
│ ├── loadMNISTLabels.m
│ ├── softmaxCost.m
│ ├── softmaxPredict.m
│ ├── softmaxTrain.m
│ ├── sparseAutoencoderCost.m
│ └── stlExercise.m
├── GMM/
│ ├── README.md
│ ├── gmm.m
│ ├── gmm.py
│ ├── testGMM.m
│ └── testSet.txt
├── KNN/
│ ├── KNN.m
│ ├── KNN.py
│ ├── KNNdatgingTest.m
│ ├── README.md
│ ├── datingTestSet2.txt
│ └── handWritingTest.m
├── Kmeans/
│ ├── README.md
│ ├── distEclud.m
│ ├── kMeans.m
│ ├── testSet.txt
│ └── testkMeans.m
├── Logistic-regression/
│ ├── ImproveStocGradAscent.m
│ ├── README.md
│ ├── gradAscent.m
│ ├── stocGradAscent.m
│ └── testSet.txt
├── MLP/
│ ├── dualperceptron.py
│ ├── perceptron.py
│ └── testSet.txt
├── PCA/
│ ├── PCA.m
│ ├── README.md
│ ├── testPCA.m
│ └── testSet.txt
├── README.md
├── bikMeans/
│ ├── README.md
│ ├── bikMeans.m
│ └── testSet.txt
└── kalmanFilter/
├── KF.m
└── kalmanFiltering.m
SYMBOL INDEX (30 symbols across 9 files) FILE: Adaboost/adaboost.py function loadSimpData (line 9) | def loadSimpData(): function stumpClassify (line 20) | def stumpClassify(dataMatrix,dimen,threshVal,threshIneq): function buildStump (line 30) | def buildStump(dataArr,classLabels,D): function adaBoostTrainDS (line 68) | def adaBoostTrainDS(dataArr,classLabels,numIt = 40): function adaClassify (line 98) | def adaClassify(datToClass,classifierArr): FILE: Decision-Tree/Tree.py function calcShannonEnt (line 3) | def calcShannonEnt(dataSet): function createDataSet (line 16) | def createDataSet(): function spiltDataSet (line 26) | def spiltDataSet(dataSet,axis,value): function chooseBestFeatureToSplit (line 39) | def chooseBestFeatureToSplit(dataSet): function majorityCnt (line 70) | def majorityCnt(classList): function createTree (line 78) | def createTree(dataSet,labels): FILE: DeepLearning/CNN_mnist/cnn.py function funcnn (line 24) | def funcnn(LR,BS): FILE: DeepLearning/CNN_mnist/data.py function load_data (line 6) | def load_data(): FILE: DeepLearning/CNN_mnist/trainCNN.py function floatrange (line 12) | def floatrange(start,stop,steps): FILE: GMM/gmm.py function gmm (line 18) | def gmm(X,K): function inti_params (line 46) | def inti_params(centroids,K,X,N,D): function calc_prop (line 62) | def calc_prop(X,N,K,pMiu,pSigma,threshold,D): function test (line 73) | def test(): FILE: KNN/KNN.py function createDataSet (line 4) | def createDataSet(): function classify0 (line 10) | def classify0(inx,dataSet,labels,k): FILE: MLP/dualperceptron.py function calInnerProduct (line 22) | def calInnerProduct(i, j): function AddVector (line 29) | def AddVector(vec1, vec2): function NumProduct (line 35) | def NumProduct(num, vec): function createGram (line 41) | def createGram(): function updateParm (line 50) | def updateParm(k): function calDistance (line 56) | def calDistance(k): function trainModel (line 65) | def trainModel(Iter): FILE: MLP/perceptron.py function updateParm (line 19) | def updateParm(sample): function calDistance (line 25) | def calDistance(sample): function trainMLP (line 34) | def trainMLP(Iter):
Condensed preview — 58 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (141K chars).
[
{
"path": "Adaboost/README.md",
"chars": 80,
"preview": "Adaboost 算法实现。\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)\n"
},
{
"path": "Adaboost/adaboost.py",
"chars": 3920,
"preview": "# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Thu Jun 11 12:42:48 2015\n\n@author: liu\n\"\"\"\nfrom numpy import *\n\ndef loadSimpData("
},
{
"path": "Adaboost/testAdaboost.py",
"chars": 1315,
"preview": "# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Thu Jun 11 12:57:27 2015\n\n@author: LiuLongpo\n\"\"\"\nimport optunity\nimport adaboost\n"
},
{
"path": "Decision-Tree/README.md",
"chars": 74,
"preview": "决策树算法实现。\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp1992)\n"
},
{
"path": "Decision-Tree/TestTree.py",
"chars": 109,
"preview": "import Tree\n\ndataSet,label = Tree.createDataSet()\nprint 'dataSet:' , dataSet\n#Tree.createTree(dataSet,label)\n"
},
{
"path": "Decision-Tree/Tree.py",
"chars": 3594,
"preview": "from math import log\n# 计算熵\ndef calcShannonEnt(dataSet):\n numEntries = len(dataSet)\n labelCounts = {}\n for featV"
},
{
"path": "DeepLearning/CNN_cifar-10/cifar.py",
"chars": 2517,
"preview": "\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Thu Aug 27 11:27:34 2015\n\n@author: lab-liu.longpo\n\"\"\"\n\n\nfrom __future__ import a"
},
{
"path": "DeepLearning/CNN_mnist/cnn.py",
"chars": 2724,
"preview": "# -*- coding: utf-8 -*-\n\nfrom __future__ import absolute_import\nfrom __future__ import print_function\nfrom keras.preproc"
},
{
"path": "DeepLearning/CNN_mnist/data.py",
"chars": 492,
"preview": "# -*- coding: utf-8 -*-\nimport os\nfrom PIL import Image\nimport numpy as np\n\ndef load_data():\n print 'start loading da"
},
{
"path": "DeepLearning/CNN_mnist/trainCNN.py",
"chars": 608,
"preview": "# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Tue Aug 25 10:59:38 2015\n\n@author: lab-liu.longpo\n\"\"\"\n\nfrom cnn import funcnn\nimp"
},
{
"path": "DeepLearning/UFLDL/Vectorization_sparseae_exercise/checkNumericalGradient.m",
"chars": 2002,
"preview": "function [] = checkNumericalGradient()\n% This code can be used to check your numerical gradient implementation \n% in com"
},
{
"path": "DeepLearning/UFLDL/Vectorization_sparseae_exercise/computeNumericalGradient.m",
"chars": 1178,
"preview": "function numgrad = computeNumericalGradient(J, theta)\n% numgrad = computeNumericalGradient(J, theta)\n% theta: a vector o"
},
{
"path": "DeepLearning/UFLDL/Vectorization_sparseae_exercise/display_network.m",
"chars": 2647,
"preview": "function [h, array] = display_network(A, opt_normalize, opt_graycolor, cols, opt_colmajor)\n% This function visualizes fi"
},
{
"path": "DeepLearning/UFLDL/Vectorization_sparseae_exercise/initializeParameters.m",
"chars": 622,
"preview": "function theta = initializeParameters(hiddenSize, visibleSize)\n\n%% Initialize parameters randomly based on layer sizes.\n"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/display_network.m",
"chars": 2647,
"preview": "function [h, array] = display_network(A, opt_normalize, opt_graycolor, cols, opt_colmajor)\n% This function visualizes fi"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/feedForwardAutoencoder.m",
"chars": 1305,
"preview": "function [activation] = feedForwardAutoencoder(theta, hiddenSize, visibleSize, data)\n\n% theta: trained weights from the "
},
{
"path": "DeepLearning/UFLDL/stl_exercise/initializeParameters.m",
"chars": 622,
"preview": "function theta = initializeParameters(hiddenSize, visibleSize)\n\n%% Initialize parameters randomly based on layer sizes.\n"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/loadMNISTImages.m",
"chars": 811,
"preview": "function images = loadMNISTImages(filename)\n%loadMNISTImages returns a 28x28x[number of MNIST images] matrix containing\n"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/loadMNISTLabels.m",
"chars": 516,
"preview": "function labels = loadMNISTLabels(filename)\n%loadMNISTLabels returns a [number of MNIST images]x1 matrix containing\n%the"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/softmaxCost.m",
"chars": 1491,
"preview": "function [cost, grad] = softmaxCost(theta, numClasses, inputSize, lambda, data, labels)\n\n% numClasses - the number of cl"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/softmaxPredict.m",
"chars": 743,
"preview": "function [pred] = softmaxPredict(softmaxModel, data)\n\n% softmaxModel - model trained using softmaxTrain\n% data - the N x"
},
{
"path": "DeepLearning/UFLDL/stl_exercise/softmaxTrain.m",
"chars": 1891,
"preview": "function [softmaxModel] = softmaxTrain(inputSize, numClasses, lambda, inputData, labels, options)\n%softmaxTrain Train a "
},
{
"path": "DeepLearning/UFLDL/stl_exercise/sparseAutoencoderCost.m",
"chars": 4211,
"preview": "function [cost,grad] = sparseAutoencoderCost(theta, visibleSize, hiddenSize, ...\n "
},
{
"path": "DeepLearning/UFLDL/stl_exercise/stlExercise.m",
"chars": 5243,
"preview": "%% CS294A/CS294W Self-taught Learning Exercise\n\n% Instructions\n% ------------\n% \n% This file contains code that helps"
},
{
"path": "GMM/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/47058109\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "GMM/gmm.m",
"chars": 2817,
"preview": "function varargout = gmm(X, K_or_centroids)\n% ============================================================\n% Expectation"
},
{
"path": "GMM/gmm.py",
"chars": 2629,
"preview": "# -*- coding: utf-8 -*-\n\n'''\n Description :GMM in Python\n Author : LiuLongpo\n Time : 2015年7月26日16:54:48\n Source :From pl"
},
{
"path": "GMM/testGMM.m",
"chars": 545,
"preview": "clear all\nclc\n\ndata = load('testSet.txt');\n[PX,Model] = GMM(data,4);\n[~,index] = max(PX');\ncent = Model.Miu;\nfigure\nI = "
},
{
"path": "GMM/testSet.txt",
"chars": 1520,
"preview": "1.658985\t4.285136\n-3.453687\t3.424321\n4.838138\t-1.151539\n-5.379713\t-3.362104\n0.972564\t2.924086\n-3.567919\t1.531611\n0.45061"
},
{
"path": "KNN/KNN.m",
"chars": 316,
"preview": "function relustLabel = KNN(inx,data,labels,k)\n%% \n% inx 为 输入测试数据,data为样本数据,labels为样本标签\n%%\n\n[datarow , datacol] = size("
},
{
"path": "KNN/KNN.py",
"chars": 1130,
"preview": "from numpy import *\nimport operator\n\ndef createDataSet():\n group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])\n lab"
},
{
"path": "KNN/KNNdatgingTest.m",
"chars": 649,
"preview": "function KNNdatgingTest\n%%\nclc\nclear\nclose all\n%%\ndata = load('datingTestSet2.txt');\ndataMat = data(:,1:3);\nlabels = dat"
},
{
"path": "KNN/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/45040685\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "KNN/datingTestSet2.txt",
"chars": 26067,
"preview": "40920\t8.326976\t0.953952\t3\n14488\t7.153469\t1.673904\t2\n26052\t1.441871\t0.805124\t1\n75136\t13.147394\t0.428964\t1\n38344\t1.669788\t"
},
{
"path": "KNN/handWritingTest.m",
"chars": 1473,
"preview": "function handWritingTest\n%%\nclc\nclear\nclose all\n%% 获取目录下的所有txt文件名称\nd = dir(['digits/trainingDigits/' '*.txt']); % struct"
},
{
"path": "Kmeans/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/45095935\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "Kmeans/distEclud.m",
"chars": 163,
"preview": "% 计算欧式距离\nfunction dist = distEclud(vecA,vecB)\n dist = sum(power((vecA-vecB),2));\nend\n\n\nfunction dist = softDist(vecA"
},
{
"path": "Kmeans/kMeans.m",
"chars": 1142,
"preview": "% K-means算法\nfunction [centSet,clusterAssment] = kMeans(dataSet,K)\n\n[row,col] = size(dataSet);\n% 存储质心矩阵\ncentSet = zeros(K"
},
{
"path": "Kmeans/testSet.txt",
"chars": 1520,
"preview": "1.658985\t4.285136\n-3.453687\t3.424321\n4.838138\t-1.151539\n-5.379713\t-3.362104\n0.972564\t2.924086\n-3.567919\t1.531611\n0.45061"
},
{
"path": "Kmeans/testkMeans.m",
"chars": 1498,
"preview": "function testkMeans\nclc\nclear all\nclose all\nK = 4;\ndataSet = load('testSet.txt');\nfigure \nscatter(dataSet(:,1),dataSet(:"
},
{
"path": "Logistic-regression/ImproveStocGradAscent.m",
"chars": 1548,
"preview": "function ImproveStocGradAscent\n%%\n%\n% Description : LogisticRegression using stocGradAsscent\n% Author : Liulongpo\n% "
},
{
"path": "Logistic-regression/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/45114421\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "Logistic-regression/gradAscent.m",
"chars": 865,
"preview": "function weight = gradAscent\n%%\n%\n% Description : LogisticRegression using gradAsscent\n% Author : Liulongpo\n% Time"
},
{
"path": "Logistic-regression/stocGradAscent.m",
"chars": 840,
"preview": "function stocGradAscent\n%%\n%\n% Description : LogisticRegression using stocGradAsscent\n% Author : Liulongpo\n% Time:"
},
{
"path": "Logistic-regression/testSet.txt",
"chars": 2087,
"preview": "-0.017612\t14.053064\t0\n-1.395634\t4.662541\t1\n-0.752157\t6.538620\t0\n-1.322371\t7.152853\t0\n0.423363\t11.054677\t0\n0.406704\t7.067"
},
{
"path": "MLP/dualperceptron.py",
"chars": 3900,
"preview": "# -*- coding: utf-8 -*-\n''' \n @Description : dualperceptron by python\n @Author: Liu_Longpo\n @Time: Sun Dec 2"
},
{
"path": "MLP/perceptron.py",
"chars": 2947,
"preview": "# -*- coding: utf-8 -*-\n\"\"\"\n @Description : perceptron by python\n @Author: Liu_Longpo\n @Time: Sun Dec 20 12:57:"
},
{
"path": "MLP/testSet.txt",
"chars": 2087,
"preview": "-0.017612\t14.053064\t0\n-1.395634\t4.662541\t1\n-0.752157\t6.538620\t0\n-1.322371\t7.152853\t0\n0.423363\t11.054677\t0\n0.406704\t7.067"
},
{
"path": "PCA/PCA.m",
"chars": 534,
"preview": "function [lowData,reconMat] = PCA(data,K)\n\n[row , col] = size(data);\nmeanValue = mean(data);\n%varData = var(data,1,1);\nn"
},
{
"path": "PCA/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/45065609\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "PCA/testPCA.m",
"chars": 237,
"preview": "function testPCA\n%%\nclc\nclear\nclose all\n%%\n\nfilename = 'testSet.txt';\nK = 1;\ndata = load(filename);\n[lowData,reconMat] ="
},
{
"path": "PCA/testSet.txt",
"chars": 18472,
"preview": "10.235186\t11.321997\n10.122339\t11.810993\n9.190236\t8.904943\n9.306371\t9.847394\n8.330131\t8.340352\n10.152785\t10.123532\n10.408"
},
{
"path": "README.md",
"chars": 134,
"preview": "该分支主要存放的是个人的MachineLearning 以及DeepLearning代码以及数据\n\n后续将添加 Caffe model.\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn"
},
{
"path": "bikMeans/README.md",
"chars": 124,
"preview": "博客链接:http://blog.csdn.net/llp1992/article/details/45096063\n\n更多机器学习深度学习博客请关注CSDN博客:[LiuLongpo](http://blog.csdn.net/llp19"
},
{
"path": "bikMeans/bikMeans.m",
"chars": 3137,
"preview": "function bikMeans\n%%\nclc\nclear\nclose all\n%%\nbiK = 4;\nbiDataSet = load('testSet.txt');\n[row,col] = size(biDataSet);\n% 存储质"
},
{
"path": "bikMeans/testSet.txt",
"chars": 1520,
"preview": "1.658985\t4.285136\n-3.453687\t3.424321\n4.838138\t-1.151539\n-5.379713\t-3.362104\n0.972564\t2.924086\n-3.567919\t1.531611\n0.45061"
},
{
"path": "kalmanFilter/KF.m",
"chars": 760,
"preview": "function [predData,dataX] = KF(dataZ)\n\n%%\n%\n% Description : kalmanFiltering\n% Author : Liulongpo\n% Time:2015-4-29 "
},
{
"path": "kalmanFilter/kalmanFiltering.m",
"chars": 612,
"preview": "function kalmanFiltering\n%%\nclc\nclose all\n\n%%\n%\n% Description : kalmanFiltering\n% Author : Liulongpo\n% Time:2015-4"
}
]
About this extraction
This page contains the full source code of the llp1992/MachineLearning GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 58 files (120.4 KB), approximately 54.1k tokens, and a symbol index with 30 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.