OBS: These snippets will not run correctly since it was taken from the code in the way it was written. Therefore, the variables do not convey any meaning. Use it just a guide and not a copy-and-paste reference.
Last active
May 27, 2024 18:18
-
-
Save tapyu/3e44aa7d087bf8f2673533f0da9cb3cf to your computer and use it in GitHub Desktop.
General Python snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
# 1. DEBUG: Detailed information, typically of interest only when diagnosing problems. # Lowest lever | |
# 2. INFO: Confirmation that things are working as expected. | |
# 3. WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected. | |
# 4. ERROR: Due to a more serious problem, the software has not been able to perform some function. | |
# 5. CRITICAL: A serious error, indicating that the program itself may be unable to continue running. # Highest lever | |
logging.basicConfig(filename='TC3.log', level=logging.DEBUG, format='%(filename)s:%(message)s', filemode='w', force = True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
for method, method_name in zip((LSC_LPC, LSC_PSD, LSC_PSD_BC), ('LSC+LPC', 'LSC+PSD', 'LSC+PSD+BC')): | |
for conf_mat in 'worst confision matrix', 'best confusion matrix': | |
df_conf_mat = pd.DataFrame(method[conf_mat], index=all_commands, columns=all_commands) | |
fig = plt.figure(figsize = (10,7)) | |
ax = fig.gca() | |
plt.title(f'{conf_mat.capitalize()} - {method_name}') | |
ax = sns.heatmap(df_conf_mat, annot=True, cmap="YlGnBu", cbar_kws={"orientation": "horizontal"}) | |
ax.set_xlabel('Labels') | |
ax.set_ylabel('Classifications') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot as plt | |
# histogram of the best residue | |
e_best = all_e[iAIC_best] | |
fig = plt.figure(figsize=(12, 9), dpi=80) | |
ax = fig.gca() | |
ax.set_axisbelow(True) | |
ax.hist(e_best, bins=10, density=True, alpha=0.75) | |
plt.xlabel(r'$\xi(n) = y(n)-ŷ(n)$') | |
plt.ylabel('Estimated probability density function') | |
plt.title('Histogram of the best residue') | |
plt.grid(True, linestyle='-.', linewidth=.5) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
fig = plt.figure(figsize=(12, 9), dpi=80) | |
ax = fig.gca() | |
ax.set_axisbelow(True) | |
ax.plot(y_hat[:N], 'y--', zorder=2, label='$\hat{y}[k]$') | |
ax.plot(y[n_best:N], 'k', zorder=1, label='$y[k]$') | |
ax.set_xticks(arange(furnas[:3*12].size), labels=('jan', 'fev', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'out', 'nov', 'dez')*3, rotation=45, fontsize=15) | |
ax.legend(loc="upper right") | |
ax.grid(True, linestyle='-.', linewidth=.5) | |
plt.xlabel(r'$k$') | |
plt.title('OLS algorithm output signal') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
fig = plt.figure(figsize=(12, 9), dpi=80) | |
ax = fig.gca() | |
ax.set_axisbelow(True) | |
ax.stem(all_data['avancar_4_p10_s1a']['all_a'][:100], markerfmt='o', label='Mine') | |
ax.stem(all_data['avancar_4_p10_s1a']['all_a_hat'][:100], markerfmt='x', label='Built-in function') | |
plt.legend(loc="upper right") | |
plt.title('Yule-Walker comparison of AR(p) coefficients:\nMy from-scratch code vs. built-in Python function') | |
plt.grid(True, linestyle='-.', linewidth=.5) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.plot as plt | |
fig, axs = plt.subplots(2, 1, figsize=(8, 8)) | |
axs[0].stem(np.abs(c_k_class1[:200])) | |
axs[0].set_title('Class 1') | |
axs[1].stem(np.abs(c_k_class2[:200])) | |
axs[1].set_title('Class 2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
fig = plt.figure(figsize=(12, 3), dpi=80) | |
ax = fig.gca() | |
# hide axes | |
fig.patch.set_visible(False) | |
ax.axis('off') | |
ax.axis('tight') | |
col_labels = [f'ARX{n,m}' for n, m in zip(all_n, all_m)] | |
row_labels = ['AIC', 'BIC'] | |
cell_tex = [[f'{i:.2e}' for i in x] for x in (all_AIC, all_BIC)] | |
table_ = ax.table(cellText=cell_tex, colLabels=col_labels, loc='center', cellLoc='center', rowLabels=row_labels) | |
table_.scale(1, 4) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot as plt | |
plt.rcParams.update({'font.family': 'DejaVu Sans', 'font.weight': 'normal', 'font.size': 22}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment