LaTeX 中的偽代碼樣式

LaTeX 中的偽代碼樣式

我正在嘗試複製以下來自“統計學習的要素”的偽代碼的風格。

我嘗試過演算法、演算法和枚舉(對於該專案),但我還沒有達到下面的結果...

虛擬程式碼

答案1

enumerate這只是環境中的嵌套algorithm

在此輸入影像描述

\documentclass{report}

\usepackage{algorithm,amsmath,lipsum,xcolor,caption}
\DeclareMathOperator{\sgn}{sign}
\DeclareCaptionFont{lightblue}{\color{blue}}
\captionsetup[algorithm]{labelfont={bf,lightblue}, textfont={it}}
\renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}}

\begin{document}

\setcounter{chapter}{10}% Just for this example
\lipsum[1]

\begin{algorithm}[htb]
  \caption{AdaBoost.M1.}
  \begin{enumerate}
    \item
    Initialize the observation weights $w_i = 1/N$, $i = 1, 2, \dots, N$

    \item
    For $m = 1$ to $M$:
    \begin{enumerate}
      \item
      Fit a classifier $G_m(x)$ to the training data using weights $w_i$.

      \item
      Compute
      \[
        \text{err}_m = \frac{\sum_{i=1}^N w_i I(y_i \neq G_m(x_i))}{\sum_{i=1}^N w_i}.
      \]

      \item
      Compute $\alpha_m = \log((1 - \text{err}_m) / \text{err}_m)$.

      \item
      Set $w_i \leftarrow w_i \cdot \exp [\alpha_m \cdot I(y_i \neq G_m(x_i))]$, $i = 1, 2, \dots, N$.
    \end{enumerate}

  \item
  Output $G(x) = \sgn \bigl[ \sum_{m = 1}^M \alpha_m G_m(x) \bigr]$.
  \end{enumerate}
\end{algorithm}

\lipsum[2]

\end{document}

相關內容