人工非智能作业4和5
实验5
from collections import defaultdict
import math
#定义贝叶斯分类器
class NaiveBayes:
def __init__(self):
#初始化类别字典
self.classes = {}
#初始化特征和类别的字典
self.freqs = defaultdict(lambda: defaultdict(int))
#初始化计数字典
self.total = defaultdict(int)
#训练模型
def train(self, data):
for features, label in data:
#类别不在字典。添加新的类别
if label not in self.classes:
self.classes[label] = 0
#对当前类别计数
self.classes[label] += 1
#对当前特征在当前类别下出现次数进行计数
for feature in features:
self.freqs[label][feature] += 1
#统计当前类别下的总特征数
self.total[label] += 1
#预测类别
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)
#对条件概率取对数
probs[label] = math.log(class_prob) + sum([math.log(fp) for fp in feature_probs])
return max(probs, key=probs.get)
#原始数据集
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"]
]
#将原始数据集转换为可以输入的贝叶斯训练集
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 = "上进"
elif features[2] == "bu":
f3 = "不上进"
else:
f3 = "一般"
train_data.append(([f1, f2, f3], int(features[3] == "jia")))
model = NaiveBayes()
model.train(train_data)
features = ["帅", "不上进", "性格不好"]
label = model.predict(features)
if label == 1:
print("女青年会同意求婚")
else:
print("女青年不会同意求婚")

实验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)
这四个是核函数,此程序每个人都一样,接下来的两个程序最好每个人的值都不一样
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的值就是每个人随便输一个数就行,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改成对应的值
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的值就是每个人随便输一个数就行,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改成对应的值
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()