Guangning Yu's Blog
Home
Code
Data
Setup
Industry
MachineLearning
Archive
Calculate the similarity of two vectors
2019-02-17 01:40:32
|
MachineLearning
### Euclidean distance ```Python from sklearn.metrics.pairwise import euclidean_distances euclidean_distances([[1,2,3], [100,200,300]]) # return: # array([[ 0. , 370.42408129], # [370.42408129, 0. ]]) ``` ### Cosine similarity ```Python from sklearn.metrics.pairwise import cosine_similarity cosine_similarity([[1,2,3],[100,200,300]]) # return: # array([[1., 1.], # [1., 1.]]) ``` ### Pearson correlation ```Python from scipy.stats.stats import pearsonr pearsonr([1,2,3], [100,200,300]) # return ('1.0', 0.0) // (Pearson’s correlation coefficient, 2-tailed p-value) ```
Previous:
Hive Basics
Next:
Cosine Similarity and Pearson Correlation Coefficient