李航《統計學方法chapter two 實踐》

自己實現梯度下降

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.datasets import  load_iris
import pandas as pd
#value_counts()方法統計數組或序列所有元素出現次數,對某一列統計可以直接用df.column_name.value_counts()
#load data
iris = load_iris()#數據不包含特徵名
df = pd.DataFrame(iris.data,columns=iris.feature_names)
df['label']=iris.target
#
df.columns=['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
df.label.value_counts()#label 各值的個數
plt.scatter(df[:50]['sepal length'],df[:50]['sepal width'],label='0')
plt.scatter(df[50:100]['sepal length'],df[50:100]['sepal width'],label='1')
plt.legend()
plt.show()
data=np.array(df.iloc[:100,[0,1,-1]])
X,y= data[:100,:-1],data[:,-1]
#將label改成1和-1
y=np.array([1 if i==1 else -1 for i in y])
#perceptron
#數據線性可分 ,二分類問題
#此處爲一元一次性方程
#在編寫代碼時只寫框架思路,具體實現還未編寫就可以用pass進行佔位,使程序不報錯,不會進行任何操作
class Model:
    def __init__(self):
        self.w = np.ones(len(data[0])-1,dtype=np.float32)#屬性只有兩個 所以w的維度爲2 w的屬性是array
        self.b = 0
        self.l_rate = 0.1#學習率
    def sign(self,x,w,b):
        y=np.dot(x,w)+b#dot即做點積
        return y

    #隨機梯度下降法
    def fit(self,X_train,y_train):
        is_wrong = True
        while is_wrong:
            wrong_count = 0
            for d in range(len(X_train)):
                X = X_train[d]
                y = y_train[d]
                if y*self.sign(X,self.w,self.b)<=0:#注意= 0也是誤分類
                    self.w = self.w + self.l_rate*np.dot(y,X)
                    self.b = self.b + self.l_rate*y
                    wrong_count +=1
            if wrong_count == 0:
                is_wrong = False
        return 'Perceptron Model'
    def score(self):
        pass

perceptron = Model()
perceptron.fit(X,y)

x_points = np.linspace(4,7,10)
print(perceptron.w)
y_  = -(perceptron.w[0]* x_points + perceptron.b)/perceptron.w[1]#因爲w*x+b=0
plt.plot(x_points,y_)

plt.plot(data[:50,0],data[:50,1],"bo",color='blue',label='0')
plt.plot(data[50:100,0],data[50:100,1],"bo",color="orange",label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
plt.show()








 sklearn 調用感知機

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.datasets import  load_iris
import pandas as pd
from sklearn.linear_model import Perceptron
#value_counts()方法統計數組或序列所有元素出現次數,對某一列統計可以直接用df.column_name.value_counts()
#load data
iris = load_iris()#數據不包含特徵名
df = pd.DataFrame(iris.data,columns=iris.feature_names)
df['label']=iris.target
#
df.columns=['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
df.label.value_counts()#label 各值的個數
plt.scatter(df[:50]['sepal length'],df[:50]['sepal width'],label='0')
plt.scatter(df[50:100]['sepal length'],df[50:100]['sepal width'],label='1')
plt.legend()
plt.show()
data=np.array(df.iloc[:100,[0,1,-1]])
X,y= data[:100,:-1],data[:,-1]
#將label改成1和-1
y=np.array([1 if i==1 else -1 for i in y])

#fit_intercept 默認爲true(代表需要中心化)
clf =  Perceptron(fit_intercept=False,n_iter=1000,shuffle=False)
clf.fit(X,y)
print(clf.coef_)#clf.coef[0] 是w
print(clf.intercept_)# 截距 即b
x_points=np.arange(4,8)
y_ = -(clf.coef_[0][0]*x_points+clf.intercept_)/clf.coef_[0][1]
plt.plot(x_points,y_)

plt.plot(data[:50,0],data[:50,1],"bo",color='blue',label='0')
plt.plot(data[50:100,0],data[50:100,1],"bo",color="orange",label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
plt.show()



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章