自适应性神经网络(Adaline)——Python实现

Python代码实现:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

class AdalineGD(object):
    """ADAptive LInear NEuron classifier.

    Parameters
    ----------
    eta : float
        Learing rate (between 0.0 and 1.0)
    n_iter : int
        Passes over the training dataset.

    Attributes
    ----------
    w_ :  1d-array
        Weights after fitting.
    errors_ : list
        Nuber of misclassifications in every epoch.

    """
    def __init__(self, eta=0.01, n_iter=50):
        self.eta = eta
        self.n_iter = n_iter

    def fit(self, X, y):
        """Fit training data.

        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
            Training vectors,
            where n_samples is the number of samples and
            n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target values

        Returns
        -------
        self : object

        """
        self.w_ = np.zeros(1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            output = self.net_input(X)
            errors = (y-output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta*errors.sum()
            cost = (errors**2).sum() / 2.0
            self.cost_.append(cost)
        return self

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

    def activation(self, X):
        """Compute linear activation"""
        return self.net_input(X)

    def predict(self, X):
        """Return class label after unit step"""
        return np.where(self.activation(X) >= 0.0, 1, -1)



Python代码实现:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import seed

class AdalineSGD(object):
    """ADAptive LInear NEuron classifier.

    Parameters
    ----------
    eta : float
        Learing rate (between 0.0 and 1.0)
    n_iter : int
        Passes over the training dataset.

    Attributes
    ----------
    w_ :  1d-array
        Weights after fitting.
    errors_ : list
        Nuber of misclassifications in every epoch.

    """
    def __init__(self, eta=0.01, n_iter=10, shuffle=True, random_state=None):
        self.eta = eta
        self.n_iter = n_iter
        self.w_initialized = False
        self.shuffle = shuffle
        if random_state:
            seed(random_state)

    def fit(self, X, y):
        """Fit training data.

        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
            Training vectors,
            where n_samples is the number of samples and
            n_features is the number of features.
        y : array-like, shape = [n_samples]
            Target values

        Returns
        -------
        self : object

        """
        self._initialize_weight(1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            if self.shuffle:
                X, y = self._shuffle(X, y)
                cost = []
                for xi, target in zip(X, y):
                    cost.append(self._update_weights(xi, target))
                avg_cost = sum(cost)/len(y)
                self.cost_.append(avg_cost)
        return self

    def partial_fit(self, X, y):
        if not self.w_initialized:
            self._initialize_weights(X.shape[1])
        if y.ravel().shape[0] > 1:
            for xi, target in zip(X, y):
                self._update_weights(xi, target)
        else:
            self._update_weights(X, y)
        return self

    def _shuffle(self, X, y):
        r = np.random.permutation(len(y))
        return X[r], y[r]

    def _initialize_weights(self, m):
        self._w = np.zeros(1+m)
        self.w_initialized = True

    def _update_weight(self, xi, target):
        output = self.net_input(xi)
        error = (target - output)
        self.w_[1:] += self.eta * xi.dot(error)
        self.w_[0] += self.eta * error
        cost = 0.5*error**2
        return cost

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

    def activation(self, X):
        """Compute linear activation"""
        return self.net_input(X)

    def predict(self, X):
        """Return class label after unit step"""
        return np.where(self.activation(X) >= 0.0, 1, -1)



  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB仿真Adaline网络谐波实现的基本步骤如下: 1. 导入必要的函数库和数据:首先,导入MATLAB的Neural Network工具箱和相应的函数库,如NN Toolbox等。然后,准备用于输入的数据,包括信号的幅度、频率和相位等信息。 2. 创建Adaline网络结构:在MATLAB中,可以使用nnetwork函数创建Adaline网络。指定网络结构的输入和输出层数、神经元个数等。 3. 定义训练参数和目标函数:为Adaline网络定义训练参数,如学习速率、最大训练轮数等。选择合适的目标函数,如均方误差(MSE)或最小二乘损失函数。 4. 划分训练和测试集:将准备好的数据集划分为训练集和测试集,一般常用80%的数据作为训练集,20%的数据作为测试集。 5. 训练网络:使用train函数对Adaline网络进行训练,输入训练集数据和目标输出,设置训练参数和目标函数。训练过程中,网络会自动调整权重和偏置,逐渐提高网络的性能。 6. 评估网络性能:使用测试集数据评估网络的性能,判断网络对未知数据的拟合能力。可以计算均方误差或相关性等指标,评估网络的准确性和鲁棒性。 7. 进行谐波仿真:使用经过训练的Adaline网络进行谐波仿真。输入新的谐波信号数据,通过网络前向传播得到预测输出。可以与实际谐波信号进行比较,评估网络的预测能力和拟合程度。 8. 调整网络参数:根据谐波仿真结果,可以调整网络的参数和结构,进一步提高网络的性能和准确性。可以尝试不同的学习速率、网络层数、神经元个数等,寻找最佳的网络配置。 通过以上步骤,可以在MATLAB中实现Adaline网络对谐波信号的仿真。这样的仿真可以在信号处理、通信系统设计、声音分析等领域中发挥重要作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值