class_docstr: Ordinary least squares linear regression.
  
  Parameters
  ------------
  method : string (default: 'direct')
      For gradient descent-based optimization, use `sgd` (see `minibatch`
      parameter for further options). Otherwise, if `direct` (default),
      the analytical method is used. For alternative, numerically more
      stable solutions, use either `qr` (QR decomopisition) or `svd`
      (Singular Value Decomposition).
  eta : float (default: 0.01)
      solver learning rate (between 0.0 and 1.0). Used with `method =`
      `'sgd'`. (See `methods` parameter for details)
  epochs : int (default: 50)
      Passes over the training dataset.
      Prior to each epoch, the dataset is shuffled
      if `minibatches > 1` to prevent cycles in stochastic gradient descent.
      Used with `method = 'sgd'`. (See `methods` parameter for details)
  minibatches : int (default: None)
      The number of minibatches for gradient-based optimization.
      If None: Direct method, QR, or SVD method (see `method` parameter
               for details)
      If 1: Gradient Descent learning
      If len(y): Stochastic Gradient Descent learning
      If 1 < minibatches < len(y): Minibatch learning
  random_seed : int (default: None)
      Set random state for shuffling and initializing the weights. Used in
      `method = 'sgd'`. (See `methods` parameter for details)
  print_progress : int (default: 0)
      Prints progress in fitting to stderr if `method = 'sgd'`.
      0: No output
      1: Epochs elapsed and cost
      2: 1 plus time elapsed
      3: 2 plus estimated time until completion
  
  Attributes
  -----------
  w_ : 2d-array, shape={n_features, 1}
    Model weights after fitting.
  b_ : 1d-array, shape={1,}
    Bias unit after fitting.
  cost_ : list
      Sum of squared errors after each epoch;
      ignored if solver='normal equation'
  
  Examples
  -----------
  For usage examples, please see
  https://rasbt.github.io/mlxtend/user_guide/regressor/LinearRegression/
class_docstr_tok: ["Ordinary","least","squares","linear","regression",".","Parameters","--","--","--","--","--","--","method",":","string","(","default",":","'direct","'",")","For","gradient","descent-based","optimization",",","use","`","sgd","`","(","see","`","minibatch","`","parameter","for","further","options",")",".","Otherwise",",","if","`","direct","`","(","default",")",",","the","analytical","method","is","used",".","For","alternative",",","numerically","more","stable","solutions",",","use","either","`","qr","`","(","QR","decomopisition",")","or","`","svd","`","(","Singular","Value","Decomposition",")",".","eta",":","float","(","default",":","0.01",")","solver","learning","rate","(","between","0.0","and","1.0",")",".","Used","with","`","method","=","`","`","'sgd","'","`",".","(","See","`","methods","`","parameter","for","details",")","epochs",":","int","(","default",":","50",")","Passes","over","the","training","dataset",".","Prior","to","each","epoch",",","the","dataset","is","shuffled","if","`","minibatches",">","1","`","to","prevent","cycles","in","stochastic","gradient","descent",".","Used","with","`","method","=","'sgd","'","`",".","(","See","`","methods","`","parameter","for","details",")","minibatches",":","int","(","default",":","None",")","The","number","of","minibatches","for","gradient-based","optimization",".","If","None",":","Direct","method",",","QR",",","or","SVD","method","(","see","`","method","`","parameter","for","details",")","If","1",":","Gradient","Descent","learning","If","len","(","y",")",":","Stochastic","Gradient","Descent","learning","If","1","<","minibatches","<","len","(","y",")",":","Minibatch","learning","random_seed",":","int","(","default",":","None",")","Set","random","state","for","shuffling","and","initializing","the","weights",".","Used","in","`","method","=","'sgd","'","`",".","(","See","`","methods","`","parameter","for","details",")","print_progress",":","int","(","default",":","0",")","Prints","progress","in","fitting","to","stderr","if","`","method","=","'sgd","'","`",".","0",":","No","output","1",":","Epochs","elapsed","and","cost","2",":","1","plus","time","elapsed","3",":","2","plus","estimated","time","until","completion","Attributes","--","--","--","--","--","-","w_",":","2d-array",",","shape=","{","n_features",",","1","}","Model","weights","after","fitting",".","b_",":","1d-array",",","shape=","{","1",",","}","Bias","unit","after","fitting",".","cost_",":","list","Sum","of","squared","errors","after","each","epoch",";","ignored","if","solver='normal","equation'","Examples","--","--","--","--","--","-","For","usage","examples",",","please","see","https",":","\/\/rasbt.github.io\/mlxtend\/user_guide\/regressor\/LinearRegression\/"]
code: def _fit(self, X, y, init_params=True):
      if init_params:
          self.b_, self.w_ = self._init_params(
              weights_shape=(X.shape[1], 1),
              bias_shape=(1,),
              random_seed=self.random_seed,
          )
          self.cost_ = []
  
      # Direct analytical method
      if self.method == "direct":
          self.b_, self.w_ = self._normal_equation(X, y)
      # Gradient descent or stochastic gradient descent learning
      elif self.method == "sgd":
          self.init_time_ = time()
          rgen = np.random.RandomState(self.random_seed)
          for i in range(self.epochs):
              for idx in self._yield_minibatches_idx(
                  rgen=rgen,
                  n_batches=self.minibatches,
                  data_ary=y,
                  shuffle=True,
              ):
                  y_val = self._net_input(X[idx])
                  errors = y[idx] - y_val
                  self.w_ += self.eta * X[idx].T.dot(errors).reshape(
                      self.w_.shape
                  )
                  self.b_ += self.eta * errors.sum()
  
              cost = self._sum_squared_error_cost(y, self._net_input(X))
              self.cost_.append(cost)
              if self.print_progress:
                  self._print_progress(
                      iteration=(i + 1), n_iter=self.epochs, cost=cost
                  )
      # Solve using QR decomposition
      elif self.method == "qr":
          Xb = np.hstack((np.ones((X.shape[0], 1)), X))
          Q, R = np.linalg.qr(Xb)
          beta = np.dot(np.linalg.inv(R), np.dot(Q.T, y))
          self.b_ = np.array([beta[0]])
          self.w_ = beta[1:].reshape(X.shape[1], 1)
      # Solve using SVD
      elif self.method == "svd":
          Xb = np.hstack((np.ones((X.shape[0], 1)), X))
          beta = np.dot(np.linalg.pinv(Xb), y)
          self.b_ = np.array([beta[0]])
          self.w_ = beta[1:].reshape(X.shape[1], 1)
  
      return self
  
file_path: mlxtend/mlxtend/regressor/linear_regression.py
imports: from time import time
  import numpy
  from .._base import _BaseModel, _IterativeModel, _Regressor
label_desc: Use image node_id 2 for calling the LinearRegression obj's underlying member method code with example usage: obj._fit(X, y, init_params) and returns: self