最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

人工非智能作業(yè)4和5

2023-04-04 17:52 作者:362的小妖怪  | 我要投稿

實(shí)驗(yàn)5

from collections import defaultdict
import math
#定義貝葉斯分類器
class NaiveBayes:
? ?def __init__(self):
? ? ? ?#初始化類別字典
? ? ? ?self.classes = {}
? ? ? ?#初始化特征和類別的字典
? ? ? ?self.freqs = defaultdict(lambda: defaultdict(int))
? ? ? ?#初始化計(jì)數(shù)字典
? ? ? ?self.total = defaultdict(int)
#訓(xùn)練模型
? ?def train(self, data):
? ? ? ?for features, label in data:
? ? ? ? ? ?#類別不在字典。添加新的類別
? ? ? ? ? ?if label not in self.classes:
? ? ? ? ? ? ? ?self.classes[label] = 0
? ? ? ? ? ?#對(duì)當(dāng)前類別計(jì)數(shù)
? ? ? ? ? ?self.classes[label] += 1
? ? ? ? ? ?#對(duì)當(dāng)前特征在當(dāng)前類別下出現(xiàn)次數(shù)進(jìn)行計(jì)數(shù)
? ? ? ? ? ?for feature in features:
? ? ? ? ? ? ? ?self.freqs[label][feature] += 1
? ? ? ? ? ? ? ?#統(tǒng)計(jì)當(dāng)前類別下的總特征數(shù)
? ? ? ? ? ? ? ?self.total[label] += 1
#預(yù)測(cè)類別
? ?def predict(self, features):
? ? ? ?#初始化概率字典
? ? ? ?probs = {}
? ? ? ?for label in self.classes:
? ? ? ? ? ?class_prob = float(self.classes[label]) / sum(self.classes.values())
? ? ? ? ? ?feature_probs = []
? ? ? ? ? ?for feature in features:
? ? ? ? ? ? ? ?feature_prob = (self.freqs[label][feature] + 1) / (self.total[label] + len(features))
? ? ? ? ? ? ? ?feature_probs.append(feature_prob)
? ? ? ? ? ?#對(duì)條件概率取對(duì)數(shù)
? ? ? ? ? ?probs[label] = math.log(class_prob) + sum([math.log(fp) for fp in feature_probs])
? ? ? ?return max(probs, key=probs.get)
#原始數(shù)據(jù)集
data = [
? ?["shuai", "hao", "shang", "jia"],
? ?["bushuai", "hao", "yiban", "bujia"],
? ?["bushuai", "buhao", "bu", "bujia"],
? ?["shuai", "hao", "yiban", "jia"],
? ?["bushuai", "hao", "shangjin", "jia"],
? ?["shuai", "buhao", "yiban", "bujia"],
? ?["shuai", "hao", "bu", "jia"],
? ?["bushuai", "buhao", "shangjin", "bujia"],
? ?["shuai", "buhao", "shangjin", "jia"],
? ?["bushuai", "hao", "bu", "bujia"]
]
#將原始數(shù)據(jù)集轉(zhuǎn)換為可以輸入的貝葉斯訓(xùn)練集
train_data = []
for features in data:
? ?if features[0] == "shuai":
? ? ? ?f1 = "帥"
? ?else:
? ? ? ?f1 = "不帥"
? ?if features[1] == "hao":
? ? ? ?f2 = "性格好"
? ?else:
? ? ? ?f2 = "性格不好"
? ?if features[2] == "shangjin":
? ? ? ?f3 = "上進(jìn)"
? ?elif features[2] == "bu":
? ? ? ?f3 = "不上進(jìn)"
? ?else:
? ? ? ?f3 = "一般"
? ?train_data.append(([f1, f2, f3], int(features[3] == "jia")))

model = NaiveBayes()
model.train(train_data)
features = ["帥", "不上進(jìn)", "性格不好"]
label = model.predict(features)
if label == 1:
? ?print("女青年會(huì)同意求婚")
else:
? ?print("女青年不會(huì)同意求婚")

實(shí)驗(yàn)6 ,方案一

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
def plot_hyperplane(clf,X,y,h=0.2,draw_sv=True,title='hyperplan'):
? ?x_min,x_max=X[:,0].min()-1,X[:,0].max()+1
? ?y_min,y_max=X[:,1].min()-1,X[:,1].max()+1
? ?xx,yy=np.meshgrid(np.arange(x_min,x_max,h),
? ? ? ? ? ? ? ? ? ? ?np.arange(y_min,y_max,h))
? ?plt.title(title)
? ?plt.xlim(xx.min(),xx.max())
? ?plt.ylim(yy.min(),yy.max())
? ?plt.xticks(())
? ?plt.yticks(())
? ?Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
? ?Z=Z.reshape(xx.shape)
? ?plt.contourf(xx,yy,Z,cmap='hot',alpha=0.5)
? ?if draw_sv:
? ? ? ?sv=clf.support_vectors_
? ? ? ?plt.scatter(sv[:,0],sv[:,1],c='y',marker='x')
X,y=make_blobs(n_samples=100,centers=3,n_features=2,random_state=0,cluster_std=0.8)
這四個(gè)是核函數(shù),此程序每個(gè)人都一樣,接下來(lái)的兩個(gè)程序最好每個(gè)人的值都不一樣
clf_linear=svm.SVC(C=1.0,kernel='linear')
clf_poly=svm.SVC(C=1.0,kernel='poly',degree=3)
clf_rbf=svm.SVC(C=1.0,kernel='rbf',gamma=0.6)
clf_rbf2=svm.SVC(C=1.0,kernel='rbf')


