Introduction:

*It computes the degree-d polynomial kernel between two vectors.
*It represents the similarity between two vectors.
*It considers not only the similarity between vectors under the same dimension, but also across dimensions.
*It accounts for feature interaction in ML.

Parameter:

*X - array like structure.
*Y - array like structure.
*d - kernel degree(default value = 3).
*co - constant(default value = 1).
*gamma - 1/(no of features).

Returns:

*Returns the gram(kernel) matrix.

Implementation of Polynomial Kernel:

from math import *
import numpy as np

class PolKer:

def ker_calc(self,l1,l2,n_features,co = 1,d = 3):

ans = []
gam = 1.0/n_features

for i in range(len(l1)):

x = []
for j in range(len(l2)):

y = ((gam * l1[i][0] * l2[j][0]) + co) ** d
x.append(y)

ans.append(x)

print(ans)

def main():

Po = PolKer()
l1 = [[2],
[4]]
l2 = [[1],
[6]]
n_features = len(l1[0])

Po.ker_calc(l1,l2,n_features)

if __name__=="__main__":
main()

[[ 27. 2197.]
[ 125. 15625.]]

Using Inbuilt Library:

from sklearn.metrics.pairwise import polynomial_kernel
x=np.array([[2],
[4]])
y=np.array([[1],
[6]])
print(polynomial_kernel(x,y))

Output:

[[ 27. 2197.]
[ 125. 15625.]]

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 665 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 411 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