數(shù)據(jù)集:https://archive.ics.uci.edu/ml/datasets/Adult
這個(gè)數(shù)據(jù)集是字符數(shù)據(jù)集,你需要自己進(jìn)行清洗數(shù)據(jù)轉(zhuǎn)換數(shù)據(jù)之類的,好在python的字符串操作特別簡單,好處理。特征取了學(xué)位情況、工作職業(yè)性質(zhì)、工作單位之類的。
樸素貝葉斯:https://www.bilibili.com/video/av36338359?from=search&seid=1177086802297258225
?
這個(gè)的數(shù)據(jù)集是預(yù)測一個(gè)人的年薪是否能超過50k美元/年。
具體的數(shù)據(jù)特征請看下網(wǎng)站的解釋:
由于是2分類,所以思路非常簡單。實(shí)際上只需要求出一類的概率即可。
下面代碼中的ConvertFeature是來轉(zhuǎn)換字符型的數(shù)據(jù)到數(shù)字類型的數(shù)據(jù)的,但是我在main函數(shù)里面直接用了數(shù)字型的,所以實(shí)際上沒有用到。
注意要改一下數(shù)據(jù)集的路徑。
import torch
import torch.nn.modules
import torch.nn
import numpy as np
from torch.autograd import Variable #torch的基本變量
import torch.nn.functional as F #里面有很多torch的函數(shù)
import matplotlib.pyplot as plt
class solution:
def __init__(self):
self.RawData=[]
self.Data=[]
self.GetClass={}
self.NUMOFFEATURE=0
self.NUMOFDATA=0
def LoadData(self,PATH):
with open(PATH,'r') as f:
for line in f:
self.RawData.append([thing.strip() for thing in line.split(',')])
self.RawData=self.RawData[:-1]
self.NUMOFDATA=len(self.RawData)
def ShowRawData(self):
for item in self.RawData:
print(item)
def WashData(self):
attribute=len(self.RawData[0])-1
#print("attribute: {}".format(attribute))
index=0
while index
P2: return 1#大于50k
elif P1
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