plt.figure(figsize=(8,8),dpi=144)
clfs=[clf_linear,clf_poly,clf_rbf,clf_rbf2]
titles=['Linear Kernel',
? ? ? ?'Polynomail Kernel with Degree=3',
? ? ? ?'Gaussian Kernel with $\gamma=0.6$',
? ? ? ?'Gaussian Kernel with $\gamma=default$']
for clf,i in zip(clfs,range(len(clfs))):
? ?clf.fit(X,y)
? ?plt.subplot(2,2,i+1)
? ?plot_hyperplane(clf,X,y,title=titles[i])
plt.show()


方案二

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
def plot_hyperplane(clf,X,y,h=0.2,draw_sv=True,title='hyperplan'):
? ?x_min,x_max=X[:,0].min()-1,X[:,0].max()+1
? ?y_min,y_max=X[:,1].min()-1,X[:,1].max()+1
? ?xx,yy=np.meshgrid(np.arange(x_min,x_max,h),
? ? ? ? ? ? ? ? ? ? ?np.arange(y_min,y_max,h))
? ?plt.title(title)
? ?plt.xlim(xx.min(),xx.max())
? ?plt.ylim(yy.min(),yy.max())
? ?plt.xticks(())
? ?plt.yticks(())
? ?Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
? ?Z=Z.reshape(xx.shape)
? ?plt.contourf(xx,yy,Z,cmap='hot',alpha=0.5)
? ?if draw_sv:
? ? ? ?sv=clf.support_vectors_
? ? ? ?plt.scatter(sv[:,0],sv[:,1],c='y',marker='x')
X,y=make_blobs(n_samples=100,centers=3,n_features=2,random_state=0,cluster_std=0.8)
改C的值就是每個(gè)人隨便輸一個(gè)數(shù)就行,degree,gamma也改
clf_linear=svm.SVC(C=2.0,kernel='linear')
clf_poly=svm.SVC(C=2.0,kernel='poly',degree=6)
clf_rbf=svm.SVC(C=2.0,kernel='rbf',gamma=2.0)
clf_rbf2=svm.SVC(C=2.0,kernel='rbf')

plt.figure(figsize=(8,8),dpi=144)
clfs=[clf_linear,clf_poly,clf_rbf,clf_rbf2]
titles改成對(duì)應(yīng)的值
titles=['Linear Kernel',
? ? ? ?'Polynomail Kernel with Degree=6',
? ? ? ?'Gaussian Kernel with $\gamma=2.0$',
? ? ? ?'Gaussian Kernel with $\gamma=default$']
for clf,i in zip(clfs,range(len(clfs))):
? ?clf.fit(X,y)
? ?plt.subplot(2,2,i+1)
? ?plot_hyperplane(clf,X,y,title=titles[i])
plt.show()


方案三

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
def plot_hyperplane(clf,X,y,h=0.2,draw_sv=True,title='hyperplan'):
? ?x_min,x_max=X[:,0].min()-1,X[:,0].max()+1
? ?y_min,y_max=X[:,1].min()-1,X[:,1].max()+1
? ?xx,yy=np.meshgrid(np.arange(x_min,x_max,h),
? ? ? ? ? ? ? ? ? ? ?np.arange(y_min,y_max,h))
? ?plt.title(title)
? ?plt.xlim(xx.min(),xx.max())
? ?plt.ylim(yy.min(),yy.max())
? ?plt.xticks(())
? ?plt.yticks(())
? ?Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
? ?Z=Z.reshape(xx.shape)
? ?plt.contourf(xx,yy,Z,cmap='hot',alpha=0.5)
? ?if draw_sv:
? ? ? ?sv=clf.support_vectors_
? ? ? ?plt.scatter(sv[:,0],sv[:,1],c='y',marker='x')
X,y=make_blobs(n_samples=100,centers=3,n_features=2,random_state=0,cluster_std=0.8)
改C的值就是每個(gè)人隨便輸一個(gè)數(shù)就行,degree,gamma也改
clf_linear=svm.SVC(C=2.0,kernel='linear')
clf_poly=svm.SVC(C=2.0,kernel='poly',degree=6)
clf_rbf=svm.SVC(C=2.0,kernel='rbf',gamma=2.0)
clf_rbf2=svm.SVC(C=2.0,kernel='rbf')

plt.figure(figsize=(8,8),dpi=144)
clfs=[clf_linear,clf_poly,clf_rbf,clf_rbf2]
titles改成對(duì)應(yīng)的值
titles=['Linear Kernel',
? ? ? ?'Polynomail Kernel with Degree=6',
? ? ? ?'Gaussian Kernel with $\gamma=2.0$',
? ? ? ?'Gaussian Kernel with $\gamma=default$']
for clf,i in zip(clfs,range(len(clfs))):
? ?clf.fit(X,y)
? ?plt.subplot(2,2,i+1)
? ?plot_hyperplane(clf,X,y,title=titles[i])
plt.show()

人工非智能作業(yè)4和5的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
当雄县| 松阳县| 揭西县| 福州市| 龙岩市| 逊克县| 青冈县| 屏东县| 鄂托克旗| 肇东市| 安化县| 嵊泗县| 新疆| 天镇县| 宁波市| 同德县| 卢氏县| 吉林省| 榕江县| 南溪县| 汉源县| 甘孜县| 景洪市| 温宿县| 临安市| 台山市| 嵊州市| 鹤壁市| 徐汇区| 绵竹市| 武功县| 武穴市| 周宁县| 克山县| 道真| 榆林市| 葵青区| 朝阳县| 石柱| 武山县| 界首市|