合作者推荐第一步 k-means 聚类

第一步:得到两个作者的数据,发表的论文数和论文被引用的数量的和

# -*- coding: utf-8 -*-
"""
Created on Thu Sep  5 10:40:57 2019

@author: Administrator
"""
import json

fo=open("D:\\mag_authors_2\\mag_authors_10.txt",'r+')
fi=open('demo3.txt','a')   
#print fo.readline()
for i in range(10000):
    js=json.loads(fo.readline())
    n_pubs=str(js['n_pubs'])
    n_citation=str(js['n_citation'])
    fi.write(n_pubs+' '+n_citation+'\n')
    print js['n_pubs'],js['n_citation']
fo.close()
fi.close()

数据示例
在这里插入图片描述
第二部:进行聚类 当然是K-means 算法啦

# -*- coding: utf-8 -*-
"""
Created on Wed Sep  4 13:06:59 2019

@author: Administrator
"""

import numpy as np
import numpy.matlib 
import math as mt
import matplotlib.pyplot as plt
def d(a1,a2):
    return np.sqrt(np.sum((np.abs(a1-a2)**2)))
def getlist():#从文本中读取取数据
    fo=open("demo3.txt",'r+')
    X=[]
    for str in fo:
        z=str.split(' ')
        x=[float(z[0]),float(z[1])]
        X.append(np.array(x))
    return X



X=getlist()
a1=X[4]
a2=X[9]
a3=X[7]
line1x=[]
line1y=[]
line2x=[]
line2y=[]
line3x=[]
line3y=[]
D1=[]#D 为初始化簇
D2=[]
D3=[]
while 1:#迭代求质点和簇
    b1=a1
    b2=a2
    b3=a3
    line1x.append(a1[0])#画图用
    line1y.append(a1[1])
    plt.plot(a1[0],a1[1],'xb')
    line2x.append(a2[0])
    line2y.append(a2[1])
    plt.plot(a2[0],a2[1],'xr')
    line3x.append(a3[0])
    line3y.append(a3[1])
    plt.plot(a3[0],a3[1],'xg')
    D1=[]#初始化簇
    D2=[]
    D3=[]
    for i in X:#计算欧氏距离,把最近的添加到各簇中
        d1=d(a1,i)
        d2=d(a2,i)
        d3=d(a3,i)
        if d1<=d2:
            if d1<=d3:
                D1.append(i)
            else:
                D3.append(i)
        else:
            if d2<=d3:
                D2.append(i)
            else:
                D3.append(i)
    
    
    if len(D1)!=0:#如果簇中没有对象,则质点不变,若有对象则重新计算质点,为各指标的和的平均,下同
        x=0
        y=0
        for j in D1:
            x+=j[0]
            y+=j[1]
        a1=[x/len(D1),y/len(D1)]
   
        
    
        
    if len(D2)!=0:
        x=0
        y=0
        for j in D2:
            x+=j[0]
            y+=j[1]
        a2=[x/len(D2),y/len(D2)]
    
        
    if len(D3)!=0:
        x=0
        y=0
        for j in D3:
            x+=j[0]
            y+=j[1]
        a3=[x/len(D3),y/len(D3)]
    
        
    print a1,a2,a3
    if np.array_equal(a1,b1)&np.array_equal(a2,b2)&np.array_equal(a3,b3):#比较质点是否发生变化(是否收敛)收敛则跳出迭代
        print '收敛'
        break
print D1
print D2
print D3
plt.plot(line1x,line1y)#画出质点的变化路径
plt.plot(line2x,line2y)
plt.plot(line3x,line3y)


x1=np.array(D1).T#画出各簇中的对象
plt.plot(x1[0],x1[1],'^b',markersize=1)
x1=np.array(D2).T
plt.plot(x1[0],x1[1],'or',markersize=1)
x1=np.array(D3).T
plt.plot(x1[0],x1[1],'og',markersize=1)
plt.show()

最后结果:
在这里插入图片描述

Softmax函数是一种常用且重要的函数,尤其在器学习和深度学习中的多分类场景中使用广泛。它可以将多分类的结果以概率的形式展现出来,并且保证这些概率之和为1。 在多分类任务中,softmax函数可以用来求解各类别的概率分布。例如,在手写数字图片识别任务中,我们可以通过softmax函数得到每个数字的概率分布,这些概率之和为1。通过对输入进行映射,softmax函数将输入值映射为0到1之间的实数,并保证各个类别的概率之和为1。这样,我们可以根据这些概率分布来判断输入属于哪个类别。 总结来说,softmax函数在多分类中起到了对输入进行归一化的作用,将输入映射为概率分布,并且使得这些概率之和为1。这样我们可以通过这些概率分布来进行多分类任务的预测。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Pytorch深度学习(六):Softmax函数实现多分类](https://blog.csdn.net/qq_40806950/article/details/126453269)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [softmax多分类.rar](https://download.csdn.net/download/qq_27047075/12261311)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值