DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Learning AI/ML: The Hard Way
  • Neural Networks: From Perceptrons to Deep Learning
  • Optimizing Model Training: Strategies and Challenges in Artificial Intelligence
  • Navigating the Complexities of Text Summarization With NLP

Trending

  • Proactive Security in Distributed Systems: A Developer’s Approach
  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Using Python Libraries in Java
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Adaline Explained With Python Example

Adaline Explained With Python Example

In this post, you will learn the concepts of Adaline (ADAptive LInear NEuron), a machine learning algorithm, along with a Python example.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Oct. 20, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
18.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, you will learn the concepts of Adaline (ADAptive LInear NEuron), a machine learning algorithm, along with a Python example. Like Perceptron, it is important to understand the concepts of Adaline as it forms the foundation of learning neural networks. The concept of Perceptron and Adaline could found to be useful in understanding how gradient descent can be used to learn the weights which when combined with input signals is used to make predictions based on unit step function output.

Here are the topics covered in this post in relation to Adaline algorithm and its Python implementation:

  • What's Adaline?
  • Adaline Python implementation
  • Model trained using Adaline implementation

What's Adaline?

Adaline, like Perceptron, also mimics a neuron in the human brain. You may want to read one of my related posts on Perceptron — Perceptron explained using Python example. Adaline is also called as single-layer neural network. Here is the diagram of Adaline:


Fig 1. Adaline - Single-layer neural network


The following represents the working of Adaline machine learning algorithm based on the above diagram:

  • Net Input function - Combination of Input signals of different strength (weights): Input signals of different strength (weights) get combined / added in order to be fed into the activation function. The combined input or sum of weighted inputs can also be called as net input.  Pay attention to the Net-input function shown in the above diagram
  • Net input is fed into activation function (Linear): Net input is fed into activation function. The activation function of adaline is an identity function. If Z is net input, the identity function would look like \(g(Z) = Z\). The activation function is linear activation function as the output of the function is linear combination of input signals and weights.
  • Activation function output is used to learn weights: The output of activation function (same as net input owing to identity function) is used to calculate the change in weights related to different inputs which will be updated to learn new weights. Pay attention to feedback loop shown with text Error or cost. Recall that in Perceptron, the activation function is a unit step function and the output is binary (1 or 0) based on whether the net input value is greater than or equal to zero (0) or otherwise.
  • Threshold function - Binary prediction (1 or 0) based on unit step function: The prediction made by Adaline neuron is done in the same manner as in case of Perceptron. The output of activation function, which is net input is compared with 0 and the output is 1 or 0 depending upon whether the net input is greater than or equal to 0. Pay attention in the above diagram as to how the output of activation function is fed into threshold function.

Adaline Python Implementation

The adaline algorithm explained in previous section with the help of diagram will be illustrated further with the help of Python code. Here are the algorithm steps and the related Python implementation:

  • Weighted input signals combined as net input: First step is to combine the input signals with respective weights (strength of input signals) and come up with sum of weighted inputs. This is also termed as net input.
Java
 




x


 
1
'''
2
Net Input is sum of weighted input signals
3
'''
4
def net_input(self, X):
5
      weighted_sum = np.dot(X, self.coef_[1:]) + self.coef_[0]
6
      return weighted_sum



  • Activation function invoked with net input: Net input is fed into activation function to calculate the output. The activation function is a linear activation function. It is an identity function. Thus, \(g(Z) = Z\). Note that the output or return value of activation function is same as input (identity function)
Java
 




xxxxxxxxxx
1


 
1
'''
2
Activation function is fed the net input. As the activation function is
3
an identity function, the output from activation function is same as the
4
input to the function.
5
'''
6
def activation_function(self, X):
7
      return X



  • Prediction based on unit step function: Prediction is made based on the unit step function which provides binary output as 1 or 0 based on whether the output of activation function is greater than or equal to zero. If the output of activation function is greater than or equal to zero, the prediction is 1 or else 0. Note how activation function is invoked with net input and the output of activation function is compared with 0.
Java
 




xxxxxxxxxx
1


 
1
'''
2
Prediction is made on the basis of output of activation function
3
'''
4
def predict(self, X):
5
  return np.where(self.activation_function(self.net_input(X)) >= 0.0, 1, 0)



  • Weights learned using activation function output (continuous value): Unlike Perceptron where weights are learned based on the prediction value which is derived as out of unit step function, the weights in case of Adaline is learned by comparing the actual / expected value with the out of activation function which is a continuous value. Note that the weights are learned based on batch gradient descent algorithm which requires the weights to be updated after considering the weight updates related to all training examples. This is unlike stochastic gradient descent where weights are updated after each training example.
