赞
踩
没有更新完之前,专栏价格为59,更新完毕之后恢复到99. 专栏内包含2024年所有数学建模比赛思路和代码,有些重要比赛着重更新(华数杯、国赛、美赛),小比赛可能会有chatgpt4更新,只需订阅一次。有些文章没有完整代码,请到专栏内查找最新代码和思路。如果比赛结束后没有更新代码(可能会有事情来不及更新)赛后我会统一退款。
目录
2024年第五届“华数杯”专栏地址:https://blog.csdn.net/m0_52343631/category_12482955.html?spm=1001.2014.3001.5482
2021年优秀论文
- from numpy import *
- import pandas as pd
- import xlrd
- import matplotlib.pyplot as plt
- def loadDataSet(fileName):
- dataMat = [ ]
- fr = xlrd.open_workbook(fileName)
- sheet = fr.sheets()[0]
- cols = sheet.ncols
- for i in range(cols):
- #curLine = line.strip().split('\t')
- fltcol = list(sheet.col_values(i))
- '''
- if i <= 3:
- print(fltcol)
- '''
-
- dataMat.append(fltcol)
- return dataMat
- def distEclud(vecA, vecB):
- return sqrt(sum(power(vecA-vecB, 2)))
- def randCent(dataSet,k):
- n = shape(dataSet)[1]
- centroids = mat(zeros((k,n)))
- for j in range (n):
- minJ = min(dataSet[:,j])
- rangeJ = float(max(dataSet[:,j]) - minJ)
- centroids[:,j] = minJ + rangeJ * random.rand(k, 1)
- return centroids
- def kMeans(dataSet, k, distMeas=distEclud, createCent=randCent):
- m = shape(dataSet)[0]
- clusterAssment = mat(zeros((m,2)))
- centroids = createCent(dataSet, k)
- clusterChanged = True
- while clusterChanged:
- clusterChanged = False
- for i in range(m):
- minDist = inf; minIndex = -1
- for j in range(k):
- distJI = distMeas(centroids[j,:],dataSet[i,:])
- if distJI < minDist:
- minDist = distJI; minIndex = j
- if clusterAssment[i,0] != minIndex: clusterChanged = True
- clusterAssment[i,:] = minIndex,minDist**2
- #print(centroids)
- for cent in range(k):
- ptsInClust = dataSet[nonzero(clusterAssment[:,0].A==cent)[0]]
- if len(ptsInClust)!= 0:
- centroids[cent,:] = mean(ptsInClust, axis=0)
-
- #centroids[cent,:] = mean(ptsInClust, axis=0)
- return centroids, clusterAssment
- '''
- def kMeans(dataset, tezhengshu, jisuan_distance = distEclud, readdata = randCent):
- dataset_lie_shu=shape(dataset)[1]
- dataset_hang_shu=shape(dataset)[0]
-
- tongji_juzhen=array(ones([dataset_hang_shu,2]))
- #ll=numpy.ones([dataset_hang_shu, 1])
- #print(type(ll))
- #print_date(dataset,numpy.array(numpy.ones([dataset_hang_shu,1])).flatten())
- creat_random_juzhen= creat_cluter (dataset,dataset_lie_shu,4)
- clu_change=True
- lun_index=1
- while clu_change:
- clu_change=False
- for i in range(dataset_hang_shu):
- juli_juzhen=[(jisuan_distance(creat_random_juzhen[j,:],dataset[i,:])) for j in range(4)]
- zhi_min_juli_juzhen=min(juli_juzhen)
- index_min_juli_juzhen=juli_juzhen.index(zhi_min_juli_juzhen)
- if tongji_juzhen[i,0] != index_min_juli_juzhen:
- clu_change=True
- tongji_juzhen[i,:]=index_min_juli_juzhen,zhi_min_juli_juzhen
- for i in range(4):
- belong_juzhen=nonzero(tongji_juzhen[:,0] == i)[0]
- #print(belong_juzhen)
- belong_dataset=dataset[belong_juzhen]
- if len(belong_juzhen) != 0:
- creat_random_juzhen[i,]=mean(belong_dataset,axis=0)
- print("第%d 轮第%d 个簇点改变:"%(lun_index,i))
- print(creat_random_juzhen)
- lun_index+=1
- return creat_random_juzhen,tongji_juzhen
- def creat_cluter(dataset,dataset_lie_shu,tezhengshu):
- temp_cluter=array(zeros([tezhengshu,dataset_lie_shu]))
- for i in range(dataset_lie_shu):
- min_lie=min(dataset[:,i])
- max_lie=max(dataset[:,i])
- kuadu_lie=max_lie-min_lie
- ran_lie=kuadu_lie*(random.rand())+min_lie
- temp_cluter[:,i]=ran_lie
- return array(temp_cluter)
- '''
- #########################################################
- def dist(vec1, vec2):
- return sqrt(sum(square(vec1 - vec2)))
- def main():
- datMat = mat(loadDataSet('shuju_nom.xlsx'))
- myCentriods, clustAssing = kMeans(datMat, 30)
- print(myCentriods)
- #print(clustAssing)
- index = 0
- dis = []
- for i in clustAssing[:,0]:
- dis.append(dist(array(datMat[index]), myCentriods[int(i)].view()))
- print(dist(array(datMat[index]), myCentriods[int(i)].view()))
- data1 = pd.DataFrame(myCentriods.T)
- data2 = pd.DataFrame(clustAssing[:,0].T)
- data3 = pd.DataFrame(array(dis).T)
-
- writer = pd.ExcelWriter('output.xlsx')
- data1.to_excel(writer, 'page_1', float_format='%.5f')
- data2.to_excel(writer, 'page_2', float_format='%d')
- data3.to_excel(writer, 'page_3', float_format='%.5f')
- writer.save()
- writer.close()
- if __name__ == '__main__':
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。