【数据挖掘】鸢尾花数据集实现逻辑回归分类

博主采用的是sigmoid核函数
具体代码如下:



import numpy as np

from matplotlib import pyplot as plt

from sklearn.svm import SVC

from sklearn.datasets import load_iris

data = load_iris()

X = data['data']

y = data['target']

print(X)

print(y)

X = X[y != 2, 0:2]

y = y[y != 2]

X -= np.mean(X, axis=0)

X /= np.std(X, axis=0, ddof=1)

m = len(X)

d = int(0.8 * m)

X_train, X_test = np.split(X, [d])

y_train, y_test = np.split(y, [d])

model_svm = SVC(C=1, kernel='sigmoid'

           
,gamma="auto"

            ,degree=10

            ,cache_size=5000)

model_svm.fit(X_train, y_train)

ss = model_svm.score(X_test, y_test)

print('测试集的准确率是:', ss)

X_train_h = model_svm.predict(X_train)

X_test_h = model_svm.predict(X_test)

x1_min, x1_max, x2_min, x2_max = np.min(X[:, 0]), np.max(X[:, 0]),
np.min(X[:, 1]), np.max(X[:, 1])

x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]

x1x2 = np.c_[x1.ravel(), x2.ravel()]

z = model_svm.decision_function(x1x2)

z = z.reshape(x1.shape)

plt.scatter(X[:, 0], X[:, 1], c=y, zorder=10)

plt.scatter(X_train[:, 0], X_train[:, 1], s=100, facecolor='none',
zorder=10, edgecolors='k')

plt.contourf(x1, x2, z >= 0, cmap=plt.cm.Paired)

plt.contour(x1, x2, z, levels=[-1, 0, 1])

plt.show()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值