Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyqpanda-algorithm/pyqpanda_alg/QSVR/QSVR.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def get_res(self):
def show_res(self):
svr = SVR(kernel=self.k_kernel, gamma=0.1)
svr.fit(self.x, self.y)
x0_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 1]), 30)
x1_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 1]), 30)
x0_test = np.linspace(min(self.x[:, 0]), max(self.x[:, 0]), 30)
x1_test = np.linspace(min(self.x[:, 1]), max(self.x[:, 1]), 30)
X0_test, X1_test = np.meshgrid(x0_test, x1_test)
X_test = np.c_[X0_test.ravel(), X1_test.ravel()]
y_pred = svr.predict(X_test).reshape(X0_test.shape)
Expand Down
58 changes: 58 additions & 0 deletions test/QAlgBase/Test_class_qsvr_Quantum_SVR.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,43 @@
import numpy as np
import os
from pyqpanda_alg.QSVR import Quantum_SVR
from pyqpanda_alg.QSVR import QSVR as qsvr_module
import warnings
import os


class _RecordingSVR:
def __init__(self):
self.seen = []

def fit(self, X, y):
return self

def predict(self, X):
X = np.asarray(X)
self.seen.append(X)
return np.zeros(len(X))


class _FakeAxes:
def __getattr__(self, name):
return lambda *args, **kwargs: None


class _FakeFigure:
def add_subplot(self, *args, **kwargs):
return _FakeAxes()


def _grid_points(monkeypatch, qsvr):
svr = _RecordingSVR()
monkeypatch.setattr(qsvr_module, "SVR", lambda **kwargs: svr)
monkeypatch.setattr(qsvr_module.plt, "figure", lambda *args, **kwargs: _FakeFigure())
monkeypatch.setattr(qsvr_module.plt, "show", lambda *args, **kwargs: None)
qsvr.show_res()
return svr.seen[0]


class Test_class_qsvr_Quantum_SVR:

def setup_method(self):
Expand Down Expand Up @@ -38,6 +71,31 @@ def test_interface11_show_res_basic(self):
except Exception as e:
pytest.fail(f"show_res()方法执行失败: {e}")

def test_show_res_grid_matches_column_ranges(self, monkeypatch):
t = np.random.randn(60)
X = np.column_stack([t + 0.05 * np.random.randn(60), 20 * t + np.random.randn(60)])
qsvr = Quantum_SVR(X, np.sin(t))

points = _grid_points(monkeypatch, qsvr)

assert points.shape == (900, 2)
for col in (0, 1):
axis = np.unique(points[:, col])
assert len(axis) == 30
expected = np.linspace(qsvr.x[:, col].min(), qsvr.x[:, col].max(), 30)
assert np.allclose(axis, expected)

def test_show_res_grid_axes_are_independent(self, monkeypatch):
x = np.column_stack([np.linspace(0, 2, 40), np.linspace(10, 30, 40)])
qsvr = Quantum_SVR(x, np.zeros(40))
# bypass the scaler/PCA so the two columns keep exact, unmistakable ranges
qsvr.x = x

points = _grid_points(monkeypatch, qsvr)

assert np.allclose([points[:, 0].min(), points[:, 0].max()], [0.0, 2.0])
assert np.allclose([points[:, 1].min(), points[:, 1].max()], [10.0, 30.0])


if __name__ == "__main__":
# 运行测试
Expand Down