1.导入包
% 导入代码块
\usepackage{listings}
\usepackage{xcolor}
\lstset{
numbers=left,
numberstyle= \tiny,
keywordstyle= \color{ blue!70},
commentstyle= \color{red!50!green!50!blue!50},
frame=shadowbox, % 阴影效果
rulesepcolor= \color{ red!20!green!20!blue!20} ,
escapeinside=``, % 英文分号中可写入中文
xleftmargin=2em,xrightmargin=2em, aboveskip=1em,
framexleftmargin=2em
}
2.正文使用
\lstset{language=python}
\begin{lstlisting}
# Make training set predictions.
y_train_predictions = logistic_regression.predict(x_train)
y_test_predictions = logistic_regression.predict(x_test)
# Check what percentage of them are actually correct.
train_precision = np.sum(y_train_predictions == y_train) / y_train.shape[0] * 100
test_precision = np.sum(y_test_predictions == y_test) / y_test.shape[0] * 100
print('Training Precision: {:5.4f}%'.format(train_precision))
print('Test Precision: {:5.4f}%'.format(test_precision))
\end{lstlisting}
实现效果