日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 智能硬件 > 人工智能AI
[導(dǎo)讀] 最近在嘗試將所有的機(jī)器學(xué)習(xí)與深度學(xué)習(xí)的模型用Python來(lái)實(shí)現(xiàn),大致的學(xué)習(xí)思路如下: 分類器 回歸與預(yù)測(cè) 時(shí)間序列 所有的模型先用 Python語(yǔ)言實(shí)現(xiàn),然后用T

最近在嘗試將所有的機(jī)器學(xué)習(xí)與深度學(xué)習(xí)的模型用Python來(lái)實(shí)現(xiàn),大致的學(xué)習(xí)思路如下:

分類器

回歸與預(yù)測(cè)

時(shí)間序列

所有的模型先用 Python語(yǔ)言實(shí)現(xiàn),然后用Tensorflow的實(shí)現(xiàn)。
 

1 數(shù)據(jù)集

本文開(kāi)始以UCI中的Iris數(shù)據(jù)集作為訓(xùn)練數(shù)據(jù)集和測(cè)試時(shí)間集。該數(shù)據(jù)集給出了花萼(sepal)的長(zhǎng)度和寬度以及花瓣(petal)的長(zhǎng)度和寬度,根據(jù)這4個(gè)特征訓(xùn)練模型,預(yù)測(cè)花的類別(Iris Setosa,Iris Versicolour,Iris Virginica)。
# 包引入
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)
df.head(10)

1.1 數(shù)據(jù)處理

我們提取前100個(gè)樣本(50個(gè)Iris Setosa和50個(gè)Iris Versicolour),并將不同的樣本類別標(biāo)注為1(Iris Versicolour)和-1(Iris Setosa);然后,將花萼的長(zhǎng)度和花瓣的長(zhǎng)度作為特征。大致處理如下:
y = df.iloc[0:100, 4].values # 預(yù)測(cè)標(biāo)簽向量
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0,2]].values # 輸入特征向量

# 使用散點(diǎn)圖可視化樣本
plt.scatter(X[:50, 0], X[:50,1], color='red', marker='o', label='setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
plt.xlabel('petal length')
plt.ylabel('sepal length')
plt.legend(loc='upper left')
plt.show

2 模型

2.1 神經(jīng)網(wǎng)絡(luò)模型

2.1.1 模型實(shí)現(xiàn)

我們可以將該問(wèn)題轉(zhuǎn)化為一個(gè)二分類的任務(wù),因此,可以將1與-1作為類別標(biāo)簽。從而激活函數(shù)可以表示如下:

大致的模型結(jié)構(gòu)如下:


class Perceptron(object):
"""
Parameters
------------
eta : float
學(xué)習(xí)率 (between 0.0 and 1.0)
n_iter : int
迭代次數(shù)
Attributes
-----------
w_ : 1d-array
權(quán)重
errors_ : list
誤差
"""
def __init__(self, eta=0.01, n_iter=10):
self.eta = eta
self.n_iter = n_iter

def fit(self, X, y):
self.w_ = np.zeros(1 + X.shape[1])
self.errors_ = []

for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self

def net_input(self, X):
return np.dot(X, self.w_[1:]) + self.w_[0]

def predict(self, X):
return np.where(self.net_input(X) >= 0.0, 1, -1)

2.1.2 模型訓(xùn)練
ppn = Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)

2.1.3 模型驗(yàn)證

誤差分析
plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of misclassificaTIons')
plt.show()

可視化分類器
from matplotlib.colors import ListedColormap
def plot_decision_regions(X, y, classifier, resoluTIon=0.01):
"""
可視化分類器
:param X: 樣本特征向量
:param y: 樣本標(biāo)簽向量
:param classifier: 分類器
:param resoluTIon: 殘差
:return:
"""

markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])

x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resoluTIon), np.arange(x2_min, x2_max, resolution))

Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx2.min(), xx2.max())

plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())

for idx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)

# 調(diào)用可視化分類器函數(shù)
plot_decision_regions(X, y, classifier=ppn)
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc='upper left')
plt.show()

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