---
title: "ChatGPT与支持向量机：AI文本分类的双重助力"
canonical: "https://96p.net/articles/gpt/41435/ChatGPT%E4%B8%8E%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F%E6%9C%BA%EF%BC%9AAI%E6%96%87%E6%9C%AC%E5%88%86%E7%B1%BB%E7%9A%84%E5%8F%8C%E9%87%8D%E5%8A%A9%E5%8A%9B"
---

# ChatGPT与支持向量机：AI文本分类的双重助力

> 发布时间：2024-01-29
> 栏目：GPT

<section style="padding-right: 10px; padding-left: 10px; text-align: left; line-height: 1.6; letter-spacing: 0.034em; color: #3f3f3f; font-size: 16px; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; margin-bottom: 24px;" data-website="https://www.mdnice.com">
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">大家好，我是了不起，一名热衷于用算法解决实际问题的奔三程序员！</p>
<p style="margin-bottom: 15px; font-weight: bold; color: black; font-size: 20px; margin-top: 1.2em;"></p>

<section style="margin-bottom: 0px; padding-left: 10px; padding-right: 10px; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.5em; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; text-align: left;" data-website="https://www.mdnice.com">
<h2 style="margin-top: 30px; margin-bottom: 15px; background-attachment: scroll; background-clip: border-box; background-image: url('https://www.96p.net/bizimg/mmbiz.qpic.cn/mmbiz_png/snVNX7xXtfp2yYOcp0ics3Dh2RLAxfna6raTSBlZNyy4uLjJnRTe40YO9N6vsenNZicGnbcmEgg7PPEJdXXbXe3A/640?wx_fmt=png&amp;from=appmsg'); background-origin: padding-box; background-position: 50% 50%; background-repeat: no-repeat; background-size: 63px; width: auto; height: auto; align-items: unset; border-radius: 0px; box-shadow: none; display: flex; flex-direction: unset; float: unset; justify-content: center; line-height: 1.5em; overflow: unset; text-shadow: none; transform: none; -webkit-box-reflect: unset; border: 1px none #000000;"><span style="font-size: 18px; color: #48b378; line-height: 2.4em; letter-spacing: 0em; margin-top: 38px; margin-bottom: 10px; border-radius: 0px; align-items: unset; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: 38px; justify-content: unset; overflow: unset; text-align: center; text-indent: 0em; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border: 1px none #000000;">前言</span></h2>
</section>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">今天我们要聊一聊支持向量机算法。这是一种强大的机器学习算法，广泛应用于文本分类、图像识别等领域。下面，让我们一起揭开支持向量机的神秘面纱。</p>

<section style="margin-bottom: 0px; padding-left: 10px; padding-right: 10px; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.5em; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; text-align: left;" data-website="https://www.mdnice.com">
<h2 style="margin-top: 30px; margin-bottom: 15px; background-attachment: scroll; background-clip: border-box; background-image: url('https://www.96p.net/bizimg/mmbiz.qpic.cn/mmbiz_png/snVNX7xXtfp2yYOcp0ics3Dh2RLAxfna6raTSBlZNyy4uLjJnRTe40YO9N6vsenNZicGnbcmEgg7PPEJdXXbXe3A/640?wx_fmt=png&amp;from=appmsg'); background-origin: padding-box; background-position: 50% 50%; background-repeat: no-repeat; background-size: 63px; width: auto; height: auto; align-items: unset; border-radius: 0px; box-shadow: none; display: flex; flex-direction: unset; float: unset; justify-content: center; line-height: 1.5em; overflow: unset; text-shadow: none; transform: none; -webkit-box-reflect: unset; border: 1px none #000000;"><span style="font-size: 18px; color: #48b378; line-height: 2.4em; letter-spacing: 0em; margin-top: 38px; margin-bottom: 10px; border-radius: 0px; align-items: unset; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: 38px; justify-content: unset; overflow: unset; text-align: center; text-indent: 0em; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border: 1px none #000000;">支持向量机算法的基本原理</span></h2>
</section>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">支持向量机（SVM）的核心思想是找到一个超平面，将不同类别的数据点尽可能分开。超平面就是那些能够将数据点分隔开的平面。SVM通过最大化两个类别之间的间隔来实现这一目标。</p>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">为了解决非线性问题，SVM引入了核函数的概念。核函数可以将数据点从原始空间映射到高维特征空间，在这个高维空间中，数据点更容易被线性分开。常见的核函数有线性核、多项式核和径向基函数等。</p>

