import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
ann建模
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import logisticregression
# 載入資料集
fruits_df = pd.read_table('fruit_data_with_colors.txt')
x = fruits_df[['width', 'height']]
y = fruits_df['fruit_label'].copy()
y[y != 1] = 0
# 分割資料集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=1/4, random_state=0)
1、 單層ann
from sklearn.neural_network import mlpclassifier
from ml_visualization import plot_class_regions_for_classifier
# 神經元個數
units = [1, 10, 100]
for unit in units:
# 啟用函式:relu, logistic, tanh
# 優化演算法:lbfgs, sgd, adam。adam適用於較大的資料集,lbfgs適用於較小的資料集。
ann_model = mlpclassifier(hidden_layer_sizes=[unit], activation='logistic', solver='lbfgs', random_state=0)
ann_model.fit(x_train, y_train)
print('神經元個數={},準確率:'.format(unit, ann_model.score(x_test, y_test)))
plot_class_regions_for_classifier(ann_model, x_test.values, y_test.values, title='unit={}'.format(unit))
2、多層ann
ann_model = mlpclassifier(hidden_layer_sizes=[10, 10], activation='relu', solver='lbfgs', random_state=0)
ann_model.fit(x_train, y_train)
print('準確率:'.format(ann_model.score(x_test, y_test)))
plot_class_regions_for_classifier(ann_model, x_test.values, y_test.values)
3、 ann中的正則化
# alpha
aplhas = [0.001, 0.01, 0.1, 1.0]
for alpha in aplhas:
ann_model = mlpclassifier(hidden_layer_sizes=[100, 100], activation='tanh', solver='lbfgs', random_state=0,
alpha=alpha)
ann_model.fit(x_train, y_train)
print('alpha={},準確率:'.format(alpha, ann_model.score(x_test, y_test)))
plot_class_regions_for_classifier(ann_model, x_test.values, y_test.values, title='alpha={}'.format(alpha))
人工神經網路 多層神經網路
模型原型 sklearn.neural network.mlpclassifier hidden layer sizes 100,activation relu algorithm adam alpha 0.0001,batch size auto learning rate constant le...
人工神經網路
人工神經網路 artificial neural network,ann 通過對大量歷史資料的計算來建立分類和 模型。神經網路的學習就是通過迭代演算法對權值逐步修改優化的過程。學習的目標是通過修改權值是訓練樣本集中所有樣本都能被正確分類。人工神經元用於模擬生物神經元,人工神經元可以看作乙個多輸入 單...
人工神經網路
今天,老師上課講了一下人工神經網路,真是佩服科學研究者,總是能夠把幾個跨學科的東西聯絡在一起來解決問題,人工神經網路的演化可以涉及多種領域,其中還可以用來 以及判斷。其中有兩個方面我很是感興趣,乙個是對聲音的辨別,另外乙個是對股價的 下面闡述下對聲音辨別的思路 1 對聲音的辨別 首先是輸入聲音的資訊...