十。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()

相关推荐
IT_陈寒1 天前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
逛逛GitHub1 天前
飞书多维表“独立”了!功能强大的超出想象。
人工智能·github·产品
机器之心1 天前
刚刚,DeepSeek-R1论文登上Nature封面,通讯作者梁文锋
人工智能·openai
aneasystone本尊1 天前
学习 Chat2Graph 的知识库服务
人工智能
IT_陈寒1 天前
Redis 性能翻倍的 7 个冷门技巧,第 5 个大多数人都不知道!
前端·人工智能·后端
飞哥数智坊2 天前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三2 天前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯2 天前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet2 天前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算2 天前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源