<section style="margin-bottom: 0px; padding-left: 10px; padding-right: 10px; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.5em; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; text-align: left;" data-website="https://www.mdnice.com">
<h2 style="margin-top: 30px; margin-bottom: 15px; background-attachment: scroll; background-clip: border-box; background-image: url('https://www.96p.net/bizimg/mmbiz.qpic.cn/mmbiz_png/snVNX7xXtfp2yYOcp0ics3Dh2RLAxfna6raTSBlZNyy4uLjJnRTe40YO9N6vsenNZicGnbcmEgg7PPEJdXXbXe3A/640?wx_fmt=png&amp;from=appmsg'); background-origin: padding-box; background-position: 50% 50%; background-repeat: no-repeat; background-size: 63px; width: auto; height: auto; align-items: unset; border-radius: 0px; box-shadow: none; display: flex; flex-direction: unset; float: unset; justify-content: center; line-height: 1.5em; overflow: unset; text-shadow: none; transform: none; -webkit-box-reflect: unset; border: 1px none #000000;"><span style="font-size: 18px; color: #48b378; line-height: 2.4em; letter-spacing: 0em; margin-top: 38px; margin-bottom: 10px; border-radius: 0px; align-items: unset; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: 38px; justify-content: unset; overflow: unset; text-align: center; text-indent: 0em; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border: 1px none #000000;">支持向量机的应用场景</span></h2>
</section>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">1、文本分类：SVM可以用于文本分类，如垃圾邮件识别、情感分析等。通过提取文本特征，SVM可以自动将文本分为不同的类别。</p>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">2、图像识别：SVM可以用于图像识别，如人脸识别、物体检测等。通过提取图像特征，SVM可以区分不同的人脸或物体。</p>

<section style="margin-bottom: 0px; padding-left: 10px; padding-right: 10px; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.5em; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; text-align: left;" data-website="https://www.mdnice.com">
<h2 style="margin-top: 30px; margin-bottom: 15px; background-attachment: scroll; background-clip: border-box; background-image: url('https://www.96p.net/bizimg/mmbiz.qpic.cn/mmbiz_png/snVNX7xXtfp2yYOcp0ics3Dh2RLAxfna6raTSBlZNyy4uLjJnRTe40YO9N6vsenNZicGnbcmEgg7PPEJdXXbXe3A/640?wx_fmt=png&amp;from=appmsg'); background-origin: padding-box; background-position: 50% 50%; background-repeat: no-repeat; background-size: 63px; width: auto; height: auto; align-items: unset; border-radius: 0px; box-shadow: none; display: flex; flex-direction: unset; float: unset; justify-content: center; line-height: 1.5em; overflow: unset; text-shadow: none; transform: none; -webkit-box-reflect: unset; border: 1px none #000000;"><span style="font-size: 18px; color: #48b378; line-height: 2.4em; letter-spacing: 0em; margin-top: 38px; margin-bottom: 10px; border-radius: 0px; align-items: unset; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: 38px; justify-content: unset; overflow: unset; text-align: center; text-indent: 0em; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border: 1px none #000000;">支持向量机的操作步骤</span></h2>
</section>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">1、数据预处理：对数据进行预处理，包括数据清洗、特征提取等。例如，对于文本分类，需要将文本转换为数值向量；对于图像识别，需要提取图像的特征。</p>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">2、模型训练：使用训练数据集训练SVM模型。在这个过程中，SVM会找到一个最优的超平面，使得不同类别的数据点尽可能分开。</p>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">3、模型测试：使用测试数据集对模型进行测试，评估模型的性能。如果模型的性能不满意，可以调整参数或尝试其他算法。</p>

<section style="margin-bottom: 0px; padding-left: 10px; padding-right: 10px; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; width: auto; font-family: Optima, 'Microsoft YaHei', PingFangSC-regular, serif; font-size: 16px; color: #000000; line-height: 1.5em; word-spacing: 0em; letter-spacing: 0em; word-break: break-word; text-align: left;" data-website="https://www.mdnice.com">
<h2 style="margin-top: 30px; margin-bottom: 15px; background-attachment: scroll; background-clip: border-box; background-image: url('https://www.96p.net/bizimg/mmbiz.qpic.cn/mmbiz_png/snVNX7xXtfp2yYOcp0ics3Dh2RLAxfna6raTSBlZNyy4uLjJnRTe40YO9N6vsenNZicGnbcmEgg7PPEJdXXbXe3A/640?wx_fmt=png&amp;from=appmsg'); background-origin: padding-box; background-position: 50% 50%; background-repeat: no-repeat; background-size: 63px; width: auto; height: auto; align-items: unset; border-radius: 0px; box-shadow: none; display: flex; flex-direction: unset; float: unset; justify-content: center; line-height: 1.5em; overflow: unset; text-shadow: none; transform: none; -webkit-box-reflect: unset; border: 1px none #000000;"><span style="font-size: 18px; color: #48b378; line-height: 2.4em; letter-spacing: 0em; margin-top: 38px; margin-bottom: 10px; border-radius: 0px; align-items: unset; background-attachment: scroll; background-clip: border-box; background-image: none; background-origin: padding-box; background-position: 0% 0%; background-repeat: no-repeat; background-size: auto; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: 38px; justify-content: unset; overflow: unset; text-align: center; text-indent: 0em; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border: 1px none #000000;">实践示例</span></h2>
</section>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">下面我们用一个简单的例子来说明如何使用SVM进行文本分类。假设我们有一组文本数据，其中一部分是垃圾邮件，另一部分是正常邮件。我们的任务是编写一个程序来自动区分垃圾邮件和正常邮件。</p>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">首先我们需要将文本数据转换为数值向量。这可以通过TF-IDF等方法实现。TF-IDF是一种统计方法，用于评估一个词语对于一个文件或一个语料库中的一份文件的重要程度。对于每个词语，我们计算其词频（TF）和逆文档频率（IDF）。这样，每个文本都可以表示为一个数值向量。</p>

<pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd; line-height: 26px;">from</span> sklearn.feature_extraction.text <span style="color: #c678dd; line-height: 26px;">import</span> TfidfVectorizer  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 示例文本数据  </span>
text_data = [<span style="color: #98c379; line-height: 26px;">"This is a spam email"</span>, <span style="color: #98c379; line-height: 26px;">"This is a normal email"</span>]  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 创建TF-IDF向量化器对象  </span>
vectorizer = TfidfVectorizer()  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 将文本数据转换为数值向量  </span>
numerical_data = vectorizer.fit_transform(text_data)  
print(numerical_data.toarray())
</code></pre>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">接下来我们使用训练数据集来训练SVM模型。在这个过程中，SVM会找到一个最优的超平面，使得垃圾邮件和正常邮件尽可能分开。我们可以使用线性核函数或多项式核函数等来定义这个超平面。</p>

<pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd; line-height: 26px;">from</span> sklearn <span style="color: #c678dd; line-height: 26px;">import</span> svm  
<span style="color: #c678dd; line-height: 26px;">from</span> sklearn.model_selection <span style="color: #c678dd; line-height: 26px;">import</span> train_test_split  
<span style="color: #c678dd; line-height: 26px;">from</span> sklearn.metrics <span style="color: #c678dd; line-height: 26px;">import</span> accuracy_score  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 创建SVM分类器对象  </span>
clf = svm.SVC(kernel=<span style="color: #98c379; line-height: 26px;">'linear'</span>) <span style="color: #5c6370; font-style: italic; line-height: 26px;"># 使用线性核函数作为示例  </span>
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 划分训练集和测试集  </span>
X_train, X_test, y_train, y_test = train_test_split(numerical_data, [<span style="color: #98c379; line-height: 26px;">'spam'</span>, <span style="color: #98c379; line-height: 26px;">'normal'</span>], test_size=<span style="color: #d19a66; line-height: 26px;">0.2</span>, random_state=<span style="color: #d19a66; line-height: 26px;">42</span>)  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 使用训练集训练模型  </span>
clf.fit(X_train, y_train)  
  
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 使用测试集进行预测  </span>
y_pred = clf.predict(X_test)  
print(y_pred) <span style="color: #5c6370; font-style: italic; line-height: 26px;"># 输出预测结果</span>
</code></pre>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">最后我们使用测试数据集对模型进行测试。我们将一些新的文本输入到模型中，模型会输出这些文本是垃圾邮件还是正常邮件的预测结果。我们可以计算模型的准确率来评估其性能。</p>

<pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #5c6370; font-style: italic; line-height: 26px;"># 示例新文本数据  </span>
new_text = [<span style="color: #98c379; line-height: 26px;">"This is another normal email"</span>]  
new_numerical = vectorizer.transform(new_text) <span style="color: #5c6370; font-style: italic; line-height: 26px;"># 将新文本转换为数值向量  </span>
print(new_numerical.toarray()) <span style="color: #5c6370; font-style: italic; line-height: 26px;"># 输出新文本的数值向量表示形式</span>
<span style="color: #5c6370; font-style: italic; line-height: 26px;"># 使用模型进行预测  </span>
predictions = clf.predict(new_numerical)  
print(predictions) <span style="color: #5c6370; font-style: italic; line-height: 26px;"># 输出预测结果</span>
</code></pre>
<p style="padding-bottom: 8px; padding-top: 1em; color: #4a4a4a; line-height: 1.75em;">通过以上代码示例，我们可以看到如何将文本数据转换为数值向量、如何使用SVM进行文本分类以及如何评估模型的性能。这只是SVM的基本应用之一，如果你对SVM有更深入的学习兴趣，可以阅读相关学术论文、博客文章或教程来了解更多细节和高级应用。</p>

<section style="margin-bottom: 0px; outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-wrap: wrap; font-size: 16px; background-color: #ffffff; text-align: right; color: #a3a3a3 !important;" data-style="outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; letter-spacing: 0.544px; white-space: normal; background-color: rgb(255, 255, 255); text-align: right;"></section>
</section>
<p style="display: none;"></p>
