十。svm运用

import numpy as np

import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

from sklearn.svm import SVC

创建自定义数据集

np.random.seed(42)

X, y = datasets.make_blobs(n_samples=100, centers=2, random_state=42)

可选:添加一些噪声

X = X + np.random.randn(100, 2) * 0.1

数据标准化

scaler = StandardScaler()

X = scaler.fit_transform(X)

划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

使用 SVM 进行分类

svm = SVC(kernel='linear')

svm.fit(X_train, y_train)

预测测试集

y_pred = svm.predict(X_test)

可视化结果

plt.scatter(X_train:, 0, X_train:, 1, c=y_train, cmap='viridis')

plt.scatter(X_test:, 0, X_test:, 1, c=y_pred, cmap='viridis', alpha=0.5)

plt.title('SVM 分类结果')

plt.xlabel('特征1')

plt.ylabel('特征2')

plt.show()

相关推荐
王中阳Go4 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
海宇AI4 小时前
零信任架构实战:基于海宇个人风险报告构建自动化分期商城信用额度初审网关
运维·人工智能·架构·自动化
tellmewhoisi4 小时前
深度学习32:卷积神经网络:卷积层(卷积运算,填充,步幅)
人工智能·深度学习·cnn
weixin_307779134 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
Sinokap5 小时前
OpenAI 披露 AI 安全实验:智能体如何突破隔离边界?
人工智能·安全
云和数据.ChenGuang5 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
重生之我在番茄自学网安拯救世界5 小时前
AI 开发者日报 第1期(本周精选)| 插件市场投毒、智能体搞出“官僚饥荒村”,十年前的 CNN 打赢大模型
人工智能·网络安全·大模型·ai编程·智能体
涛声依旧-底层原理研究所6 小时前
拆解 Cordis 底座:Agent 是如何被「组合」出来的
人工智能·架构
秦先生在广东6 小时前
记忆不再是黑盒
人工智能
秦先生在广东6 小时前
49个AI智能体接管你的游戏开发
人工智能