Python Scikit Learn Metrics - Pairwise Distances Argmin














































Python Scikit Learn Metrics - Pairwise Distances Argmin



Introduction:


*It computes minimum distances between one point and a set of points. *This function computes for each row in X, the index of the row of Y which is closest (according to the specified distance). *This function works with dense 2D arrays only.



Parameter:

*X - array like structure.
*Y - array like structure.


Returns:

*Returns minimum indices array.



Implementation of Pairwise Distances Argmin:


from math import *
import numpy as np

class MinDis:

  def dii(self,a,b):
    
    s=0
    for i in range(len(a)):
      s += (a[i] - b[i]) * (a[i] - b[i])
    
    return sqrt(s)



  def midis_calc(self,l1,l2):
    
    ans = []
    for j in range(len(l1)):

      x = self.dii(l1[j],l2[0])
      idx = 0

      for i in range(1,len(l2)):
        y = self.dii(l1[j],l2[i])
        if y<x:
          idx = i
        
      ans.append(idx)

    print(ans)


def main():

  dis = MinDis()
  l1 = [[1,2],
        [4,5]]
  l2 = [[1,2],
        [4,5]]
dis.midis_calc(l1,l2)
  


if __name__=="__main__":
  main()




Output:

[0, 1]




Using Inbuilt Library:


from sklearn.metrics import pairwise_distances_argmin
l1 = [[1,2],
      [4,5]]
l2 = [[1,2],
      [4,5]]
print(pairwise_distances_argmin(l1,l2))




Output:

[0, 1]




More Articles of Piyush unknown:

Name Views Likes
Python Scikit Learn - Precision Score 498 1
Python Scikit Learn - Mean Absolute Error 387 1
Python Scikit Learn Linear Model - MNIST with Logistic Regression 706 1
Python Scikit Learn Metrics - DCG Score 627 1
Python Scikit Learn Neighbors - KNeighborsClassifier 494 2
Python Scikit Learn Metrics - Sigmoid Kernel 784 2
Python Scikit Learn Metrics - Cohen Kappa Score 2216 1
Python Scikit Learn Metrics - ROC Curve 322 1
Python Scikit Learn Metrics - ROC AUC Score 489 1
Python Scikit Learn Metrics - Mean Poisson Deviance 995 1
Python Scikit Learn Metrics - NaN Euclidean Distances 1830 1
Python Scikit Learn - Jaccard Score 1338 1
Python Scikit Learn Introduction 374 1
Python Scikit Learn - FBeta Score 402 1
Python Scikit Learn - Max Error 406 1
Python Scikit Learn Metrics - Chi2 Kernel 539 2
Python Scikit Learn Metrics - Manhattan Distances 774 1
Python Scikit Learn Metrics - Euclidean Distance 351 1
Python Scikit Learn Model Selection - Train Test Split 566 1
Python Scikit Learn Metrics - Laplacian Kernel 713 2
Python Scikit Learn Metrics - Zero One Loss 1063 1
Python Scikit Learn - Hamming Loss 379 1
Python Scikit Learn Metrics - Linear Kernel 586 1
Pytho Scikit Learn - Balanced Accuracy Score 804 1
Python Scikit Learn - Accuracy Score 410 1
Python Scikit Learn - F1 score 464 1
Python Scikit Learn Metrics - Polynomial Kernel 559 2
Python Scikit Learn Metrics - AUC 311 1
Python Scikit Learn - Mean Squared Log Error 346 1
Python Scikit Learn Metrics - Precision Recall Fscore Support 1160 1
Python Scikit Learn - R2 Score 407 1
Python Scikit Learn Preprocessing - Standard Scaler 305 1
Python Scikit Learn Linear Model - Linear Regression 282 1
Python Scikit Learn Metrics - Brier Score 667 1
Python Scikit Learn - Median Absolute Error 749 1
Python Scikit Learn Metrics - Multilabel Confusion Matrix 889 1
Python Scikit Learn Linear Model - Logistic Regression 310 1
Python Scikit Learn - Mean Squared Error 448 1
Python Scikit Learn - Matthews Correlation Coefficient 1236 1
Python Scikit Learn Metrics - Cosine Similarity 626 1
Python Scikit Learn Metrics - NDCG Score 2087 1
Python Scikit Learn Metrics - Pairwise Distances Argmin 539 2
Python Scikit Learn Preprocessing - Max Abs Scaler 819 1
Python Scikit Learn Preprocessing - Min Max Scaler 464 1
Python Scikit Learn Metrics - Mean Gamma Deviance 1091 1
Python Scikit Learn - Confusion Matrix 404 1
Python Scikit Learn - Recall Score 381 1
Python Scikit Learn Metrics - Cosine Distances 350 1
Python Scikit Learn - Explained Variance Score 932 1
Python Scikit Learn Metrics - Precision Recall Curve 629 1

Comments