MiaoRain / lund

9 stars 2 forks source link

李航机器学习之估计,感知机 #15

Open MiaoRain opened 4 years ago

MiaoRain commented 4 years ago

image

MiaoRain commented 4 years ago

判别模型和生成模型的区别 https://www.zhihu.com/question/20446337

MiaoRain commented 4 years ago

image image 贝叶斯估计是要有人为预设一个先验的假设。样本量极大时,贝叶斯估计近似于极大似然估计。但是样本量较小时,贝叶斯估计可以避免极端出现,估计效果更好 https://zhuanlan.zhihu.com/p/86009986 https://zhuanlan.zhihu.com/p/40024110 image image image image image image 极大似然估计 image image image image image

MiaoRain commented 4 years ago

感知机 看李航例2.1 https://zhuanlan.zhihu.com/p/30155870 image image image 感知机是线性的模型,其不能表达复杂的函数,不能出来线性不可分的问题,其连异或问题(XOR)都无法解决,因为异或问题是线性不可分的,怎样解决这个问题呢,通常可以: 1.用更多的感知机去进行学习,这也就是人工神经网络的由来。 2.用非线性模型,核技巧,如SVM进行处理。 image image image

MiaoRain commented 4 years ago

感知机

import numpy as np
import matplotlib.pyplot as plt

class MyPerceptron:
    def __init__(self):
        self.w=None
        self.b=0
        self.l_rate=1

    def fit(self,X_train,y_train):
        #用样本点的特征数更新初始w,如x1=(3,3)T,有两个特征,则self.w=[0,0]
        self.w=np.zeros(X_train.shape[1])
        i=0
        while i<X_train.shape[0]:
            X=X_train[i]
            y=y_train[i]
            # 如果y*(wx+b)≤0 说明是误判点,更新w,b
            if y*(np.dot(self.w, X) + self.b) <= 0:
                self.w = self.w + self.l_rate * np.dot(y, X)
                self.b = self.b + self.l_rate * y
                i=0 #如果是误判点,从头进行检测
            else:
                i+=1

def draw(X,w,b):
    #生产分离超平面上的两点
    X_new=np.array([[0], [6]])
    y_predict=-b-(w[0]*X_new)/w[1]
    #绘制训练数据集的散点图
    plt.plot(X[:2,0],X[:2,1],"g*",label="1")
    plt.plot(X[2:,0], X[2:,0], "rx",label="-1")
    #绘制分离超平面
    plt.plot(X_new,y_predict,"b-")
    #设置两坐标轴起止值
    plt.axis([0,6,0,6])
    #设置坐标轴标签
    plt.xlabel('x1')
    plt.ylabel('x2')
    #显示图例
    plt.legend()
    #显示图像
    plt.show()

def main():
    # 构造训练数据集
    X_train=np.array([[3,3],[4,3],[1,1]])
    y_train=np.array([1,1,-1])
    # 构建感知机对象,对数据集继续训练
    perceptron=MyPerceptron()
    perceptron.fit(X_train,y_train)
    print(perceptron.w)
    print(perceptron.b)
    # 结果图像绘制
    draw(X_train,perceptron.w,perceptron.b)

if __name__=="__main__":
    main()
MiaoRain commented 4 years ago
**from sklearn.linear_model import Perceptron
import numpy as np

X_train = np.array([[3, 3], [4, 3], [1, 1]])
y = np.array([1, 1, -1])

perceptron=Perceptron()
perceptron.fit(X_train,y)
print("w:",perceptron.coef_,"\n","b:",perceptron.intercept_,"\n","n_iter:",perceptron.n_iter_)

res=perceptron.score(X_train,y)
print("correct rate:{:.0%}".format(res))

# from sklearn.linear_model import Perceptron
# from sklearn.linear_model import SGDClassifier
# import numpy as np
#
# X_train = np.array([[3, 3], [4, 3], [1, 1]])
# y = np.array([1, 1, -1])
# #perceptron=Perceptron(penalty="l2",alpha=0.01,eta0=1,max_iter=50,tol=1e-3)
# #perceptron=Perceptron()
# perceptron=SGDClassifier(loss="perceptron",eta0=1, learning_rate="constant", penalty=None)
# perceptron.fit(X_train,y)
# print(perceptron.coef_)
# print(perceptron.intercept_)
# print(perceptron.n_iter_)
# X=np.array([[2,2]])
# y=perceptron.predict(X)**