我的Django电子商务代码已经可以支持Django 4.X版本了,由于github很慢,现在放到百度网盘上了。链接:https://pan.baidu.com/s/1FEs6lggPtPplnYTsyEbaSg?pwd=7gq1提取码:7gq1
8.2 SVC
8.2.1 SVC类参数、属性和方法
class sklearn.svm.SVC(*, C=1.0, kernel='rbf', degree=3, gamma='scale', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=- 1, decision_function_shape='ovr', break_ties=False, random_state=None)
参数
属性 |
类型 |
解释 |
C |
float, default=1.0 |
正则化参数。正则化的强度与c成反比。必须严格为正。惩罚是l2平方惩罚。 |
kernel |
{'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'}, default='rbf' |
指定算法中要使用的内核类型。它必须是'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'或可调用类型之一。如果没有给出,将使用‘rbf’。如果给定了一个可调用的,它被用来从数据矩阵中预先计算内核矩阵;该矩阵应该是形状数组(n_samples, n_samples)。 |
gamma |
{'scale', 'auto' or float, default='scale' |
'rbf','poly'和'sigmoid'的核系数。如果gamma='scale '(默认值)被传递,那么它使用1 / (n_features * X.var())作为gamma的值,如果'auto',则使用1 / n_features。 |
属性
属性 |
类型 |
class_weight_ |
ndarray of shape (n_classes,) |
classes_ |
ndarray of shape (n_classes,) |
coef_ |
is a readonly property derived from dual_coef_ and support_vectors_. |
coef_ |
ndarray of shape (n_classes * (n_classes - 1) / 2, n_features) |
dual_coef_ |
ndarray of shape (n_classes -1, n_SV) |
fit_status_ |
int |
intercept_ |
ndarray of shape (n_classes * (n_classes - 1) / 2,) |
support_ |
ndarray of shape (n_SV) |
support_vectors_ |
ndarray of shape (n_SV, n_features) |
n_support_ |
ndarray of shape (n_classes,), dtype=int32 |
probA_ |
_ndarray of shape (n_classes * (n_classes - 1) / 2) |
probB |
_ndarray of shape (n_classes * (n_classes - 1) / 2) |
shape_fit_ |
tuple of int of shape (n_dimensions_of_X,) |
方法
decision_function(X) |
计算X中样本的决策函数。 |
fit(X, y[, sample_weight]) |
根据给定的训练数据拟合SVM模型。 |
get_params([deep]) |
获取此估计器的参数。 |
predict(X) |
对X中的样品进行分类。 |
score(X, y[, sample_weight]) |
返回给定测试数据和标签的平均精度。 |
set_params(**params) |
设置此估计器的参数。 |
8.2.2 SVC原理(分析make_blobs数据)
def SVC_Theory(): myutil = util() #创建50个数据点,分成2类 X , y = make_blobs(n_samples=50,random_state=6,centers=2) for kernel in ['linear','rbf','sigmoid','poly']: for gamma in ['scale', 'auto',0.1,0.01,0.001]: clf = SVC(kernel=kernel,gamma=gamma,C=1000)# C-SVC的惩罚参数C,默认值是1.0 clf.fit(X,y) plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.Paired,s=30) ax = plt.gca xlim = ax.get_xlim() ylim = ax.get_ylim() xx = np.linspace(xlim[0],xlim[1],30) yy = np.linspace(ylim[0],ylim[1],30) YY , XX = np.meshgrid(yy,xx) 。 xy = np.vstack([XX.ravel(),YY.ravel()]).T Z = clf.decision_function(xy). reshape(XX.shape) ax.contour(XX,YY,Z,colors='k',levels=[-1,0,1],alpha=0.5,linestyles=['--','-','--']) #绘制等高线 ax.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=100,linewidth=1,facecolors='none') title=u"VC原理,"+kernel+",gamma="+str(gamma) myutil.show_pic(title)
kernel=linner (线性核) 基本一致
kernel=rbf (径向基函数核(高斯核)),第2、3最好
kernel= Sigmoid (Sigmoid核)
kernel=poly(多项式核) 最后一个很好
8.2.3 SVC分析鸢尾花数据
#SVC分析鸢尾花数据 def SVC_for_load_iris(): warnings.filterwarnings("ignore") myutil = util() X,y = datasets.load_iris().data,datasets.load_iris().target X1 = datasets.load_iris().data[:,:2] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) for kernel in ['linear','rbf','sigmoid','poly']: for gamma in ['scale', 'auto',0.1,0.01,0.001]: clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma) clf.fit(X_train, y_train) title = "SVC_鸢尾花数据,kernel="+kernel+",gamma="+str(gamma) myutil.print_scores(clf,X_train,y_train,X_test,y_test,title) myutil.plot_learning_curve(SVC(max_iter=100000,kernel=kernel,gamma=gamma),X,y,title) myutil.draw_scatter(X,y,clf,title) myutil.show_pic(title) clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma).fit(X1,y) myutil.draw_scatter_for_clf(X1,y,clf,title)
输出 SVC_鸢尾花数据,kernel=linear,gamma=scale: 99.17% SVC_鸢尾花数据,kernel=linear,gamma=scale: 100.00% SVC_鸢尾花数据,kernel=linear,gamma=auto: 99.17% SVC_鸢尾花数据,kernel=linear,gamma=auto: 100.00% SVC_鸢尾花数据,kernel=linear,gamma=0.1: 99.17% SVC_鸢尾花数据,kernel=linear,gamma=0.1: 100.00% SVC_鸢尾花数据,kernel=linear,gamma=0.01: 99.17% SVC_鸢尾花数据,kernel=linear,gamma=0.01: 100.00% SVC_鸢尾花数据,kernel=linear,gamma=0.001: 99.17% SVC_鸢尾花数据,kernel=linear,gamma=0.001: 96.67% SVC_鸢尾花数据,kernel=rbf,gamma=scale: 96.67% SVC_鸢尾花数据,kernel=rbf,gamma=scale: 96.67% SVC_鸢尾花数据,kernel=rbf,gamma=auto: 97.50% SVC_鸢尾花数据,kernel=rbf,gamma=auto: 96.67% SVC_鸢尾花数据,kernel=rbf,gamma=0.1: 97.50% SVC_鸢尾花数据,kernel=rbf,gamma=0.1: 96.67% SVC_鸢尾花数据,kernel=rbf,gamma=0.01: 95.00% SVC_鸢尾花数据,kernel=rbf,gamma=0.01: 86.67% SVC_鸢尾花数据,kernel=rbf,gamma=0.001: 70.00% SVC_鸢尾花数据,kernel=rbf,gamma=0.001: 53.33% SVC_鸢尾花数据,kernel=sigmoid,gamma=scale: 6.67% SVC_鸢尾花数据,kernel=sigmoid,gamma=scale: 10.00% SVC_鸢尾花数据,kernel=sigmoid,gamma=auto: 4.17% SVC_鸢尾花数据,kernel=sigmoid,gamma=auto: 3.33% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.1: 5.83% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.1: 6.67% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.01: 70.00% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.01: 53.33% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.001: 70.00% SVC_鸢尾花数据,kernel=sigmoid,gamma=0.001: 53.33% SVC_鸢尾花数据,kernel=poly,gamma=scale: 98.33% SVC_鸢尾花数据,kernel=poly,gamma=scale: 93.33% SVC_鸢尾花数据,kernel=poly,gamma=auto: 99.17% SVC_鸢尾花数据,kernel=poly,gamma=auto: 93.33% SVC_鸢尾花数据,kernel=poly,gamma=0.1: 97.50% SVC_鸢尾花数据,kernel=poly,gamma=0.1: 93.33% SVC_鸢尾花数据,kernel=poly,gamma=0.01: 89.17% SVC_鸢尾花数据,kernel=poly,gamma=0.01: 86.67% SVC_鸢尾花数据,kernel=poly,gamma=0.001: 57.50% SVC_鸢尾花数据,kernel=poly,gamma=0.001: 50.00%
比较差
很差
很差
很差
很差
很差。可见Kernel=sigmoid基本上都很差。
很差
很差
很差
很差
】