#!/usr/bin/python
# coding=utf-8
from numpy import *
# 過濾網站的惡意留言 侮辱性:1 非侮辱性:0
# 創建一個實驗樣本
def loadDataSet():
postingList = [[‘my’,‘dog’,‘has’,‘flea’,‘problems’,‘help’,‘please’],
[‘maybe’,‘not’,‘take’,‘him’,‘to’,‘dog’,‘park’,‘stupid’],
[‘my’,‘dalmation’,‘is’,‘so’,‘cute’,‘I’,‘love’,‘him’],
[‘stop’,‘posting’,‘stupid’,‘worthless’,‘garbage’],
[‘mr’,‘licks’,‘ate’,‘my’,‘steak’,‘how’,‘to’,‘stop’,‘him’],
[‘quit’,‘buying’,‘worthless’,‘dog’,‘food’,‘stupid’]]
classVec = [0,1,0,1,0,1]
return postingList, classVec
# 創建一個包含在所有檔案中出現的不重復詞的串列
def createVocabList(dataSet):
vocabSet = set([]) # 創建一個空集
for document in dataSet:
vocabSet = vocabSet | set(document) # 創建兩個集合的并集
return list(vocabSet)
# 將檔案詞條轉換成詞向量
def setOfWords2Vec(vocabList, inputSet):
returnVec = [0]*len(vocabList) # 創建一個其中所含元素都為0的向量
for word in inputSet:
if word in vocabList:
# returnVec[vocabList.index(word)] = 1 # index函式在字串里找到字符第一次出現的位置 詞集模型
returnVec[vocabList.index(word)] += 1 # 檔案的詞袋模型 每個單詞可以出現多次
else: print (“the word: %s is not in my Vocabulary!” % word)
return returnVec
uj5u.com熱心網友回復:
筆記請放到個從博客, 謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/103310.html
標籤:非技術版
上一篇:樸素貝葉斯
下一篇:查找
