自定义数据集 使用scikit-learn中svm的包实现svm分类

引入必要的库

复制代码
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report

生成自定义数据集

复制代码
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=0, random_state=42)

划分训练集和数据集

复制代码
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=50)

创建并训练 SVM 分类器

复制代码
# 创建 SVM 分类器实例,使用径向基函数(RBF)作为核函数
clf = SVC(kernel='rbf', random_state=50)
# 使用训练集对模型进行训练
clf.fit(X_train, y_train)

模型预测与评估

复制代码
# 使用训练好的模型对测试集进行预测
y_pred = clf.predict(X_test)
# 计算模型在测试集上的准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"模型准确率: {accuracy * 100:.2f}%")

# 打印详细的分类报告,包含精确率、召回率、F1 值等信息
print("分类报告:")
print(classification_report(y_test, y_pred))

结果展示

相关推荐
骇客野人6 小时前
AI全栈开发指定技术架构
人工智能
Escape6 小时前
为什么你的 AI 越聊越傻?从 Token 到 Agent,彻底搞懂 AI Agent的秘密㊙️
前端·人工智能·后端
2501_926978336 小时前
认知边缘的双向耦合:从核技术类比到智力货币时代
人工智能·经验分享·笔记·ai写作
SQDN7 小时前
Chatbox 1.22.1 获取不到模型?先验 /models,再对齐精确 model ID
人工智能·测试工具·机器学习·chatgpt·json
颜酱7 小时前
08 | 把维度值同步到 Elasticsearch(生成阶段)
人工智能·python·langchain
小马哥crazymxm7 小时前
Arxiv论文周选 (2026-W28)
论文阅读·人工智能·科技
ASKED_20197 小时前
一文阐述国内网络环境下使用NIM方法
人工智能·系统架构
GlobalHRTalk7 小时前
埃及名义雇主EOR业务概述与市场发展优势分析
大数据·运维·人工智能
董员外7 小时前
RAG 系统进化论(二):Naive RAG,检索增强生成的最小闭环
前端·人工智能·后端
Zeeland7 小时前
Agent 能完成一个任务,但它能持续追一个三个月的目标吗?
人工智能·github·openai