难以使用 oop python 的 numpy 数组
Difficult with numpy array using oop python
ValueError:x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (0,)。 foor 循环实例的值未存储在方法 init
中
import numpy as np
import matplotlib.pyplot as plt
class inter():
def __init__(self):
self.x = np.array([0, 20, 40, 60, 80, 100], float)
self.y = np.array([20.0, 48.6, 61.6, 71.2, 74.8, 75.2], float)
self.xplt = np.linspace(self.x[0], self.x[-1])
self.yplt = np.array([], float)
self.yp = 0
def loop(self):
for xp in self.xplt:
for xi, yi in zip(self.x, self.y):
self.yp += yi * np.prod((xp - self.x[self.x != xi]) / (xi - self.x[self.x != xi]))
self.yplt = np.append(self.yplt, self.yp)
return self.yplt
def draw(self):
plt.plot(self.xplt, self.yplt)
plt.show()
int = inter()
int.draw()
我想你忘记打电话了:int.loop()
它对我有用:在 int.draw()
之前调用 int.loop()
或在 __init__()
中调用 self.loop()
import numpy as np
import matplotlib.pyplot as plt
class inter():
def __init__(self):
self.x = np.array([0, 20, 40, 60, 80, 100], float)
self.y = np.array([20.0, 48.6, 61.6, 71.2, 74.8, 75.2], float)
self.xplt = np.linspace(self.x[0], self.x[-1])
self.yplt = np.array([], float)
self.yp = 0
self.loop()
def loop(self):
for xp in self.xplt:
for xi, yi in zip(self.x, self.y):
self.yp += yi * np.prod((xp - self.x[self.x != xi]) / (xi - self.x[self.x != xi]))
self.yplt = np.append(self.yplt, self.yp)
return self.yplt
def draw(self):
plt.plot(self.xplt, self.yplt)
plt.show()
int = inter()
int.draw()
ValueError:x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (0,)。 foor 循环实例的值未存储在方法 init
中import numpy as np
import matplotlib.pyplot as plt
class inter():
def __init__(self):
self.x = np.array([0, 20, 40, 60, 80, 100], float)
self.y = np.array([20.0, 48.6, 61.6, 71.2, 74.8, 75.2], float)
self.xplt = np.linspace(self.x[0], self.x[-1])
self.yplt = np.array([], float)
self.yp = 0
def loop(self):
for xp in self.xplt:
for xi, yi in zip(self.x, self.y):
self.yp += yi * np.prod((xp - self.x[self.x != xi]) / (xi - self.x[self.x != xi]))
self.yplt = np.append(self.yplt, self.yp)
return self.yplt
def draw(self):
plt.plot(self.xplt, self.yplt)
plt.show()
int = inter()
int.draw()
我想你忘记打电话了:int.loop()
它对我有用:在 int.draw()
之前调用 int.loop()
或在 __init__()
self.loop()
import numpy as np
import matplotlib.pyplot as plt
class inter():
def __init__(self):
self.x = np.array([0, 20, 40, 60, 80, 100], float)
self.y = np.array([20.0, 48.6, 61.6, 71.2, 74.8, 75.2], float)
self.xplt = np.linspace(self.x[0], self.x[-1])
self.yplt = np.array([], float)
self.yp = 0
self.loop()
def loop(self):
for xp in self.xplt:
for xi, yi in zip(self.x, self.y):
self.yp += yi * np.prod((xp - self.x[self.x != xi]) / (xi - self.x[self.x != xi]))
self.yplt = np.append(self.yplt, self.yp)
return self.yplt
def draw(self):
plt.plot(self.xplt, self.yplt)
plt.show()
int = inter()
int.draw()