我想将两个数组(x 和 y)转换为频率nxn矩阵(n = 5),指示每个单元格包含的点数。它包括将两个变量重新采样为五个间隔,并计算每个单元格的现有点数。
我曾尝试使用 pandas pivot_table但不知道引用每个轴坐标的方式。X 和 Y 数组是两个因变量,包含 0 到 100 之间的值。
我真的很感激有人的帮助。非常感谢您提前。
这是代码的示例:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Arrays example. They are always float type and ranging 0-100. (n_size array = 15)
x = 100 * np.random.random(15)
y = 100 * np.random.random(15)
# Df created for trying to pivot and counting values per cell
df = pd.DataFrame({'X':x,'Y':y})
# Plot the example …Run Code Online (Sandbox Code Playgroud) 我试图定义一个装饰器来执行一个类方法,首先尝试它,如果检测到错误,则提出它并提及失败的方法,以便用户可以看到哪个方法是错误的。
在这里,我展示了我的代码的MRE(最小的、可重现的示例)。
from functools import wraps
def trier(func):
"""Decorator for trying A-class methods"""
@wraps(func)
def inner_func(self, name, *args):
try:
func(self, *args)
except:
print(f"An error apeared while {name}")
return inner_func
class A:
def __init__(self):
self._animals = 2
self._humans = 5
@trier('getting animals')
def animals(self, num):
return self._animals + num
@trier('getting humans')
def humans(self):
return self._humans
A().animals
Run Code Online (Sandbox Code Playgroud)
出现许多错误,例如:
类型错误:inner_func() 缺少 1 个必需的位置参数:“名称”
或者误解 self class 与 self function 。