Java
 




xxxxxxxxxx
1
14


 
1
Batch Gradient Descent 
2

           
3
1. Weights are updated considering all training examples.
4
2. Learning of weights can continue for multiple iterations
5
3. Learning rate needs to be defined
6
'''
7
def fit(self, X, y):
8
    rgen = np.random.RandomState(self.random_state)
9
    self.coef_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
10
    for _ in range(self.n_iterations):
11
          activation_function_output = self.activation_function(self.net_input(X))
12
          errors = y - activation_function_output
13
          self.coef_[1:] = self.coef_[1:] + self.learning_rate*X.T.dot(errors)
14
          self.coef_[0] = self.coef_[0] + self.learning_rate*errors.sum()



Here is the entire Python code of Adaline algorithm custom implementation:

Java
 




xxxxxxxxxx
1
57


 
1
class CustomAdaline(object):
2
    
3
    def __init__(self, n_iterations=100, random_state=1, learning_rate=0.01):
4
        self.n_iterations = n_iterations
5
        self.random_state = random_state
6
        self.learning_rate = learning_rate
7

           
8
    '''
9
    Batch Gradient Descent 
10
    
11
    1. Weights are updated considering all training examples.
12
    2. Learning of weights can continue for multiple iterations
13
    3. Learning rate needs to be defined
14
    '''
15
    def fit(self, X, y):
16
        rgen = np.random.RandomState(self.random_state)
17
        self.coef_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
18
        for _ in range(self.n_iterations):
19
              activation_function_output = self.activation_function(self.net_input(X))
20
              errors = y - activation_function_output
21
              self.coef_[1:] = self.coef_[1:] + self.learning_rate*X.T.dot(errors)
22
              self.coef_[0] = self.coef_[0] + self.learning_rate*errors.sum() 
23
    
24
    '''
25
    Net Input is sum of weighted input signals
26
    '''
27
    def net_input(self, X):
28
            weighted_sum = np.dot(X, self.coef_[1:]) + self.coef_[0]
29
            return weighted_sum
30
    
31
    '''
32
    Activation function is fed the net input. As the activation function is
33
    an identity function, the output from activation function is same as the
34
    input to the function.
35
    '''
36
    def activation_function(self, X):
37
            return X
38
    
39
    '''
40
    Prediction is made on the basis of output of activation function
41
    '''
42
    def predict(self, X):
43
        return np.where(self.activation_function(self.net_input(X)) >= 0.0, 1, 0) 
44
    
45
    '''
46
    Model score is calculated based on comparison of 
47
    expected value and predicted value
48
    '''
49
    def score(self, X, y):
50
        misclassified_data_count = 0
51
        for xi, target in zip(X, y):
52
            output = self.predict(xi)
53
            if(target != output):
54
                misclassified_data_count += 1
55
        total_data_count = len(X)
56
        self.score_ = (total_data_count - misclassified_data_count)/total_data_count
57
        return self.score_



Model Trained With Adaline Algorithm

Here is the Python code which represents the breast cancer classification model trained using Adaline implementation explained in the previous section:

Java
 




xxxxxxxxxx
1
22


 
1
#
2
# Load the data set
3
#
4
bc = datasets.load_breast_cancer()
5
X = bc.data
6
y = bc.target
7
#
8
# Create training and test split
9
#
10
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42, stratify=y)
11
#
12
# Instantiate CustomPerceptron
13
#
14
adaline = CustomAdaline(n_iterations = 10)
15
#
16
# Fit the model
17
#
18
adaline.fit(X_train, y_train)
19
#
20
# Score the model
21
#
22
adaline.score(X_test, y_test), prcptrn.score(X_train, y_train)



The score of the model on the test data set turns out to be around 0.63.

Conclusions

Here is the summary of what you learned in this post in relation to Adaline algorithm and its Python implementation:

  • Adaline algorithm mimics a neuron in the human brain
  • Adaline is similar to the algorithm Perceptron. It can also be termed as a single-layer neural network.
  • The difference between Adaline and Perceptron lies in the manner in which weights are learned based on the differences between the output label and the continuous value output of the activation function. In Perceptron, the difference between an actual label and a predicted label is used to learn the weights.
Activation function Python (language) neural network Net (command) Algorithm Perceptron Machine learning Gradient descent Implementation

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Learning AI/ML: The Hard Way
  • Neural Networks: From Perceptrons to Deep Learning
  • Optimizing Model Training: Strategies and Challenges in Artificial Intelligence
  • Navigating the Complexities of Text Summarization With NLP

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!