from sklearn.naive_bayes import BernoulliNB,MultinomialNB from sklearn.feature_extraction.text import CountVectorizer from sklearn.model_selection import train_test_split import pandas as pd path = 'E:/Python_file/zuoye/SMSSpamCollection.txt' Cnames=['labels','messages'] data = pd.read_csv(path,sep='\t', header=None, names=Cnames) #读取数据集,分隔符是\t data=data.replace({'ham':0,'spam':1}) #替换标签值 print('数据集展示:') print(data) print('\n----------------------------------\n') X=data['messages'] y=data['labels'] x_train,x_test,y_train,y_test=train_test_split(X,y,train_size=0.8,random_state=123) vector_nomial=CountVectorizer() #实现词袋模型 vector_bernou=CountVectorizer() #多项式模型分类垃圾短信 train_matrix=vector_nomial.fit_transform(x_train) test_matrix=vector_nomial.transform(x_test) polynomial=MultinomialNB() clm_nomial=polynomial.fit(train_matrix,y_train) result_nomial=clm_nomial.predict(test_matrix) #伯努利模型分类垃圾短信 train_matrix=vector_bernou.fit_transform(x_train) test_matrix=vector_bernou.transform(x_test) Bernoulli=BernoulliNB() clm_bernoulli=Bernoulli.fit(train_matrix,y_train) result_bernou=clm_bernoulli.predict(test_matrix) print('多项式模型的预测结果,类型,长度:') print(result_nomial,type(result_nomial),result_nomial.shape) print('多项式模型的前一百个预测结果:') print(result_nomial[0:100]) print('多项式模型模型R²评分:'+ str(clm_nomial.score(test_matrix,y_test))) print('\n----------------------------------\n') print('伯努利模型的预测结果,类型,长度:') print(result_bernou,type(result_bernou),result_bernou.shape) print('伯努利模型的前一百个预测结果:') print(result_bernou[0:100]) print('伯努利模型R²评分:'+ str(clm_bernoulli.score(test_matrix,y_test)))
时间: 2024-02-17 12:04:20 浏览: 125
这段代码是一个简单的垃圾短信分类器的实现。具体来说,它使用了朴素贝叶斯算法中的多项式模型和伯努利模型来分类短信为垃圾短信或非垃圾短信。以下是代码的详细解释:
1. 导入必要的库和模块:
```python
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
import pandas as pd
```
2. 读取数据集,并将标签值替换为0或1:
```python
path = 'E:/Python_file/zuoye/SMSSpamCollection.txt'
Cnames=['labels','messages']
data = pd.read_csv(path, sep='\t', header=None, names=Cnames)
data = data.replace({'ham':0, 'spam':1})
```
3. 划分训练集和测试集:
```python
X = data['messages']
y = data['labels']
x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=123)
```
4. 实现词袋模型和多项式模型:
```python
vector_nomial = CountVectorizer() # 实现词袋模型
vector_bernou = CountVectorizer() # 多项式模型
```
5. 使用多项式模型分类垃圾短信:
```python
train_matrix = vector_nomial.fit_transform(x_train)
test_matrix = vector_nomial.transform(x_test)
polynomial = MultinomialNB()
clm_nomial = polynomial.fit(train_matrix, y_train)
result_nomial = clm_nomial.predict(test_matrix)
```
6. 使用伯努利模型分类垃圾短信:
```python
train_matrix = vector_bernou.fit_transform(x_train)
test_matrix = vector_bernou.transform(x_test)
Bernoulli = BernoulliNB()
clm_bernoulli = Bernoulli.fit(train_matrix, y_train)
result_bernou = clm_bernoulli.predict(test_matrix)
```
7. 输出结果:
```python
print('多项式模型的预测结果,类型,长度:')
print(result_nomial, type(result_nomial), result_nomial.shape)
print('多项式模型的前一百个预测结果:')
print(result_nomial[0:100])
print('多项式模型模型R²评分:'+ str(clm_nomial.score(test_matrix, y_test)))
print('\n----------------------------------\n')
print('伯努利模型的预测结果,类型,长度:')
print(result_bernou, type(result_bernou), result_bernou.shape)
print('伯努利模型的前一百个预测结果:')
print(result_bernou[0:100])
print('伯努利模型R²评分:'+ str(clm_bernoulli.score(test_matrix, y_test)))
```
其中,输出结果包括模型的预测结果、类型、长度、前一百个预测结果以及模型的R²评分。
阅读全文