<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Royi haddad on Medium]]></title>
        <description><![CDATA[Stories by Royi haddad on Medium]]></description>
        <link>https://medium.com/@royihaddad?source=rss-aca29e882926------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SNDYojFEreRFAEZKoy2aAg.jpeg</url>
            <title>Stories by Royi haddad on Medium</title>
            <link>https://medium.com/@royihaddad?source=rss-aca29e882926------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:12:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@royihaddad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Predicting Heart Attack with Decision Trees]]></title>
            <link>https://medium.com/@royihaddad/predicting-heart-attack-with-decision-trees-89f7ddf6f55f?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/89f7ddf6f55f</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Thu, 23 Nov 2023 16:32:28 GMT</pubDate>
            <atom:updated>2023-11-23T16:32:28.112Z</atom:updated>
            <content:encoded><![CDATA[<p>Decision trees are one of the most commonly used machine learning algorithms for classification because they are Explainable, Can capture complex non-linear relationships between features and targets, work well with categorical features without having to one-hot encode, they are easy and fast to train.</p><p>Let’s walk through a simple tutorial on how to predict Heart Attacks with a decision tree. We will cover some data preparation and ways to optimize our classifier.</p><blockquote>Project packages</blockquote><p>Lets import the packages we will use</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>from pandas import DataFrame, read_csv<br>from sklearn.tree import DecisionTreeClassifier<br>from sklearn.model_selection import train_test_split<br>from sklearn import metrics<br>from sklearn.tree import plot_tree</pre><h3>Understanding the data</h3><p>With any machine learning task, we want to have enough data and a wide distribution of the data for training such that our models can generalize well. It is worth mentioning that with Decision Trees we can work with messy data, missing values, and without the need to normalize values and preprocess categorical features.</p><p>Let&#39;s see a sample of our data and understand it at a high level</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c79cb8be71dca1e7257ac1652284e593/href">https://medium.com/media/c79cb8be71dca1e7257ac1652284e593/href</a></iframe><p>I have named the columns such that they are self-explanatory but some are not just numerical, they represent some classes. For example, chest pain has the following distribution</p><pre># Chest-Pain distribution<br>1: typical angina, 2: atypical angina, 3: non-anginal pain, 4: asymptomatic</pre><p>We can see another example in the blood-sugar</p><pre># Concentration of sugar in the blood. Over 120 mg/dl (Milligrams per deciliter)<br>1: True, 0: False,</pre><p>Let’s import the data, review the distribution of our data, and identify red flags</p><pre>medical_records: DataFrame = read_csv(&quot;medical_records.csv&quot;)<br><br>medical_records.hist(figsize=(16, 20))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MRE3wyTXSmq_FsHH189wfA.png" /></figure><p>By analyzing the distribution we can decide if we want to drop certain data ranges or in some cases even features (Assuming they have a low correlation with predicting the output).</p><p>We can see that the age distribution does not include individuals younger than 30. This is a healthy distribution because the overall likelihood of heart attacks occurs at later stages in life. Adding younger individuals might not add any value to our model</p><p>Cholesterol is an interesting one given the following levels. 200 or less is associated with healthy hearts. 200–239 is associated with at-risk (meaning there is some level of risk to a heart). 240 and up is associated with heart-related events. We can view the Cholesterol distribution next.</p><pre>medical_records.cholesterol.describe()<br><br>count 303.000000<br>mean 246.264026<br>std 51.830751<br>min 126.000000<br>25% 211.000000<br>50% 240.000000<br>75% 274.500000<br>max 564.000000</pre><p>We can see that around 50% will be in the Danger zone (240+).</p><h3>Model Training</h3><p>Let’s split the data set into training and testing. We are going to use a 70/30 ratio for our use case but I recommend testing different ratios, for certain data sets it has some small impact on model accuracy.</p><blockquote>Data Split</blockquote><pre>features: DataFrame = medical_records[[<br>  &quot;age&quot;, <br>  &quot;sex&quot;, <br>  &quot;chest-pain&quot;, <br>  &quot;resting-blood-pressure&quot;, <br>  &quot;cholesterol&quot;, <br>  &quot;blood-sugar&quot;, <br>  &quot;electrocardiographic&quot;, <br>  &quot;max-heart-rate&quot;<br>]]<br>targets: DataFrame = medical_records.output<br><br>train_features, test_features, train_targets, test_targets = train_test_split(features, targets, test_size=0.3, random_state=1)</pre><blockquote>Model and Training</blockquote><pre># Create Decision Tree classifer object<br>model: DecisionTreeClassifier = DecisionTreeClassifier()<br><br># Train Decision Tree Classifer<br>model.fit(train_features, train_targets)<br><br>#Predict the response for test dataset<br>predictions: np.ndarray = model.predict(test_features)</pre><blockquote>Evaluation</blockquote><pre>accuracy: float = metrics.accuracy_score(test_targets, predictions)</pre><p>We should get an accuracy score of <strong>0.6593406593406593</strong></p><blockquote>Improvements</blockquote><p>There are several methods we can use to increase our accuracy.</p><p>Pruning — We can reduce the number of nodes and the maximum tree depth.</p><pre># Create a model with max tree depth of 3 nodes<br>model: DecisionTreeClassifier = DecisionTreeClassifier(max_depth=3)</pre><p>This has increased our accuracy to <strong>0.7252747252747253</strong></p><h3>Visualization</h3><p>Being able to explain our ML model is one of the advantages of Decision Trees. We can use Sklearn plot tree to visualize our tree and the nodes</p><pre>plt.figure(figsize=(50, 50))<br>plot_tree(model, <br>          feature_names = [<br>              &quot;age&quot;, <br>              &quot;sex&quot;, <br>              &quot;chest-pain&quot;, <br>              &quot;resting-blood-pressure&quot;, <br>              &quot;cholesterol&quot;, &quot;blood-sugar&quot;, <br>              &quot;electrocardiographic&quot;, <br>              &quot;max-heart-rate&quot;<br>          ], <br>          class_names = [&quot;True&quot;, &quot;False&quot;], <br>          filled = True, <br>          rounded = True)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k3bJ5qVYEazfcK2O7bpRMQ.png" /></figure><h3>Conclusion</h3><p>Decision Trees are a great option when you need to explain how your model made a prediction and when your data set is far from perfect.</p><p>They are heavily used in industries that are regulated like Insurance. Denying claims or Policy purchases has to be supported by a clear reason</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89f7ddf6f55f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Predicting Companies Profits with Linear Regression]]></title>
            <link>https://medium.com/@royihaddad/predicting-companies-profitswith-linear-regression-7a314eb51987?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a314eb51987</guid>
            <category><![CDATA[machine-learning-ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Mon, 06 Nov 2023 20:16:59 GMT</pubDate>
            <atom:updated>2023-11-08T02:38:05.177Z</atom:updated>
            <content:encoded><![CDATA[<h3>Predicting Companies&#39; Profits with Linear Regression</h3><h3>Introduction</h3><p>Generative AI is definitely a game changer but for most business problems good old-fashioned machine learning models like regression and classification are still the go-to.</p><p>Imagine how investors like Private Equity or VCs would make use of machine learning. To answer such a question we first must understand what data investors look at and how is it used. Investing in companies is not done only by quantifiable data like spending, growth, and burn rate but also with qualitative data like the founders&#39; records, customer feedback, the product experience, and more</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PnY6QgJ4zVTizIAXFY6NQQ.png" /></figure><p>This article will get you started with Linear Regression and you can find the complete code <a href="https://github.com/RoyiHD/linear-regression">here</a></p><h3>Project Setup</h3><p>We will be using Jupyter Notebook for this purpose (This is not a tutorial on Jupyter Notebooks). Let&#39;s import a few libraries</p><blockquote>Imports</blockquote><pre># Drawing graphs<br>import matplotlib.pyplot as plt<br># Data management and processing<br>from pandas import DataFrame<br># Drawing heatmaps<br>import seaborn as sns<br># Analytics<br>from sklearn.metrics import r2_score<br># Data management for training and testing<br>from sklearn.model_selection import train_test_split<br># Importing the linear model<br>from sklearn.linear_model import LinearRegression<br># Code annotation <br>from typing import List</pre><h3>The Data</h3><p>We will be working with regional data to simplify the problem space. The data represents companies&#39; spending categories and profits. We can see a sample of the different data points we have. We want to use the spending data to train a linear regression and predict Profits.</p><blockquote>It is important to understand that the data we will use describe the spending of a company. Any meaningful prediction power will come when we combine spending data with data like Revenue Growth, Local Taxes, Amortization, and market conditions.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f8af98dde56c734b88ac57435c5b399/href">https://medium.com/media/5f8af98dde56c734b88ac57435c5b399/href</a></iframe><blockquote>Load Data</blockquote><pre>companies: DataFrame = pd.read_csv(&quot;companies.csv&quot;, header = 0)</pre><h4>Visualization</h4><p>Understanding the data is important to know which features to use, which features require normalization and transformation, remove outliers from the data, and what processing we should be doing on specific data points</p><blockquote>Targets (Profits) Histogram</blockquote><p>We can use the DataFrame directly to draw a histogram. (Pandas make use of Matplotlib to draw data frames). We can access the Profits directly and plot it.</p><pre>companies[&#39;Profit&#39;].hist( color=&#39;g&#39;, bins=100);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oxa9tprvSIiruKTkAGXnDQ.png" /></figure><p>We can see that there are very few outliers with profits that go beyond $200,000.00. We can also assume from this that our data represents companies that are at a specific size. Because the number of outliers is relatively small, we can keep them in place.</p><blockquote>Features (Spending) Histograms</blockquote><p>Here we want to see the histograms of our features and view the distribution. The Y axis represents the number frequency and the X axis represents the spend.</p><pre>companies[[<br>  &quot;R&amp;D Spend&quot;, <br>  &quot;Administration&quot;, <br>  &quot;Marketing Spend&quot;<br>]].hist(figsize=(16, 20), bins=50, xlabelsize=8, ylabelsize=8)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*op4CCVcCfXftgw8_0IqxyA.png" /></figure><p>We can also see a healthy distribution with a small number of outliers. Acting on intuition we can expect companies that spend more on R&amp;D and Marketing to have higher profits. Looking at the scatter plot below, we see a clear correlation between R&amp;D spending and profits</p><pre><br>profits: DataFrame = companies[[&quot;Profit&quot;]]<br>research_and_development_spending: DataFrame = companies[[&quot;R&amp;D Spend&quot;]]<br><br>figure, ax = plt.subplots(figsize = (9, 9))<br>plt.xlabel(&quot;R&amp;D Spending&quot;)<br>plt.ylabel(&quot;Profits&quot;)<br>ax.scatter(<br>  research_and_development_spending, <br>  profits, <br>  s=60, <br>  alpha=0.7, <br>  edgecolors=&quot;k&quot;,<br>  color=&#39;g&#39;,<br>  linewidths=0.5<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*SSnZc-1Zu_3rgs6p2EHsPg.png" /></figure><p>We can explore further the correlation between our Spending and Profits with a correlation heatmap. We can see that R&amp;D and Marketing spending is more correlated with Profits than administration spending.</p><pre>sns.heatmap(companies.corr())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*17lXdxq2hiYZmV0SOxuybQ.png" /></figure><h3>Model Training</h3><p>We first need to split our data set into training and testing. Sklearn offers a helper method to do just that. Given that our data set is simple and small enough we can separate the features from the targets in the following way.</p><blockquote>Data Sets</blockquote><pre>features: DataFrame = companies[[<br>    &quot;R&amp;D Spend&quot;, <br>    &quot;Administration&quot;, <br>    &quot;Marketing Spend&quot;,<br>]]<br>targets: DataFrame = companies[[&quot;Profit&quot;]]<br><br>train_features, test_features, train_targets, test_targets = train_test_split(<br>  features, <br>  targets,<br>  test_size=0.2<br>)</pre><p>Most data scientists will use different naming conventions like X_train, y_train, or other similar variations. I prefer explicitness.</p><blockquote>Model Training</blockquote><p>We can now create and train our model. Sklearn makes things very simple</p><pre>model: LinearRegression = LinearRegression()<br>model.fit(train_features, train_targets)</pre><h3>Model Evaluation</h3><p>We want to evaluate our model performance and its usability. First, let’s look at the computed coefficients. In machine learning the coefficients are learned weights or numbers that we use to multiply each feature by. We expect to see a learned coefficient per feature.</p><pre>coefficients = model.coef_<br><br>&quot;&quot;&quot;<br>We should see the following in our console<br><br>Coefficients  [[0.55664299 1.08398919 0.07529883]]<br>&quot;&quot;&quot;</pre><p>As we can see there are 3 coefficients, one per feature (“R&amp;D Spend”, “Administration”, “Marketing Spend”). We can also plot this to get a more visual understanding of each coefficient</p><pre>plt.figure()<br>plt.barh(train_features.columns, coefficients[0])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Yig8cNUeri2uP2yaLoqDA.png" /></figure><blockquote>Calculating Error</blockquote><p>We want to understand the error rate of our model. We are going to use the Sklearn R2 score</p><pre>test_predictions: List[float] = model.predict(test_features)<br>root_squared_error: float = r2_score(test_targets, test_predictions)<br>&quot;&quot;&quot;float<br>We should see an ouput similar to this<br>0.9781424529214315<br>&quot;&quot;&quot;</pre><p>The closer we are to 1 the more accurate our model is. We can actually test this in a very naive way.</p><p>We can take the first row of our dataset. If we use the model to predict the Profit using the below spending, we expect a number that is close enough to $192, 261</p><pre>&quot;R&amp;D Spend&quot; |  &quot;Administration&quot; |  &quot;Marketing Spend&quot; | &quot;Profit&quot;<br>165349.2       136897.8            471784.1            192261.83</pre><p>Let’s create an inference request.</p><pre>inference_request: DataFrame = pd.DataFrame([{<br>  &quot;R&amp;D Spend&quot;:165349.2, <br>  &quot;Administration&quot;:136897.8, <br>  &quot;Marketing Spend&quot;:471784.1 <br>}])</pre><p>Let’s run the model</p><pre>inference: float = model.predict(inference_request)<br>&quot;&quot;&quot;<br>We should get a number that is around<br>199739.88721901<br>&quot;&quot;&quot;</pre><p>We now see the margin of error by abs(199739–192261) / 192261 = 0.0388. This is pretty accurate.</p><h3>Conclusion</h3><p>There are many ways to process data, build models, and analyze them. There is no one solution that fits all and one of the key processes when solving business problems with Machine Learning is building multiple models that all aim to solve the same problem and picking the one that is most promising.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a314eb51987" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything you need to run a Production Python Application]]></title>
            <link>https://medium.com/@royihaddad/everything-you-need-to-run-a-production-python-application-3a15814d222b?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a15814d222b</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Wed, 01 Nov 2023 15:58:12 GMT</pubDate>
            <atom:updated>2023-11-01T15:58:12.823Z</atom:updated>
            <content:encoded><![CDATA[<p>Building and running a production-grade Python web application involves many little pieces but there is a set of six fundamental building blocks that should be part of every web application.</p><p>I have put them all together for you to make it easy to start and scale your web app. You can find the complete project <a href="https://github.com/RoyiHD/python-basic-app">here</a></p><h4>Request Response Validation</h4><p>Every request that comes into our web application should be validated. Validation means that we have defined a request schema as a DTO (Pydantic BaseModel) and that on each request we can check if the request data schema matches our defined schema.</p><p>We have created a decorator that makes it easy to validate any request to a defined DTO schema</p><pre># imports ...<br><br># Define an incoming request DTO schema<br>class FooRequest(BaseModel):<br>  <br>    user_id: str<br><br># Define an outgoing response DTO schema<br>class FooResponse(BaseModel):<br>  <br>    message: str<br><br><br># API Route<br>@app.post(&quot;/api/v1/foo&quot;)<br>@request_dto(FooRequest)<br>@response_dto(FooResponse)<br>def foo_route(request_dto: FooRequest) -&gt; FooResponse:<br>    return FooResponse(...)</pre><h4>Application Environment Variable Management</h4><p>Environment variables are injected into a web application on server start time. The injection is usually done as part of the infrastructure setup. Once injected we need to make use of them in code. We will be using Pydantic Settings to manage all environment variables. We can create different classes with different prefixes to structure our variables.</p><p>In our below example, we expect an environment variable to be injected as</p><pre>SOME_FOO_VARIABLE=&quot;something&quot;</pre><pre># Imports ...<br><br>class FooSettings(BaseSettings):<br><br>    variable: str<br><br>    class Config:<br>        env_prefix: str = &quot;SOME_FOO_&quot;</pre><h4>Testing</h4><p>Although testing is a wide topic, our aim was to create a basic setup to add fixtures and route testing. The reason we decided to keep it bare bone was because every app will have different variations of unit testing choices and we did not want to be highly opinionated. We want users to build on top of the basic setup. We have elected to use Pytest as our framework for testing because it is well-supported and simple</p><p>We can define some fixtures as</p><pre># imports ...<br><br># This assumes Flask app but can be adapated accordignly<br>@pytest.fixture(scope=&quot;module&quot;)<br>def test_client() -&gt; Generator[FlaskClient, None, None]:<br>    <br>    # This can be changed once we have created our app<br>    app: Flask = None # create_app()<br><br>    with app.test_client() as test_client:<br>        yield test_client</pre><p>We defined some route tests as</p><pre># Imports ...<br><br>def test_example(test_client: FlaskClient) -&gt; None:<br>    &quot;&quot;&quot;<br>        This assumes a flask route in place.<br>    &quot;&quot;&quot;<br>    response: TestResponse = test_client.get(&quot;/api/v1/example&quot;)<br><br>    assert response.status_code == 201<br>    assert response.text == &quot;Hello from example&quot;</pre><h4>Linting</h4><p>We have elected to use Mypy and Pylint for liniting and type checking. We have added basic configurations for both but you should customize them based on your individual needs. The confirmation can be found in setup.cfg file as follows</p><pre>[pylint.]<br># Naming style matching correct class names.<br>class-naming-style=PascalCase<br><br># Maximum number of characters on a single line.<br>max-line-length=120<br><br># String used as indentation unit. This is usually &quot;    &quot; (4 spaces) or &quot;\t&quot; (1<br># tab).<br>indent-string=&quot;    &quot;<br><br><br>[mypy]<br>warn_return_any = True</pre><h4>CLI</h4><p>We have chosen to include CLI although it is mostly used for local usage and during CI processes. We wanted users to have an interface to perform different tasks like database setup, invocations of internal processes, and more without running the entire application. We have selected click as the framework and created a basic setup for users to build on</p><pre># imports ... <br><br>@click.group(&quot;inference-tools&quot;)<br>def cli() -&gt; None:<br>    <br>    &quot;&quot;&quot; Inference related commands &quot;&quot;&quot;<br>    return<br><br><br>@cli.command(&quot;run&quot;, help=&quot;inference run&quot;)<br>@click.option(<br>    &quot;-r&quot;, <br>    &quot;--run&quot;, <br>    &quot;run&quot;, <br>    type=click.BOOL, <br>    help=&quot;run option&quot;<br>)<br>def run(run: bool) -&gt; None:<br>    click.echo(&quot;Running inference %s &quot; % run)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    cli()</pre><h4>Logging and Monitoring</h4><p>Both monitoring and logging usage configurations may vary based on your chosen framework for dumping logs and metrics. We have created a basic setup where you can configure handlers and loggers and easily import them.</p><p>We have not taken steps to define metrics as part of this release. We are considering adding Prometheus as a basic setup.</p><p>You can see the basic configuration of the logger. It is possible to define multiple loggers and invoke the get_logger function with a specific name</p><pre># imports ...<br><br>logging.basicConfig(<br>    level=logging.DEBUG,<br>    format=&quot;%(asctime)s - %(name)s - %(levelname)s - %(message)s&quot;<br>    # Customize handlers and loggers<br><br>    # handlers = {},<br><br>    # loggers = {},<br>)<br><br>def get_logger() -&gt; Logger:<br>    return logging.getLogger(__name__)</pre><h4>Conclusion</h4><p>We hope you will find this article and the repo useful as you build Python web applications. Our aim is to continue and improve the code base such that it becomes a framework that is somewhat opinionated.</p><p>You can access the complete repository <a href="https://github.com/RoyiHD/python-basic-app">here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a15814d222b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[If Only They Had Idempotency]]></title>
            <link>https://medium.com/@royihaddad/if-only-they-had-idempotency-0520044b4723?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/0520044b4723</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Sun, 29 Oct 2023 19:53:14 GMT</pubDate>
            <atom:updated>2023-10-29T21:59:35.012Z</atom:updated>
            <content:encoded><![CDATA[<p>During a visit to Sweden in 2022 we were waiting to take a corona test in some local clinic. We need the test to travel back to the US. As we were done with the tests and ready to pay, we inserted our card into the clinic payment processing machine and encountered some errors with the processing. It seems the clinic system did not provide confirmation after the first try and the receptionist kept hitting the charge button in her system.</p><p>Long story short, we woke up the next day with four similar charges from the clinic and spent an hour on the phone trying to get them reversed. I can only imagine how many other people suffered the same experience.</p><p>In certain products and workflows, idempotency is not an option but an obligation how should we think about incorporating Idempotency and how should we implement one?</p><h4>The User Experience</h4><p>The clinic user experience looked something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SmjvFYx46lo-0yiDQrSj_Q.png" /></figure><p>Like most systems, the clinic software was not managing their payment processing but instead relying on several existing payment processing like Stripe. What is the relationship between systems like the clinic CRM and payment processing? Who is responsible for incorporating Idempotency into the charge flow and how is it implemented?</p><h4>Idempotent Transactions</h4><p>An idempotent transaction is one where we want to guarantee that only one process will happen in a given session(E.g. Customers will only be charged once for a transaction). It is usually based on a unique identifier (<strong>token</strong>) that is passed around between the product systems (client) and the processing systems (server)</p><p>We can consider three possibilities for breakage in the communication between product systems and process systems such that the product system would attempt to execute the process again.</p><blockquote><strong><em>Process initiated but did not reach server</em></strong></blockquote><p>The client system will attempt to make a call to the server but the server may not be responding and the request did not make it to the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*b9mCGEpmPkOM_gOUfzwt6w.png" /></figure><blockquote><strong><em>Process in progress</em></strong></blockquote><p>The client request has made it to the server, the server began processing the request but resulted in an error before completing the request</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*CDgVYSHCSqAY7kbj4ekRgg.png" /></figure><blockquote><strong><em>Process completed but success reponse did not reach the client</em></strong></blockquote><p>The client made a request to the server, the server processed the request successfully but was unable to respond to the client, or the response was dropped in the network</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*nul7rNQc814Uxv-BTOd7ew.png" /></figure><h4>End-to-End Idempotent Transaction</h4><p>It is common for clients to not only generate tokens for each unique transaction but also persist it in some sessions to ensure that it can be used even if the client system crashes. We can consider the following scenario. You are making a payment with Clinic CRM, the CRM app has generated the token and initiated the payment. As soon as you hit the pay button, the CRM app crashes but the transaction went through. Without confirmation that the funds were charged, the user may attempt to try again. The server system can only tell us if given an idempotency token a transaction was successful or not.</p><p>We can see an illustration of an end-to-end transaction where the user of the CRM system initiates the purchase flow, the CRM system keeps records of all transactions and their status. Before the CRM makes a request to charge the patient, it records a new transaction and waits for confirmation from the payment system to update the transaction as successful. The server will attempt to retrieve a charge record using the idempotent token and update the client on whether a charge with the same token has been successful or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oKvW67SFDt1iWSnnwIvmHw.png" /></figure><blockquote><em>Client Responsibilities</em></blockquote><p>Generate the idempotency token</p><p>Persist tokens and transaction status</p><p>Handle errors</p><p>Manage user experience to reduce duplicate charges</p><blockquote><em>Server Responsibilities</em></blockquote><p>Persist idempotency token</p><p>Process payments</p><p>Map idempotency tokens to process payments</p><p>Provide a dedicated API for idempotent requests</p><p>Handle duplicate requests and client responses</p><h4>Idempotent Server Implementation</h4><p>This is a simplified implementation in Python that does not use a distributed database to persist and access idempotent token charges. It is meant to illustrate the responsibility of the server. You can view the complete code <a href="https://github.com/RoyiHD/idempotent-service">here</a></p><h4>Conclusion</h4><p>Idempotent workflows are a must in certain situations. The responsibility to ensure a once-and-only-once transaction lies both on the client and the server. Each has its own area of responsibility.</p><p>I recommend readers to take this to the next level and think about how idempotency can be implemented in a distributed environment where users can access product system from multiple flows (E.g. access your bank account from the web and mobile)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0520044b4723" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python API Request Validator]]></title>
            <link>https://medium.com/@royihaddad/python-api-request-validator-45fb984e1661?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/45fb984e1661</guid>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Wed, 25 Oct 2023 16:35:18 GMT</pubDate>
            <atom:updated>2023-10-25T16:35:18.512Z</atom:updated>
            <content:encoded><![CDATA[<h4>Introduction</h4><p>Building robust and explicit APIs must take into consideration data validation. Validating user requests and responses is a must in an environment where our APIs have to be available 99% of the time, fast, maintainable, and testable. There are other benefits which we will not go into.</p><p>Schema validation in Python is not as trivial as using Typescript but it’s not hard to do.</p><p>This article will take you through a simple yet powerful way to implement request validation for your APIs. You can use the implementation as a baseline and expand based on your needs.</p><h4>Building Blocks</h4><p>We are going to be using three main frameworks and concepts. Pydantic, Flask, and Decorators. This article is not a tutorial on each but we will cover the basics needed to complete this project. I do recommend getting familiar with them before you go through with this implementation.</p><h4>Project Structure</h4><p>Although project structures will change based on your needs, I recommend creating the following structure for our project for the purpose of this article</p><pre>├── README.md<br>├── app.py<br>├── application<br>│   ├── __init__.py<br>│   ├── decorators<br>│   │   ├── __init__.py<br>│   │   ├── request.py<br>│   │   └── response.py<br>│   └── routes<br>│       ├── __init__.py<br>│       └── v1<br>│           ├── __init__.py<br>│           ├── dtos<br>│           │   ├── __init__.py<br>│           │   ├── base_dto.py<br>│           │   ├── request.py<br>│           │   └── response.py<br>│           └── route.py<br>└── setup.py</pre><h4>Project Setup</h4><p>Let&#39;s start by creating a new folder with the setup file</p><pre>from setuptools import find_packages, setup<br><br>project = &quot;Foo&quot;<br>version = &quot;0.1.0&quot;<br>setup(<br>    name = project,<br>    version = version,<br>    description = &quot;&quot;,<br>    author = &quot;Foo Corp&quot;,<br>    packages = find_packages(exclude=[&quot;*.tests&quot;]),<br>    include_packages_data = True,<br>    install_requires = [<br>        &quot;Flask==3.0.0&quot;,<br>        &quot;pydantic==2.4.2&quot;<br>    ]<br>)</pre><p>We can run this file from our project folder. You should have a virtual environment activated before we run the script.</p><blockquote><em>Install requirements</em></blockquote><pre>pip install .</pre><blockquote><em>Defining Request Schema</em></blockquote><p>We need to define the request schema that we will use to validate incoming requests. We will be using the Pydantic Base Model for this purpose</p><p>First, let’s define a base model that other models can inherit from. The reason we will do so is to make sure that each schema inherited the schema configuration and specifically the ability to detect if extra parameters have been passed in the request</p><p>We shall add a file named base_dto.py in our dtos folder</p><pre>from pydantic import BaseModel, Extra<br><br>class BaseDto(BaseModel):<br><br>    class Config:<br>        extra = Extra.forbid</pre><p>Now let&#39;s define our request schema. We will create a new file in the dtos folder named request.py (You can name it based on CRUD. E.g. create.py, delete.py). We will show a basic schema where we define a low-level field name and a high-level object User. With Pydantic you can create elaborate schemas</p><pre>from pydantic import Field<br>from application.routes.v1.dtos.base_dto import BaseDto<br><br><br>class User(BaseDto):<br><br>    name: str = Field(<br>        alias = &quot;name&quot;,<br>        description = &quot;name of the user&quot;,<br>        example = &quot;John doe&quot;,<br>        title = &quot;Name&quot;<br>    )<br><br><br>class FooRequest(BaseDto):<br><br>    user: User</pre><blockquote><em>Define Response Schema</em></blockquote><p>We also want our API to return a pre-defined validated data model. We will create a response.py file in our dtos folder. Our response schema is basic and will just return a message and datetime information</p><pre>from datetime import datetime<br>from pydantic import Field<br>from application.routes.v1.dtos.base_dto import BaseDto<br><br><br>class FooResponse(BaseDto):<br><br>    created_at: datetime = Field(<br>        alias = &quot;createdAt&quot;,<br>        description = &quot;Request datetime&quot;,<br>        default_factory=datetime.now,<br>        example = &quot;2023-10-24 13:41:09.702907&quot;,<br>        title = &quot;Created At&quot;<br>    )<br>    <br>    message: str = Field(<br>        alias = &quot;message&quot;,<br>        description = &quot;response message&quot;,<br>        example = &quot;Successful request and response validation&quot;,<br>        title = &quot;Message&quot;<br>    )</pre><blockquote><em>Defining Request Validator</em></blockquote><p>On every request we want to capture the request details, specifically the schema that was passed by the client and validate it against our defined FooRequest schema. There are several ways that this can be done. One option is that we can process it in the route itself and do it for each individual route. Although this may work it creates code duplication, if you change validation in one route, you now have to update the rest of the routes to be consistent and there are a few other reasons why this is not preferable.</p><p>A better option is to use decorators and wrap each of our routes. Let’s define our request.py file in the decorators folder. I will not go into details on how decorators work but will point out that the schema validation can be done using the Pydantic parse_obj function which will take a schema as input and try to match it to the BaseModel of FooRequest. If the schema cannot be mapped a ValidationError will be thrown by Pydantic</p><pre>from functools import wraps<br>from flask import g, request<br>from typing import Callable<br>from pydantic import BaseModel<br><br>def request_dto(dto_cls: type[BaseModel]) -&gt; Callable:<br><br>    def decorator(route: Callable) -&gt; Callable:<br><br>        @wraps(route)<br>        def wrapper(*args, **kwargs) -&gt; Callable:<br>            <br>            &quot;&quot;&quot;<br>              Here we not only perform the schema validation using<br>              Pydantic parse_obj but also ensuring that any access to the <br>              request_dto is done in isolation for any given request by <br>              setting g.request_dto to be the parsed schema. It will guarantee <br>              seperation of context between requests<br>            &quot;&quot;&quot;<br>            request_dto = g.request_dto = dto_cls.parse_obj(request.json)<br>            return route(*args, **kwargs, request_dto=request_dto)<br>       <br>        return wrapper<br>    <br>    return decorator</pre><blockquote><em>Defining Response Validator</em></blockquote><p>We want to make sure that we are sending consistent and well-defined responses to our clients. Let’s define a response schema validator to do just that. Our response validator performs the same schema validation as the request using the Pydantic parse_obj function. Two important factors to keep in mind.</p><p>The first is that routes in Python are considered view functions that may have different response values. You should consider adapting the response from invoking the route call to suit your needs.</p><p>The second is that we are looking to return a Response object. We are essentially overriding the route&#39;s return values in the validator, and returning a Response object to the client that includes not only the FooResponse but also standard headers, status-code, message, and more.</p><pre>from functools import wraps<br>from flask import g, jsonify, Response<br>from typing import Callable<br><br>from pydantic import BaseModel<br><br>def response_dto(dto_cls: type[BaseModel]) -&gt; Callable:<br><br>    def decorator(route: Callable) -&gt; Callable:<br><br>        @wraps(route)<br>        def wrapper(*args, **kwargs) -&gt; Response:<br>            &quot;&quot;&quot;<br>              Python routes are view functions which may return different<br>              response values. <br>            &quot;&quot;&quot;<br>          <br>            raw_response = route(*args, **kwargs)<br><br>            response_dto = g.response_dto = dto_cls.parse_obj(raw_response)<br><br>            # We want to return a Response object<br>            response = jsonify(response_dto.dict(by_alias=True))<br>            response.status_code = 200<br><br>            return response<br><br>        return wrapper<br><br>    return decorator</pre><blockquote><em>API Resource</em></blockquote><p>Let&#39;s create our first API resource and route using our defined FooRequest, and FooResponse data models and our request_dto and response_dto data model validators. We will define a route.py file in our routes/V1 and implement our API resource with our decorator functions.</p><pre>from flask import Flask<br><br>from application.decorators import request_dto, response_dto<br>from application.routes.v1.dtos import FooRequest, FooResponse<br><br><br>def add_foo_route(app: Flask) -&gt; None:<br><br>    @app.route(&#39;/api/v1/foo&#39;, methods = [&#39;POST&#39;])<br>    @request_dto(FooRequest)<br>    @response_dto(FooResponse)<br>    def create_foo(request_dto: FooRequest) -&gt; FooResponse:<br><br>        # Process request ...<br><br>        return FooResponse(<br>            message = &quot;Request processed successfuly&quot;,<br>        )</pre><p>We can add multiple routes from our V1 folder. We can test our validation on different routes to make sure it is generic enough. Feel free to add more routes like GET, DELETE, PUT</p><blockquote><em>Adding Routes</em></blockquote><p>There are multiple ways to register API resources with Flask. Our approach creates an add_routes function in the routes folder __init__.py file. We can use add_routes in our app instantiation</p><pre>from flask import Flask<br>from application.routes.v1 import add_foo_route<br><br>def add_routes(app: Flask) -&gt; None:<br><br>    add_foo_route(app)<br><br>__all__ = [<br>    &quot;add_routes&quot;,<br>]</pre><blockquote><em>Basic Flask App</em></blockquote><p>Our goal is to test that our validator works and not necessarily invest in a robust API. We need a basic API POST resource and a way to start a web server. We will be using the Flask development web server for this purpose and define simple function-based API resources.</p><p>Let&#39;s create a file named app.py in the root folder</p><pre>from flask import Flask<br>from application.routes import add_routes<br><br><br>def create_app() -&gt; Flask:<br><br>    app = Flask(__name__, static_folder=None)<br><br>    add_routes(app)<br><br>    return app<br><br><br>if __name__ == &quot;__main__&quot;:<br>    app = create_app()<br><br>    app.run(<br>        host=&quot;0.0.0.0&quot;,<br>        port=3000<br>    )</pre><blockquote><em>Running Flask and Making Requests</em></blockquote><p>Let’s run our Flask server from within our project folder. (Make sure your virtual environment is activated)</p><pre>python app.py</pre><p>We can now make a request. I am using <a href="https://httpie.io/">httpie</a> to make requests from the command line, feel free to use your preferred method. To check that our validators work as expected, we will want to make changes to the request schema we are sending to our server. you can add extra fields, you can remove fields, you can change the data type</p><pre>http POST http://localhost:3000/api/v1/foo \<br>--raw &#39;{<br>    &quot;user&quot;:{<br>        &quot;name&quot;: &quot;foo bar&quot;<br>    }<br>}&#39;</pre><blockquote><em>Conclusion</em></blockquote><p>Although the above example is basic, it can serve as a strong foundation for creating more complicated and robust schema validations in APIs.</p><p>As the next steps, I would recommend that readers take the time to manage server response status codes and messages to clients. Preferably we would throw 422 when requests fail the schema validation.</p><p>The complete code can be found <a href="https://github.com/RoyiHD/python-request-validator">here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45fb984e1661" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Product Market Fit Part 2: How to reach it?]]></title>
            <link>https://medium.com/@royihaddad/product-market-fit-part-2-how-to-reach-it-625502c9577d?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/625502c9577d</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[product-management]]></category>
            <category><![CDATA[product]]></category>
            <category><![CDATA[startup-lessons]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Mon, 13 Mar 2023 14:11:11 GMT</pubDate>
            <atom:updated>2023-03-13T14:14:24.447Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://medium.com/@royihaddad/product-market-fit-part-1-what-is-it-and-what-it-is-not-627cc3b08ab1"><strong>part 1</strong></a> we explored the building blocks of Product Market Fit, what it is, and what it is not. In this part, we will explore how to measure it, and how we can increase the probability of reaching it.</p><p>Let’s explore the business of Home Insurance Policies and see how we can measure the following key metrics. Adoption, Engagement, Retention, Satisfaction</p><h3>User Journey</h3><p>Let’s define the life cycle of a customer</p><p>The customer is in the process of buying a home, the customer uses a bank to take out a mortgage and the bank requires proof of insurance from an insurance company before the mortgage can be provided. The customer searches for and eventually buys an insurance policy to cover his home for 1 year. Three months later a tree falls on the roof of the home. The customer is now using the product (Insurance Policy) he bought from our agency for the first time. He files a claim, his claim gets processed and the roof gets repaired. Time passes and the customer policy is about to expire. The customer needs to decide if to renew the policy and stay as a customer or cancel it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*9L5cvYcG96rJjIzClvqGZg.png" /></figure><h3>What is our Home Insurance Product?</h3><p>Our product is guaranteed contract to process a claim and repair damages to the property defined in the contract. Users will pay an Insurance premium (cost of buying our product) and in exchange, they get a right to “activate“ the contract in the event of damage to the property. The Insurance company processes the user’s claim by assessing the extent of the damage to the property and the amounts allocated to repairing the home.</p><h3>How can we measure Product Market Fit?</h3><p>Let’s define some high-level key metrics that are universal and as a whole can help us derive how well we are doing. The actual low-level metrics are going to vary from product to product</p><h4>User Adoption</h4><p>In our home insurance example, we want to measure our ability to acquire new users given our product offering, our pricing, our image, and our messaging. We look at such metrics as how many users went through a purchase flow (e.g spoke with an agent on the phone or use our self-service website) and actually converted into customers who bought an insurance policy.</p><h4>User Retention</h4><p>We can define these users as ones that have used the product or service and are coming back to consume more. In our Home Insurance use case, using the product means invoking the insurance by filing a claim for damages. We do not want to measure usage of the product as returning users but as users who renew their policy. A returning user for us is a user who has renewed his insurance policy.</p><h4>User Engagement</h4><p>Every product or service solves a specific problem. We want to measure that users are interacting with the core feature of our product or service. In our home insurance example, the core feature is invoking the insurance when damage has occurred and processing a claim. We want to measure how successful (Success should be measured by a subset of metrics based on time, ease of use, error in processing, etc) are users filing a claim, getting their claim processed, getting the repairs done, closing the claim.</p><p>We can also argue that in insurance, the fundamental problem we are solving for the users is holding a promissory contract to provide coverage. Many if not most Insurance Policies would not be activated during their lifetime. In such a case, engagement can be measured by holding/using the contract as proof during user external interaction (e.g providing proof to the bank)</p><h4>User Satisfaction</h4><p>We can rely on some users to provide feedback about our product or service. Users’ feedback can be delivered directly such as emails or phone calls to customer support to either complain or praise the product or service. We can also measure sentiment by reviewing logged communications, manager escalations, and online reviews. We would want to understand the customer sentiment across product workflows (Initial Purchase, Claim Filing, Updates, Renewals, Accessing evidence of insurance, etc)</p><h4>Revenue</h4><p>We want to understand if our users are willing to pay for our product. Our users pay a premium for their insurance. We want to measure how many users were presented with Insurance Quotes and converted into paying users. How many insurance policies went the full term without missing a payment? How many policies went the full term without being canceled by the users? How many policies were renewed after price changes? The reasons behind cancelations are important, especially the ones initiated by customers.</p><h3>How can we increase the chances of reaching product market fit?</h3><h4>Define the Initial Target Market</h4><p>The more focused we are on the initial target users, the easier it becomes to identify the core problem they have.</p><p>Instead of saying that we want to sell home insurance to every homeowner in the US, we can define the target market as a homeowner who earns X, is older than Y, and has a home that was built after A. We can expand this as far as we need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SGCBTDlo7FE8x7qaYfK6kw.png" /></figure><h4>Define the Problem</h4><p>We want to answer two questions.</p><p><strong>1. Are we solving the right problem?</strong></p><p>We can think of the problem space in two dimensions, Horizontal and Vertical.</p><p>For homeowners</p><p><strong>Horizontal</strong> could be something like “I want to have peace of mind that my home will be covered for damages“ or “I want to reduce the risk of damage occurring to my home“. This helps us identify the problems within the space of homeowners and the need for insurance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00HlHRo8dUVjcx02d90gpg.png" /></figure><p><strong>Vertical</strong> could be something like “I want to have my home covered for damages“, “I do not know what kind of coverage I should have“, “I do not know where to buy the coverage I need“, or “I cannot find affordable coverage“ The Vertical help us focus on the core problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*MTWPqUhtVIk-CZ33Dhzxkw.png" /></figure><p><strong>2. Is it a problem worth solving?</strong></p><p>Although there are multiple factors that would go into answering this, we can focus on some fundamentals.</p><p>Differentiator — Can we offer something better than existing products? Can the users do more with existing resources? Can our product save them X more time? Can we offer a lower-priced product? Can they do something that they are unable today?</p><p>Market Size — How many homeowners exist that would want to get cheaper prices at the cost of personal service?</p><p>Time and Cost to Market — How long and how much would it take to build a self-service user flows that can show users insurance quotes and buy them without agent assistance?</p><p>Timing — Can we access enough insurance carriers that give us quotes and buy policies via some API? Are consumers ready to give away the agent relationship? Can we offer lower prices and how? (Maybe we are in an inflationary environment which would make it hard to offer lower pricing)</p><p>Revenue — In Insurance, we do not charge customers to use the agency services. From a cost perspective, they would buy from us if they believe the price we quote them is a good deal (a good deal means many things. The insurance quote, user experience, customer support, added value at the time of purchase, etc)</p><h4>Iterations and Feedback loop</h4><p>There are 4 fundamental phases in the life cycle of a product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*qZHsO6E1La37gzfSCl90kg.png" /></figure><p><strong>User Discovery </strong>— before we decide to build a product, we want to interact with our users, validate assumptions and arrive at what problem are we solving. Interacting with the users can be done via direct interviews, landing pages, surveys, etc.</p><p><strong>Prototyping — </strong>A simulated product takes our discovery one step further as it allows users to see what a potential solution to their problem would look like. At this phase, we gather feedback about our solution. We would like to see users asking questions and challenging our solution and UX. Having customers say things like “Can the product do X? “What happens if Y occurs?“ is much better than “This looks cool“ or ”yeah, I’ll try it”.</p><p><strong>Basic Product </strong>— A working basic product that users can actually use. We can now gather actual metrics on how users are using the product. How often do they come back, how successful are they in performing actions, and are they able to solve their problem?</p><p><strong>Production — </strong>Fully functioning product that is serving a large number of users, is solving one or more problems, and is maintained and built upon. Reaching this phase does not mean product market fit as we see products out there that were built in full (E.g Google Plus) but never gained enough traction to become a viable businesses.</p><h3>Summary and Takeaways</h3><p>Measuring product-market fit means different things for different products and finding the right metrics to measure can shorten the time to understand your users, the problem, and what the solution looks like.</p><p>User discovery and prototyping is a phase where you want to invest much effort as it can save you from a future where you spend X years building something nobody wants just to shut it down.</p><p>To understand your users you need to be in their shoes. You need to live their lives. This will help you avoid statements like “We are building an investment platform for accredited investors where they can start investing with as low as $100“. Accredited investors have money, they do not care to invest $100 for a minuscule fraction of something. They do care about finding deals and investing for large returns</p><p>Finally, it’s ok to say “This is not going to work, we need to do something different“. You want to reach this conclusion as early as possible.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=625502c9577d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Product Market Fit Part 1: What is it and what it is not?]]></title>
            <link>https://medium.com/@royihaddad/product-market-fit-part-1-what-is-it-and-what-it-is-not-627cc3b08ab1?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/627cc3b08ab1</guid>
            <category><![CDATA[product-management]]></category>
            <category><![CDATA[startup-lessons]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[product]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Mon, 13 Mar 2023 14:09:42 GMT</pubDate>
            <atom:updated>2023-03-13T14:15:10.920Z</atom:updated>
            <content:encoded><![CDATA[<p>How many founders and product teams struggle with questions such as “<strong>Have we reached Product Market Fit?</strong>”, “<strong>Should we pivot?</strong>”, “<strong>Should we terminate the project?</strong>”. I have often asked these questions, both as a founder and as part of a large organization with multiple product teams.</p><p>The number one reason why startups and internal projects shut down is because they ran out of funding before they reached a product market fit.</p><p>We want to explore Product Market Fit and increase the probability of reaching it by using a few real-world examples.</p><p>In Part 1 we will explore the foundations of product market fit by looking at a Dog Day Care use case.</p><p>In part 2 we will explore the home insurance use case to understand how we can measure and increase the chances of reaching product market fit</p><h3><strong>What is Product Market Fit?</strong></h3><p>It’s a state where the product or service you are offering is solving one or more problems for a user in such a way that the user gets enough value from it and is willing to pay for it on an ongoing basis. We look to validating that</p><ol><li>A significant market of potential buyers with similar unmet needs exists</li><li>We have built a solution that offers a significant value over what they are doing today to address their problem such that they are willing to try it, buy it, and use it going forward</li></ol><p>Let’s review the following example</p><h4>Dog Sitting</h4><p><strong>The Users</strong>: Owners of dogs and cats who earn more than $100,000, want their pets handled by professionals, do not want to leave their pets alone, and are looking for social interactions for their pets. (We can expand this further)</p><p>are willing to spend money for someone to take care of their pets while they are traveling away from home.</p><p><strong>The problem</strong>: “How do I sustain a quality of living for my pet while I am away” or “How can I ensure my pets’ well-being while I am away”</p><p><strong>The Motivation:</strong> Sense of security, trust, and knowing that the pet is being taken care of.</p><p><strong>The Product</strong>: A place where pets can have a warm bed, 3 meals a day, social activities, and care for by professionals while their owners are traveling.</p><p>There are several other solutions that these users can try but none would solve the fundamental problems optimally. The pet owners can leave the pet with a friend, the pet owners can hire someone to visit the home, feed the pet, take it out for walks, and can have automated feeders.</p><p>Let’s dive deeper to break down the Product component and the Market component.</p><h4><strong>The Product</strong></h4><p>There are three building blocks in the foundations of every product or service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*OKv_XgCQ3AK82_yH625ssg.png" /></figure><p>Usability — How easy is it to reserve a spot for your pet? What does the pet onboarding process look like? How easy is it for the owner to check the well-being of the pet at any point in time? How simple is it to get the pet back? How responsive are the staff? How successful are the users in completing a pet boarding period?</p><p>Value — It has to provide value to the users, usually in the form of solving the unmet need. Some examples of value are, saving users time, saving users money, improving users’ health, and making users feel positive emotions.</p><p>Cost — The cost you expect your users to pay for the product or service but also justify the existence of the product or service as a unit of business.</p><h4>The Market</h4><p>Every market is based on three basic building blocks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*pyZHvY3TIjciV-dKbx6IhA.png" /></figure><p>Users — You need someone to consume your product or service. In the early stages, we refer to this group as early adopters. In later stages, we refer to them as early/late majority and laggards.</p><p>Unmet Need — The users in your market should have some unmet need, either in the form of an unsolved problem or a problem that is solved sub-optimally by an existing solution.</p><p>Motivation — Having an unmet need is often not enough, users need to be motivated by some trigger to use the product or service. Let’s continue with the Doggy daycare example. The unmet need is caring for the pet given specific requirements (Social interactions, food, shelter, etc) and the motivation could be “ I am going away on vacation ” or “ I have a work trip ”</p><h4>The Product Market Fit</h4><p>Bring the two together and you get the product market fit. We end up where we have a group of users who have an unmet need, who are motivated to adopt a product or service, and who are willing to pay for a value proposition from a product that is usable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*wCVA3Zevr1OvY47e03I2MA.png" /></figure><h3><strong>What product market fit is not?</strong></h3><p>Product Trial — Just because someone is willing to try our product does not mean we have a product market fit. Users can try the product and never come back. Going back to the dog-sitting example, you may try the Doggy Day Care but something in the experience would not meet your expectations and you will not use it again (Your pet has suffered wounds, looks malnourished, depressed, you were not able to check in on the pet, cost …)</p><p>Paying Customer — Having a customer pay for the product does not mean that it is a fit for them. It all depends on how the product is presented to users. You may be asking users to pay for the product or trial and they will but will not stick around. Imagine our Doggy daycare having a constant flow of paying customers that never come back.</p><p>Wrong Metrics — Having users log in but failing to perform key actions is not a sign of product market fit. In our Doggy Day Care example interactions may be infrequent and on top of measuring the frequency of pet owners using the service, we can measure other things like the number of pets getting injured or sick, the number of pets losing weight, how often the pet owner interacts with systems that allow him to monitor the pets well being (security cameras, call-ins), we can also measure the pets social interactions.</p><p>Feature Factory — We often see teams adding more features to a product because it may supplement an additional feature or they think the customer will definitely want “this”, adding more features without going through the same user validation process just makes the product more complicated and may have the opposite effect. You will see users leaving the product because it is too difficult to even use, hard to navigate, or not intuitive.</p><h3>Summary</h3><p>Product Market Fit is a journey that is very dynamic when we first embark on it. It is dynamic because we are trying to understand what the users really need and what product we should build. Once we reach Product Market Fit, we will be in some static state until our user’s needs have changed and we need to embark on this Journey again.</p><p>In <a href="https://medium.com/@royihaddad/product-market-fit-part-2-how-to-reach-it-625502c9577d"><strong>Part 2</strong></a> we will talk about measuring Product Market Fit and increasing our chances of reaching it</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=627cc3b08ab1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ML Architecture in Distributed Systems]]></title>
            <link>https://medium.com/@royihaddad/ml-architecture-in-distributed-systems-7956cd6788b0?source=rss-aca29e882926------2</link>
            <guid isPermaLink="false">https://medium.com/p/7956cd6788b0</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Royi haddad]]></dc:creator>
            <pubDate>Thu, 19 Jan 2023 23:32:16 GMT</pubDate>
            <atom:updated>2023-01-20T14:55:07.239Z</atom:updated>
            <content:encoded><![CDATA[<h3>Who should read this Article?</h3><p>Engineers and leaders who are looking to introduce ML into the organization. The article explores the high-level understanding of systems and processes. It covers a use case from the Insurance world, the lifecycle of MLOps in an organization, and the architecture of ML in a distributed system production environment. The article also covers Three critical areas (Data, Monitoring, and Collaboration) which can impact model development, releases, and accuracy of impact on the User experiences. The article does not cover the process of training models.</p><h3>What problem are we looking to solve?</h3><p>Companies are investing in ML in the hope of solving business problems more efficiently and accurately. Building ML Infrastructure is not a trivial task and requires understanding the data which exists in the business ecosystems, the interfaces between systems, the boundaries of responsibilities between teams, and the process of collaboration between the business, data science team, data engineers, and production system teams. We want to increase our chances of successfully integrating ML into the business.</p><h3>Home Insurance Business Use Case</h3><p>Customers can purchase insurance using an online flow. During the flow pieces of data would be collected about the property, insured, and the relevant underwriting data. We want to use the data and invoke a model to predict what is the expected loss ratio. If the loss ratio is high, we would want to block the customer and not sell him a home insurance policy. The data may be provided by the customer or fetched from 3P sources. At multiple phases in the flow, the ML Underwriting model will be called to predict the loss ratio. When the loss ratio is low, the customer may proceed with the flow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mjKrgHYRTilxAcyk8e6T1g.png" /></figure><h3>What is the Lifecycle of ML in a company?</h3><p>It all starts with a business problem we want to solve which cannot be solved efficiently using conventional methods. The data science team will gather data and select a model going through an iterative approach to do feature selection and evaluate if the problem can be solved while collaborating with production teams and data warehouse teams. Model training and fine-tuning would follow. Once a baseline that is acceptable to the business has been achieved, the model will get deployed and monitored in production for evaluation and further training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vkFrk4mDOLvuvo2Wk926Og.png" /></figure><h3>What are the components of ML Architecture?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rQh9pHjNOlTQCroMcRhZcw.png" /></figure><h4>Client</h4><p>This layer refers to any system that is requesting inferences from the ML layer. Such systems could be internal or external, in the insurance world a client may make a post request passing the id of an Insurance Application.</p><p>** UI-based client system would implement an additional layer known as BFF (Backend for Frontend) which is responsible to model request/response</p><pre>POST /api/v1/underwriting/lossRatio  HTTP/1.1<br>Content-Type: application/json; charset=utf-8<br><br>{<br>  &quot;insuranceApplicationId&quot;: &quot;796acf28-e197-476d-9ec1-f2ecd3dcdd91&quot;<br>}<br><br>HTTP/1.1 201 Created<br>Content-Type: application/json; charset=utf-8<br><br>{<br>  &quot;id&quot;: &quot;0e3d75ca-e60e-41fe-b413-930f795885f0&quot;,<br>  &quot;lossRatioRiskClass&quot;: &quot;HIGH&quot;<br>}</pre><h4>ML Orchestrator</h4><p>In a system where there are multiple models to be called and we may have dependency between them (E.g Model A requires input from Model B inference) we would use an orchestrator. It’s a component that would sit between the Services and Clients. Orchestrators can and should assume the role of data aggregators for inputs. ** Orchestrator does not necessarily means a new HTTP-based service. In some cases, it can be a module or a standalone library. It depends on the existing architecture and engineering needs</p><p>Example: The client requests an inference for an Insurance Score. The inputs for the Insurance Score Model would come from multiple core services and different models. The Orchestrator would be responsible to call the different core services, gathering the inputs required for the First model, invoking it, and constructing a new input payload for the main model.</p><h4>Core Services</h4><p>The inputs to our Underwriting model exists in different domains and core service which has their own interface (E.g REST).</p><p><strong>Customer Service</strong> holds data about the customer <strong>{ firstName, lastName, dob, phone, email … }</strong></p><p><strong>Property Service</strong> holds data about the property <strong>{ address, yearBuilt, roofYearBuilt … }</strong></p><h4>Underwriting Model</h4><p>This layer is the actual layer where the model lives. it is responsible to provide an HTTP interface to the model, maintain the model artifact or load it from external sources. It is also responsible to integrate with feature stores and ml monitoring systems. We aim to maintain a very specific response from such services to the caller, usually in the form of an enumerated class or level of probability.</p><pre>POST /api/v1/lossRatio/inference HTTP/1.1<br>Content-Type: application/json; charset=utf-8<br><br>{<br>  &quot;insuranceApplicationId&quot;: &quot;796acf28-e197-476d-9ec1-f2ecd3dcdd91&quot;,<br>  &quot;insuredAge&quot;: &quot;25&quot;,<br>  &quot;propertyYearBuilt&quot;: &quot;1955&quot;,<br>  ...<br>}<br><br>HTTP/1.1 201 Created<br>Content-Type: application/json; charset=utf-8<br><br>{<br>  &quot;id&quot;: &quot;0e3d75ca-e60e-41fe-b413-930f795885f0&quot;,<br>  &quot;lossRatioRiskClass&quot;: &quot;HIGH&quot;,<br>}</pre><h4>Monitoring</h4><p>ML Models have a tendency to degrade over time, it has to do with the data that is ever-changing. We want to understand our model performance on a constant basis. For that, we need to log each call to our model. The logged data should contain the inputs passed, inference response, and other data that may be useful, latency, external ids, etc.</p><h4>Feature Store</h4><p>Not all model inputs would come directly from the production system data stores. We may have data sets that are used solely by the ML layer.</p><p>Example: We have a model that requires data from the US Census, this data is not required by any of the production systems and user flows.</p><p>In order to make this data available during training and inference, we would keep it in a component called a feature store which is essentially a database with an API.</p><h3>What is the Data problem?</h3><p>During training, the Data Science team will work with data from various sources. External like US Census, and Internal like a Data warehouse. The data science team is trying to solve a business problem and we cannot limit them to only using data X. We, therefore, run the risk that the data they use either does not exist during the inference stage in production or that there are data transformations that occur (E.g Data warehouse collects data from the production system and alter it which is now used by the data science team) in some system before the data science team consume the data for training.</p><h4>Transformed Data</h4><p>Data that was generated by the production system in a certain format but was altered in the Data warehouse and used by the data science team. The transformation was meant for a different consumer of the data warehouse. Production system generated <strong>{ homeYearBuilt: String }</strong>. Data warehouse transformed <strong>{ ageOfhome: String }</strong>. The ml interface layer may expect the <strong>ageOfHome</strong> which is not available in its raw form and has to be derived.</p><h4>Non-production Data</h4><p>The data science team trained a model using data that comes from external sources like US Census data. this data does not exist in the production system and must be exposed to the model during training and inference.</p><h4>Legacy Data</h4><p>Companies often have data that is no longer in use but is still available for consumers (Business analytics and of course data science).</p><p>The model was trained on <strong>{ zillowHomePrice: String}</strong>. The production system does not collect this data any longer.</p><p>The data problem can be addressed (Addressed does not necessarily means 100% solved) by implementing systems like feature stores, dedicated workspaces for data science within data warehouses, documentation and collaboration with production teams, and moving data transformations into the ML Service layer</p><h3>How should we Monitor our ML Models?</h3><p>Monitoring ML has varying complexities which companies should assume based on their current goals of using ML, their resources, and use cases. Monitoring is an evolving need and it&#39;s reasonable for companies to start with simple solutions first and increase complexity as the ML needs of the business change.</p><h4>What data we should log?</h4><p>We want to understand the data payload that went into the service on each invocation. This enables us to understand the evolution of requests over time and ensure that the required inputs are passed.</p><pre>{<br>  propertyZipCode: &#39;12345&#39;,<br>  insuredCreditScore: &#39;700&#39;,<br>  numberOfPreviousClaims: &#39;0&#39;,<br>  futureClaimRisk: &#39;HIGH&#39;,<br>  insuranceApplicationId: &#39;796acf28-e197-476d-9ec1-f2ecd3dcdd91&#39;<br>  ...<br>}</pre><p>We also want to understand the model predictions and how they differ from an expected baseline over time</p><pre>{<br>  prediction: &#39;0.17&#39;,<br>  lossRatioRiskClass: &#39;LOW&#39;,<br>}</pre><h4>Example log</h4><pre>{<br>  inputs: {<br>    propertyZipCode: &#39;12345&#39;,<br>    insuredCreditScore: &#39;700&#39;,<br>    numberOfPreviousClaims: &#39;0&#39;,<br>    futureClaimRisk: &#39;HIGH&#39;,<br>    insuranceApplicationId: &#39;796acf28-e197-476d-9ec1-f2ecd3dcdd91&#39;<br>    ...<br>  },<br>  output: {<br>    prediction: &#39;0.17&#39;,<br>    lossRatioRiskClass: &#39;LOW&#39;,<br>  }<br>}</pre><h4>Where should we log the data?</h4><p>It will depend on where the company is in its ML adoption life cycle.</p><p>We can use out-of-the-box solutions like AWS and Google Cloud or we can build our own. If you are just starting out, logging the data as raw logs to a system like Loggly or Coralogix may be the simplest option. More advanced systems can log the data to S3 files.</p><p>The logged data needs to end up in a system that can read it and provide the insight the data science is looking for in terms of changes to the inputs and outputs. Out-of-the-box systems have this covered end to end. Building your own would require putting the data in a Data warehouse or some other system like Tableau.</p><h3>Process and Collaboration.</h3><p>Multiple teams are involved in implementing and using ML in organizations. Having the right Infrastructure in place is only <strong>15%</strong> of the effort. <strong>85%</strong> is on the process and collaboration. it is important to define the different roles and their responsibilities. E. g the MLOps engineer role would be responsible to spin up new services for the different models, they would also work with data science to validate data across systems. They would help maintain the feature store and more. The responsibilities of different roles would differ from one organization to another but there are some basic functions that all should follow.</p><h4>Important Roles and Responsibilities</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OzpYrlC96z8nVEIitD2dRQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7956cd6788b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>