# -*- coding: utf-8 -*- # Filename : dataSet.py import numpy as np import operator import Untils import Kohonen from numpy import * import matplotlib.pyplot as plt # 加载坐标数据文件 dataSet = Untils.loadDataSet("dataset.txt") dataMat = mat(dataSet) # print dataMat normDataset = Kohonen.mapMinMax(dataMat) # print normDataset # 生成int随机数,不包含高值 # print random.randint(0,30) # 计算向量中最小值的索引值 xx = mat([1, 9]) w1 = mat([[1, 2, 3, 4], [5, 6, 7, 8]]) minIndx = Kohonen.distM(xx, w1).argmin() # 计算距离 jdpx = mat([[0, 0], [0, 1], [1, 0], [1, 1]]) d1 = ceil(minIndx / 4) d2 = mod(minIndx, 4) mydist = Kohonen.distM(mat([d1, d2]), jdpx.transpose()) # print mydist
# -*- coding:utf-8 -*- # Filename : testBoltzmann01.py import operator import copy import Untils import Boltzmann from numpy import * import matplotlib.pyplot as plt dataSet = Untils.loadDataSet("dataSet25.txt") cityPosition = mat(dataSet) m, n = shape(cityPosition) bestx, di = Boltzmann.boltzmann(cityPosition, MAX_ITER=1000, T0=100) # 优化前城市图,路径图 Untils.drawScatter(cityPosition, flag=False) Untils.drawPath(list(range(m)), cityPosition) # 显示优化后城市图,路径图 Untils.drawScatter(cityPosition, flag=False) Untils.drawPath(bestx, cityPosition, color='b') # 绘制误差趋势线 x0 = list(range(len(di))) Untils.TrendLine(x0, di)
# -*- coding: GBK -*- # Filename :gradDecent.py from numpy import * import operator import Untils import matplotlib.pyplot as plt # BP神经网络 # 数据集: 列1:截距 1;列2:x坐标; 列3:y坐标 dataMat,classLabels = Untils.loadDataSet("student.txt") dataMat = mat(dataMat) classMat= mat(classLabels) # 数据归一化 dataMat = Untils.normalize(dataMat) # 绘制数据集坐标散点图 Untils.drawClassScatter(dataMat,classLabels,False) # m行数 n列数 m,n = shape(dataMat) labelMat = classMat.transpose() # 步长 alpha = 0.001 # 迭代次数 maxCycles = 500 #构成线性分割线 y=a*x+b: b:weights[0]; a:weights[1]/weights[2] weights = ones((n,1)) # 计算回归系数 weights
# -*- coding:utf-8 -*- # Filename : testBoltzmann01.py import operator import copy import Untils import Boltzmann from numpy import * import matplotlib.pyplot as plt dataSet = Untils.loadDataSet("cities.txt") cityPosition = mat(dataSet) m,n = shape(cityPosition) pn = m # 将城市的坐标矩阵转换为邻接矩阵(城市间距离矩阵) dist = Boltzmann.distM(cityPosition,cityPosition.transpose()) # 初始化 MAX_ITER = 2000 # 1000-2000 MAX_M = m; Lambda = 0.97; T0 = 1000; # 100-1000 # 构造一个初始可行解 x0 = arange(m) random.shuffle(x0) # T = T0; iteration = 0; x = x0; # 路径变量 xx = x0.tolist(); # 每个路径 di = []
# -*- coding: utf-8 -*- # Filename : testKohonen.py import numpy as np import operator import Untils import Kohonen from numpy import * import matplotlib.pyplot as plt # 加载坐标数据文件 dataSet = Untils.loadDataSet("dataset.txt"); dataMat = mat(dataSet) dm,dn = shape(dataMat) # 归一化数据 normDataset = Kohonen.mapMinMax(dataMat) # 参数 # 学习率 rate1max=0.8 #0.8 rate1min=0.05; # 学习半径 r1max=3; r1min=0.8 #0.8 ## 网络构建 Inum=2; M=2; N=2; K=M*N; #Kohonen总节点数 # Kohonen层节点排序
# -*- coding:utf-8 -*- # Filename : testBoltzmann01.py import operator import copy import Untils import Boltzmann from numpy import * import matplotlib.pyplot as plt dataSet = Untils.loadDataSet("cities.txt") cityPosition = mat(dataSet) m, n = shape(cityPosition) pn = m # 将城市的坐标矩阵转换为邻接矩阵(城市间距离矩阵) dist = Boltzmann.distM(cityPosition, cityPosition.transpose()) # 初始化 MAX_ITER = 2000 # 1000-2000 MAX_M = m Lambda = 0.97 T0 = 1000 # 100-1000 # 构造一个初始可行解 x0 = arange(m) random.shuffle(x0) # T = T0 iteration = 0 x = x0 # 路径变量
# -*- coding:utf-8 -*- # Filename : testBoltzmann01.py import operator import copy import Untils import Boltzmann from numpy import * import matplotlib.pyplot as plt dataSet = Untils.loadDataSet("dataSet25.txt") cityPosition = mat(dataSet) m,n = shape(cityPosition) bestx,di = Boltzmann.boltzmann(cityPosition,MAX_ITER = 1000,T0 = 100) # 优化前城市图,路径图 Untils.drawScatter(cityPosition,flag=False) Untils.drawPath(range(m),cityPosition) # 显示优化后城市图,路径图 Untils.drawScatter(cityPosition,flag=False) Untils.drawPath(bestx,cityPosition,color='b') # 绘制误差趋势线 x0 = range(len(di)); Untils.TrendLine(x0,di)
# -*- coding: GBK -*- # Filename : 01dataSet.py import numpy as np import operator import Untils import BackPropgation from numpy import * import matplotlib.pyplot as plt dataMat,classLabels = Untils.loadDataSet("student.txt") # 绘制图形:二维散点,无分类 # Untils.drawScatter(dataMat) # 绘制图形:二维散点,有分类,适合训练集 # Untils.drawClassScatter(mat(dataMat),classLabels) # 合并两个多维的matrix,并返回合并后的Matrix # 输入参数有先后顺序 # [m,n]=shape(dataMat) # classMat = transpose(mat(classLabels)) # matMerge = Untils.mergMatrix(mat(dataMat),classMat) # 元素乘法 # a = mat([1,1,1]) ;b = mat([2,2,2]) # print multiply(a,b) # 测试BackPropgation.dlogsig(hp,tau) # A = mat([0,1,2]); # print "A*(1-A)",multiply(A,(1-A))