<jupyter_start><jupyter_text>Large-scale machine learning and stochastic gradient descent<jupyter_code>class AdalineSGD(object):
    def __init__(self,eta=0.01,n_tier=10,
                 shuffle=True,random_state=None):
        self.eta=eta
        self.n_tier=n_tier
        self.w_initialized = False
        self.shuffle=shuffle
        self.random_state = random_state

    def fit(self,X,y):

        self._initialize_weights(X.shape[1])
        self.cost_ = []
        for i in range (self.n_tier):
            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(X,y)
        return self

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

    def _initialize_weights(self,m):
        self.rgen = np.random.RandomState(self.random_state)
        self.w_= self.rgen.normal(loc=0.0,scale=0.01,
                                  size=1+m)
        self.w_initialized=True

    def _update_weights(self,xi,target):
        output = self.activation(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):
        return np.dot(X,self.w_[1:])+self.w_[0]

    def activation(self,X):
        return X

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

ada_sgd = AdalineSGD(n_tier=15,eta=0.01,random_state=1)
ada_sgd.fit(X_std,y)
plot_decision_regions(X_std,y,classifier=ada_sgd)
plt.title('Adaline - Stochastic Gradient Descent')
plt.xlabel('Sepal Length [standardized]')
plt.ylabel('Pepal Length [standardized]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()
plt.plot(range(1,len(ada_sgd.cost_)+1),ada_sgd.cost_,marker='o')
plt.xlabel('Epochs')
plt.ylabel('Average Cost')
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13655640373197625, 'fraction_numerical': 0.01441899915182358, 'mean_word_length': 2.579666160849772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '405417', 'license': 'CNRI-Python', 'n_tokens_mistral': 884, 'n_tokens_neox': 832, 'n_words': 120, 'path': 'Ch2.ipynb', 'repo_name': 'epson12332/Python_ML_Playground', 'size': '2358'}