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 401 1
Python Scikit Learn - Mean Absolute Error 333 1
Python Scikit Learn Linear Model - MNIST with Logistic Regression 605 1
Python Scikit Learn Metrics - DCG Score 549 1
Python Scikit Learn Neighbors - KNeighborsClassifier 424 2
Python Scikit Learn Metrics - Sigmoid Kernel 629 2
Python Scikit Learn Metrics - Cohen Kappa Score 2004 1
Python Scikit Learn Metrics - ROC Curve 264 1
Python Scikit Learn Metrics - ROC AUC Score 420 1
Python Scikit Learn Metrics - Mean Poisson Deviance 810 1
Python Scikit Learn Metrics - NaN Euclidean Distances 1452 1
Python Scikit Learn - Jaccard Score 1198 1
Python Scikit Learn Introduction 321 1
Python Scikit Learn - FBeta Score 332 1
Python Scikit Learn - Max Error 320 1
Python Scikit Learn Metrics - Chi2 Kernel 441 2
Python Scikit Learn Metrics - Manhattan Distances 636 1
Python Scikit Learn Metrics - Euclidean Distance 302 1
Python Scikit Learn Model Selection - Train Test Split 490 1
Python Scikit Learn Metrics - Laplacian Kernel 619 2
Python Scikit Learn Metrics - Zero One Loss 917 1
Python Scikit Learn - Hamming Loss 326 1
Python Scikit Learn Metrics - Linear Kernel 460 1
Pytho Scikit Learn - Balanced Accuracy Score 692 1
Python Scikit Learn - Accuracy Score 358 1
Python Scikit Learn - F1 score 396 1
Python Scikit Learn Metrics - Polynomial Kernel 484 2
Python Scikit Learn Metrics - AUC 254 1
Python Scikit Learn - Mean Squared Log Error 291 1
Python Scikit Learn Metrics - Precision Recall Fscore Support 1028 1
Python Scikit Learn - R2 Score 351 1
Python Scikit Learn Preprocessing - Standard Scaler 242 1
Python Scikit Learn Linear Model - Linear Regression 230 1
Python Scikit Learn Metrics - Brier Score 577 1
Python Scikit Learn - Median Absolute Error 666 1
Python Scikit Learn Metrics - Multilabel Confusion Matrix 764 1
Python Scikit Learn Linear Model - Logistic Regression 252 1
Python Scikit Learn - Mean Squared Error 367 1
Python Scikit Learn - Matthews Correlation Coefficient 1142 1
Python Scikit Learn Metrics - Cosine Similarity 503 1
Python Scikit Learn Metrics - NDCG Score 1773 1
Python Scikit Learn Metrics - Pairwise Distances Argmin 412 2
Python Scikit Learn Preprocessing - Max Abs Scaler 686 1
Python Scikit Learn Preprocessing - Min Max Scaler 364 1
Python Scikit Learn Metrics - Mean Gamma Deviance 908 1
Python Scikit Learn - Confusion Matrix 346 1
Python Scikit Learn - Recall Score 329 1
Python Scikit Learn Metrics - Cosine Distances 293 1
Python Scikit Learn - Explained Variance Score 840 1
Python Scikit Learn Metrics - Precision Recall Curve 537 1

Comments