473,388 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How To Modify Adaline Stochastic Gradient Descent

7





Dear


May I know how to modify my own Python programming so that I will get the same picture as refer to the attached file - Adaline Stochastic gradient descent

(I am using the Anaconda Python 3.7)

Prayerfully

Tron Orino Yeong
tcynotebook@yahoo.com
0916643858











Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. from matplotlib.colors import ListedColormap
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from numpy.random import seed
  7. import pandas as pd
  8.  
  9. # Stochastic Gradient Descent
  10. class SGD(object):
  11.    def __init__(self, rate = 0.01, niter = 10,
  12.                 shuffle=True, random_state=None):
  13.       self.rate = rate
  14.       self.niter = niter
  15.       self.weight_initialized = False
  16. 
  17.       # If True, Shuffles training data every epoch
  18.       self.shuffle = shuffle
  19.  
  20.       # Set random state for shuffling and initializing the weights.
  21.       if random_state:
  22.          seed(random_state)
  23.  
  24.    def fit(self, X, y):
  25.       """Fit training data
  26.       X : Training vectors, X.shape : [#samples, #features]
  27.       y : Target values, y.shape : [#samples]
  28.       """
  29.  
  30.       # weights
  31.       self.initialize_weights(X.shape[1])
  32.  
  33.       # Cost function
  34.       self.cost = []
  35.  
  36.       for i in range(self.niter):
  37.          if self.shuffle:
  38.             X, y = self.shuffle_set(X, y)
  39.          cost = []
  40.          for xi, target in zip(X, y):
  41.             cost.append(self.update_weights(xi, target))
  42.          avg_cost = sum(cost)/len(y)
  43.          self.cost.append(avg_cost)
  44.       return self
  45.  
  46.    def partial_fit(self, X, y):
  47.       """Fit training data without reinitializing the weights"""
  48.       if not self.weight_initialized:
  49.          self.initialize_weights(X.shape[1])
  50.       if y.ravel().shape[0] > 1:
  51.          for xi, target in zip(X, y):
  52.             self.update_weights(xi, target)
  53.       else:
  54.          self.up
  55.       return self
  56.  
  57.    def shuffle_set(self, X, y):
  58.       """Shuffle training data"""
  59.       r = np.random.permutation(len(y))
  60.       return X[r], y[r]
  61.  
  62.    def initialize_weights(self, m):
  63.       """Initialize weights to zeros"""
  64.       self.weight = np.zeros(1 + m)
  65.       self.weight_initialized = True
  66.  
  67.    def update_weights(self, xi, target):
  68.       """Apply SGD learning rule to update the weights"""
  69.       output = self.net_input(xi)
  70.       error = (target - output)
  71.       self.weight[1:] += self.rate * xi.dot(error)
  72.       self.weight[0] += self.rate * error
  73.       cost = 0.5 * error**2
  74.       return cost
  75.  
  76.    def net_input(self, X):
  77.       """Calculate net input"""
  78.       return np.dot(X, self.weight[1:]) + self.weight[0]
  79.  
  80.    def activation(self, X):
  81.       """Compute linear activation"""
  82.       return self.net_input(X)
  83.  
  84.    def predict(self, X):
  85.       """Return class label after unit step"""
  86.       return np.where(self.activation(X) >= 0.0, 1, -1)
  87.  
  88. def plot_decision_regions(X, y, classifier, resolution=0.02):
  89.    # setup marker generator and color map
  90.    markers = ('s', 'x', 'o', '^', 'v')
  91.    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
  92.    cmap = ListedColormap(colors[:len(np.unique(y))])
  93.  
  94.    # plot the decision surface
  95.    x1_min, x1_max = X[:,  0].min() - 1, X[:, 0].max() + 1
  96.    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  97.    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
  98.    np.arange(x2_min, x2_max, resolution))
  99.    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
  100.    Z = Z.reshape(xx1.shape)
  101.    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
  102.    plt.xlim(xx1.min(), xx1.max())
  103.    plt.ylim(xx2.min(), xx2.max())
  104.  
  105.    # plot class samples
  106.    for idx, cl in enumerate(np.unique(y)):
  107.       plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
  108.       alpha=0.8, c=cmap(idx),
  109.       marker=markers[idx], label=cl)
  110.  
  111. df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None)
  112.  
  113. y = df.iloc[0:100, 4].values
  114. y = np.where(y == 'Iris-setosa', -1, 1)
  115. X = df.iloc[0:100, [0, 2]].values
  116.  
  117. # standardize
  118. X_std = np.copy(X)
  119. X_std[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
  120. X_std[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()
  121.  
  122. sgd1 = SGD(niter=100, rate=0.01, random_state=1)
  123. sgd2 = SGD(niter=50, rate=0.01, random_state=1)
  124. sgd3 = SGD(niter=10, rate=0.01, random_state=1)
  125.  
  126. sgd1.fit(X_std, y)
  127. sgd2.fit(X_std, y)
  128. sgd3.fit(X_std, y)
  129.  
  130. plt.plot(range(1, len(sgd1.cost) + 1), sgd1.cost, 
  131.          marker='o', linestyle='oo', label='batch=1')
  132. plt.plot(range(1, len(sgd2.cost_) + 1), np.array(sgd2.cost_) / len(y_train), 
  133.          marker='o', linestyle='--', label='batch=2')
  134. plt.plot(range(1, len(sgd3.cost_) + 1), np.array(sgd3.cost_) / len(y_train), 
  135.          marker='o', linestyle='xx', label='batch=3')
  136.  
  137. plt.xlabel('Epochs')
  138. plt.ylabel('Average Cost')
  139. plt.show()
  140.  
  141.  


Please refer to the link -

https://www.scienceforums.net/topic/...omment-1097272



Attached Images
File Type: jpg 00001.jpg (159.9 KB, 51 views)
File Type: jpg 00002.jpg (68.4 KB, 54 views)
Attached Files
File Type: pdf Adaline Stochastic gradient descent.pdf (171.1 KB, 100 views)
File Type: txt Python Stochastic gradient descent.txt (4.4 KB, 139 views)
Mar 15 '19 #1
0 1250

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Woody Splawn | last post by:
I have a webform where I would like to have the background color for the webform be a gradient in blue. How do I do this? What is the easiest way? What is the way most used by other developers? ...
13
by: Crirus | last post by:
The main ideea: I havea 513x513 array of values....I draw a sort of map using that values for colors... so I have a realistic lanscape (map). I want to zoom about 60 times that map...and the...
0
by: Don | last post by:
I'm making a custom control that has a multiline textbox on it. I want to make the textbox have a gradient background. On my custom control's Paint event I call a routine that draws a gradient on...
11
by: Maximus | last post by:
Hi all, Has anyone been able to reterive the gradient selection colors used in outlook 2003? Basically, I have a grid showing a list of records and my client wants the selected row to have the...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
4
by: Jon Slaughter | last post by:
I'm drawing a linear gradient and its very "banded". The colors are far enough part that it should be plenty smooth enough but I always get the same number of bands regardless. I've tried to set...
2
by: moondaddy | last post by:
How do I make a background gradient diagonal instead of just horizontal or vertical? This makes the standard horizontal gradient. progid:dximagetransform.microsoft.gradient(gradienttype=1,...
6
by: moondaddy | last post by:
I want to fill the entire background of a page with a gradient. As a test, I first filled with a solid color like this: <body style="background-color: #ccffff;" > and the entire page was this...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
9
by: Eric Lindsay | last post by:
How do you provide a consistent gradient fill as a background in a liquid layout? If I make a gradient fill image say 1000 pixels wide (and repeat it down the page) to suit a typical computer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.