1063. Set Similarity (25)-PAT

本文介绍了一个计算两个整数集合相似度的方法,通过定义相似度为两集合共有不同元素数量除以总的不同元素数量来衡量集合间的相似性。文章提供了一个具体的输入输出样例,并给出了实现该功能的C++代码。

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
Sample Output:
50.0%
33.3%
推荐指数:※
merge原理
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int compare(const void *a,const void *b){
	return *(long*)a-*(long*)b;
}
float similarity(long *a,int len_a,long *b,int len_b){
	qsort(a,len_a,sizeof(long),compare);
	qsort(b,len_b,sizeof(long),compare);

	int i=0,j=0;
	int sum=0;
	int count=0;
	while(i<len_a&&j<len_b){
		while(i+1<len_a){
			if(a[i]==a[i+1]){
					i++;
					sum++;
			}
			else 
				break;
		}
	   while(j+1<len_b){
			if(b[j]==b[j+1]){
					j++;
					sum++;
			}
			else 
				break;
		}
	   if(a[i]<b[j])
		   i++;
	   else if(a[i]>b[j])
		   j++;
	   else{
			i++;
			j++;
			count++;
	   }
	}
	float res=count*1.0/(len_a+len_b-sum-count);
	return res;
}
int main()
{
	int n,i,j;
	scanf("%d",&n);
	long **num=new long *[n+1];
	int *len=new int[n+1];
	for(i=1;i<=n;i++){
		scanf("%d",&len[i]);
		num[i]=new long [len[i]];
		for(j=0;j<len[i];j++)
			scanf("%ld",&num[i][j]);
	}
	int k;
	scanf("%d",&k);
	for(i=0;i<k;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		float sim=similarity(num[a],len[a],num[b],len[b]);
		printf("%.1f%%\n",sim*100);
	}
	return 0;
}


你提供的这段输出是一个长度为 **100** 的浮点数数组,它看起来像是一个 **词向量(word embedding)** 或 **句子向量(sentence embedding)** 的表示。 这类向量通常由以下几种模型生成: - **Word2Vec** - **FastText** - **Doc2Vec** - **BERT / Sentence-BERT** - **Gensim** 中的模型 - 或你自己训练/加载的模型生成的向量 --- ## 🧠 一、这段向量的含义 这段输出表示一个 **100 维的向量**,每个值代表该维度上的权重或激活值。例如: ```python [-0.0052772, -0.00601762, ..., 0.00234289] ``` ### 它可以代表什么? | 场景 | 含义 | |------|------| | Word2Vec | 一个词的向量表示,如 "人工智能" | | FastText | 一个词的向量,支持未登录词(OOV) | | Doc2Vec | 一个句子、段落的向量表示 | | Sentence-BERT | 一个句子的语义向量,用于相似度计算 | --- ## 🧪 二、如何使用这个向量 ### 1. 获取相似词或句子(以 Gensim 模型为例) 如果你是从 Gensim 模型中获取的向量,可以使用如下方式查找相似词或句子: ```python from gensim.models import Word2Vec # 加载模型 model = Word2Vec.load("word2vec.model") # 获取某个词的向量 vector = model.wv["人工智能"] # 查找最相似的词 similar_words = model.wv.most_similar("人工智能") print(similar_words) ``` ### 2. 向量相似度计算(cosine similarity) ```python import numpy as np from sklearn.metrics.pairwise import cosine_similarity vec1 = np.array([-0.0052772, ..., 0.00234289]) # 第一个向量 vec2 = np.array([0.00312068, ..., -0.00486073]) # 第二个向量 # reshape 为二维数组 vec1 = vec1.reshape(1, -1) vec2 = vec2.reshape(1, -1) similarity = cosine_similarity(vec1, vec2) print("Cosine Similarity:", similarity[0][0]) ``` --- ## 💾 三、如何保存和加载向量 ### 1. 保存为文件(如 numpy 文件) ```python import numpy as np vector = np.array([-0.0052772, ..., 0.00234289]) # 你的向量 np.save("vector.npy", vector) ``` ### 2. 加载向量 ```python vector = np.load("vector.npy") print(vector) ``` --- ## 🧰 四、常见用途 | 用途 | 说明 | |------|------| | 文本相似度 | 使用 cosine similarity 比较两个句子/词的语义相似性 | | 聚类 | 将多个向量进行聚类分析,如 KMeans | | 分类 | 作为特征输入到分类模型中(如 SVM、MLP) | | 可视化 | 使用 t-SNE 或 PCA 降维后可视化向量空间 | --- ## ✅ 五、总结 你看到的这段向量是一个 **100 维的嵌入向量**,可能是: - 某个词的向量(Word2Vec/FastText) - 某个句子的向量(Doc2Vec) - 某个模型输出的语义向量(如 BERT) 你可以: - 用它做相似度计算 - 用它做聚类或分类 - 用它做可视化分析 --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值