<?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 Longbin Chen on Medium]]></title>
        <description><![CDATA[Stories by Longbin Chen on Medium]]></description>
        <link>https://medium.com/@longbinchen?source=rss-a99e2d4abd03------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RYpl--nchkGCn1VH.jpeg</url>
            <title>Stories by Longbin Chen on Medium</title>
            <link>https://medium.com/@longbinchen?source=rss-a99e2d4abd03------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:58:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@longbinchen/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[Be careful with sklearn and pandas in production environment]]></title>
            <link>https://medium.com/@longbinchen/be-careful-with-sklearn-and-pandas-in-production-environment-35f6ef585b61?source=rss-a99e2d4abd03------2</link>
            <guid isPermaLink="false">https://medium.com/p/35f6ef585b61</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[sklearn]]></category>
            <dc:creator><![CDATA[Longbin Chen]]></dc:creator>
            <pubDate>Mon, 04 May 2020 16:07:25 GMT</pubDate>
            <atom:updated>2020-05-04T16:07:25.264Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I spent some time to solve a latency issue in one of our ML service, which is built with python and a bunch of python machine learning libraries, including numpy, scipy, sklearn and pandas. It turns out that sklearn and pandas are the ones to blame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fpYy4x4wQ2QC4op_TcbU-g.png" /></figure><p>sk-learn is implemented in pure python language(correct me if I am wrong), which inevitably incurs some latency issue due to the ineffectiveness of python language. In our case, we found one function of sk-learn, <em>sklearn.preprocessing.MultiLabelBinarizer</em>, cause around 80ms delays to convert words to vectors. It is a bit surprising as this should be just a simply dictionary look-up. Fortunately, on sk-learn website they provide the source code so that I can see how they implement it.</p><p>Apparently they don’t cache the dictionary and, when called, this function would create the dictionary on the fly and that’s how it cost about 80ms unnecessary latency to our service.</p><p>BTW, 80ms seems to be small and ignorable when you are thinking about training deep learning algorithms on GPU machines in days, however it is a significant chunk of latency that we can’t afford in online service where the total budget for latency is around 200m ~ 500ms.</p><p>At the end, I had to do an ugly ‘patch’ to work around this without making a major change to the codes. I really hope sk-learn can provide a function to build the dictionary and keep the dictionary as a part of the object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SbA9irS3OFJr1TQ1LluK6Q.png" /></figure><p>Similarly, I found Pandas’s DataFrame would incur significant overhead to be created. As a syntax sugar, it is not really worthy to use it in time-sensitive scenarios like online service.</p><p>Numpy, on the other hand, is a masterpiece IMO. Numpy’s matrix operations leverage <a href="https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions">SSE</a> and is lightening fast in matrix operations. It can multiply a vector of 100x1 with a huge matrix of 100x1,000,000 in 100ms. That is to find the nearest neighbor in 1 million samples in 100 dimension space in 100ms. It is so fast that it makes the ANN(approximate nearest neighbor) algorithms unnecessary in medium size applications which have around a couple of million samples.</p><p>I believe sk-learn will evolve over the time and I won’t need to worry about this in the future. As for now, just be cautious.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35f6ef585b61" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>