据我所知,TDD的典型工作流程基于黑盒测试.首先我们定义接口然后编写一个或一组测试,然后我们实现通过所有测试的代码.那么看下面的例子:
from abc import ABCMeta
class InterfaceCalculator:
__metaclass__ = ABCMeta
@abstractmethod
def calculate_mean(self):
pass
Run Code Online (Sandbox Code Playgroud)
示例性测试案例
from unittest import TestCase
class TestInterfaceCalculator(TestCase):
def test_should_correctly_calcluate_mean(self):
X=[1,1]
expected_mean = 1
calcluator =Calculator()
self.assertAlmostEqual(calculator.calculate_mean(X), expected_mean)
Run Code Online (Sandbox Code Playgroud)
我跳过类Calculator(InterfaceCalculator)的实现,因为它是微不足道的.
以下想法很容易理解.机器学习怎么样?让我们考虑以下示例.我们想实现猫狗照片分类器.从界面开始.
from abc import ABCMeta
class InterfaceClassifier:
__metaclass__ = ABCMeta
@abstractmethod
def train_model(self, data):
pass
@abstractmethod
def predict(self, data):
pass
Run Code Online (Sandbox Code Playgroud)
我准备了很多单元测试
from unittest import TestCase
class TestInterfaceCalculator(TestCase):
def __init__(self):
self.model = CatDogClassifier()
def test_should_correctly_train_model(self, data):
"""
How can be implemented?
"""
self.model.train_model(data)
def test_should_correctly_calcluate_mean(self):
input ="cat.jpg"
expected_result = …Run Code Online (Sandbox Code Playgroud) 我想模拟返回void的函数:
class FileDownloaderMock : public FileDownloader
{
public:
MOCK_CONST_METHOD1(downloadFile,
void(data *data_ptr));
};
Run Code Online (Sandbox Code Playgroud)
在测试用例中,我想两次调用此函数,首先应引发异常,第二次应可以正常工作。所以我的测试用例看起来像这样,但是不起作用。
TEST_F(BCtrlTargetBdDownloaderTests, DownloaderShouldCorrectlyDownloadTargetBdInFirstAttempt)
{
EXPECT_CALL(m_fileDownloader, downloadFile( DataReqMatcher(l_expectedReq) ) ).Times(2)
.WillOnce(Throw(UpgradeException("Download failed") ));
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
到EXPECT_CALL(m_fileDownloader,downloadFile(DataReqMatcher(l_expectedReq)))中指定的几个操作...预期被调用两次,但只有1个WillOnce()。
那么我该如何解决这个难题呢?最好的祝福
现在我想在单独的画布上为每个功能制作boxplot.分离条件是第一列.我有类似的直方图图(下面的代码),但我不能为boxplot制作工作版.
hist_params = {'normed': True, 'bins': 60, 'alpha': 0.4}
# create the figure
fig = plt.figure(figsize=(16, 25))
for n, feature in enumerate(features):
# add sub plot on our figure
ax = fig.add_subplot(features.shape[1] // 5 + 1, 6, n + 1)
# define range for histograms by cutting 1% of data from both ends
min_value, max_value = numpy.percentile(data[feature], [1, 99])
ax.hist(data.ix[data.is_true_seed.values == 0, feature].values, range=(min_value, max_value),
label='ghost', **hist_params)
ax.hist(data.ix[data.is_true_seed.values == 1, feature].values, range=(min_value, max_value),
label='true', **hist_params)
ax.legend(loc='best')
ax.set_title(feature)
Run Code Online (Sandbox Code Playgroud)