数据分析:5个数据相关性指标

简介: 相似性度量是许多数据分析和机器学习任务中的重要工具,使我们能够比较和评估不同数据片段之间的相似性。有许多不同的指标可用,每个指标各有利弊,适用于不同的数据类型和任务。

1. 介绍

本文将探讨一些最常见的相似性指标并比较它们的优缺点。通过了解这些指标的特点和局限性,我们可以选择最适合我们特定需求的指标,并确保结果的准确性和相关性。

2. 指标

2.1. 欧几里得距离

该指标计算 n 维空间中两点之间的直线距离。它常用于连续的数值数据,易于理解和实现。但是,它可能对异常值很敏感,并且没有考虑不同特征的相对重要性。

from scipy.spatial import distance

# Calculate Euclidean distance between two points
point1 = [1, 2, 3]
point2 = [4, 5, 6]

# Use the euclidean function from scipy's distance module to calculate the Euclidean distance
euclidean_distance = distance.euclidean(point1, point2)

2.2. 曼哈顿距离

该指标通过考虑两点坐标在每个维度中的绝对差异并将它们相加来计算两点之间的距离。它对离群点的敏感性不如欧氏距离,但在某些情况下可能无法准确反映点与点之间的实际距离。

from scipy.spatial import distance

# Calculate Manhattan distance between two points
point1 = [1, 2, 3]
point2 = [4, 5, 6]

# Use the cityblock function from scipy's distance module to calculate the Manhattan distance
manhattan_distance = distance.cityblock(point1, point2)

# Print the result
print("Manhattan Distance between the given two points: " + \
      str(manhattan_distance))

2.3. 余弦相似度

该指标通过考虑角度来计算两个向量之间的相似度。它通常用于文本数据并且可以抵抗向量大小的变化。但是,它没有考虑不同特征的相对重要性。

from sklearn.metrics.pairwise import cosine_similarity

# Calculate cosine similarity between two vectors
vector1 = [1, 2, 3]
vector2 = [4, 5, 6]

# Use the cosine_similarity function from scikit-learn to calculate the similarity
cosine_sim = cosine_similarity([vector1], [vector2])[0][0]

# Print the result
print("Cosine Similarity between the given two vectors: " + \
      str(cosine_sim))Jaccard Similarity

2.4. Jaccard相似度

该指标通过考虑两个集合的交集和并集的大小来计算两个集合之间的相似性。它通常用于分类数据并且可以抵抗集合大小的变化。但是,它不考虑集合的顺序或元素的频率。

def jaccard_similarity(list1, list2):
    """
    Calculates the Jaccard similarity between two lists.
    
    Parameters:
    list1 (list): The first list to compare.
    list2 (list): The second list to compare.
    
    Returns:
    float: The Jaccard similarity between the two lists.
    """
    # Convert the lists to sets for easier comparison
    s1 = set(list1)
    s2 = set(list2)
    
    # Calculate the Jaccard similarity by taking the length of the intersection of the sets
    # and dividing it by the length of the union of the sets
    return float(len(s1.intersection(s2)) / len(s1.union(s2)))

# Calculate Jaccard similarity between two sets
set1 = [1, 2, 3]
set2 = [2, 3, 4]
jaccard_sim = jaccard_similarity(set1, set2)

# Print the result
print("Jaccard Similarity between the given two sets: " + \
      str(jaccard_sim))

2.5. 皮尔逊相关系数

该指标计算两个变量之间的线性相关性。它通常用于连续的数值数据,并考虑不同特征的相对重要性。但是,它可能无法准确反映非线性关系。

import numpy as np

# Calculate Pearson correlation coefficient between two variables
x = [1, 2, 3, 4]
y = [2, 3, 4, 5]

# Numpy corrcoef function to calculate the Pearson correlation coefficient and p-value
pearson_corr = np.corrcoef(x, y)[0][1]

# Print the result
print("Pearson Correlation between the given two variables: " + \
      str(pearson_corr))
相关文章
|
4天前
|
数据可视化 前端开发 数据挖掘
R语言对综合社会调查GSS数据进行自举法bootstrap统计推断、假设检验、探索性数据分析可视化|数据分享(上)
R语言对综合社会调查GSS数据进行自举法bootstrap统计推断、假设检验、探索性数据分析可视化|数据分享
|
4天前
|
存储 分布式计算 搜索推荐
【专栏】数据之海,分布式计算、数据存储与管理、数据分析与挖掘成为关键技术
【4月更文挑战第27天】在大数据时代,数据量爆炸性增长、类型多样及处理速度需求提升带来挑战。分布式计算、数据存储与管理、数据分析与挖掘成为关键技术,如Hadoop、Spark、HDFS、NoSQL等。实际应用包括互联网搜索、推荐系统、金融科技、智能城市等领域,大规模数据处理发挥关键作用,持续推动创新与奇迹。
|
4天前
|
数据采集 数据可视化 数据挖掘
Python 与 PySpark数据分析实战指南:解锁数据洞见
Python 与 PySpark数据分析实战指南:解锁数据洞见
|
4天前
|
SQL 数据采集 存储
Hive实战 —— 电商数据分析(全流程详解 真实数据)
关于基于小型数据的Hive数仓构建实战,目的是通过分析某零售企业的门店数据来进行业务洞察。内容涵盖了数据清洗、数据分析和Hive表的创建。项目需求包括客户画像、消费统计、资源利用率、特征人群定位和数据可视化。数据源包括Customer、Transaction、Store和Review四张表,涉及多个维度的聚合和分析,如按性别、国家统计客户、按时间段计算总收入等。项目执行需先下载数据和配置Zeppelin环境,然后通过Hive进行数据清洗、建表和分析。在建表过程中,涉及ODS、DWD、DWT、DWS和DM五层,每层都有其特定的任务和粒度。最后,通过Hive SQL进行各种业务指标的计算和分析。
46 1
Hive实战 —— 电商数据分析(全流程详解 真实数据)
|
4天前
|
前端开发 数据可视化 数据挖掘
R语言对综合社会调查GSS数据进行自举法bootstrap统计推断、假设检验、探索性数据分析可视化|数据分享(下)
R语言对综合社会调查GSS数据进行自举法bootstrap统计推断、假设检验、探索性数据分析可视化|数据分享
|
4天前
|
数据挖掘 Python
SPSS时间序列ARIMA、指数平滑法数据分析汽车销量数据
SPSS时间序列ARIMA、指数平滑法数据分析汽车销量数据
|
4天前
|
机器学习/深度学习 安全 数据挖掘
数据分享|函数型数据分析部分省市新冠疫情数据
数据分享|函数型数据分析部分省市新冠疫情数据
|
4天前
|
数据可视化 数据挖掘 Linux
如何在Linux部署DataEase数据分析服务并实现无公网IP远程分析内网数据信息
如何在Linux部署DataEase数据分析服务并实现无公网IP远程分析内网数据信息
|
4天前
|
数据挖掘 API 区块链
《区块链公链数据分析简易速速上手小册》第2章:数据获取基础(2024 最新版)
《区块链公链数据分析简易速速上手小册》第2章:数据获取基础(2024 最新版)
45 0
|
4天前
|
数据可视化 数据挖掘
R语言分段回归数据数据分析案例报告
R语言分段回归数据数据分析案例报告