import numpy as np
from sklearn.svm import SVC
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics.pairwise import rbf_kernel
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import cosine_similarity
# X, y = datasets.load_breast_cancer(return_X_y=True)
X, y = datasets.make_blobs(n_features=2,n_samples=200,centers=2, cluster_std=[2.0, 2.0], random_state=100)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=100)
plt.scatter(X_train[:,0], X_train[:,1],c=y_train)
# plt.show()
m,d = X_train.shape
gamma = 1/(d*X_train.var())
model = SVC(kernel="linear",C=300)
model.fit(X=X_train,y=y_train)
y_pred = model.predict(X=X_test)
print(model.support_vectors_)
A = model.support_vectors_
plt.scatter(A[:,0],A[:,1],c='g',marker="^")
w = model.coef_[0]
print("系数",w)
plt.scatter(w[0],w[1],s=120,c='r',marker="*")
plt.scatter(-2.21313321 ,-0.33918436, s=300)
plt.scatter(0,0,s=120,c="r",marker='>')
plt.show()
Acc = accuracy_score(y_test, y_pred)
print("精度::",Acc)
A = np.array([-2.21313321,-0.33918436])
B = np.array([-2.21313321, -0.33918436])
V = A-B
print(cosine_similarity([V], [w]))
# A = np.array([1,2,3,4])
# B = np.array([2,3,4,5])
# C = np.array([7,8,9,10])
# print(rbf_kernel([A],[B]))
# print(rbf_kernel([A],[C]))
余弦距离为0
|