6 ms·
Building a recommendation engine inside Postgres with Python and Pandas (2020)
- wallace01 5y agoEnjoyed that read but to be honest I always had mixed feelings about PSQL Extensions. Logic in a database is wrong
- CameronNemo 5y agoCan you expand on why you think it is wrong or problematic?
- systemvoltage 5y agoDatabase is the last thing that scales usually so if you put a bunch of computational load on it besides queries, you've set yourself up for scaling/sharding sooner than later.
- lowwave 5y agoNot always! If computation involves math over set of records, then Postgres is great for that. Have the operation inside the db reduce connection pooling on the application level.
- jamesfinlayson 5y agoI remember a friend telling me in a previous job me they loaded the database with stored procedures because it ran on the most powerful server when the product launched. And then the database server hit its capacity very quickly.
- rbanffy 5y agoReminds me of why I learned to program in PostScript - the PostScript printer was, by far, the fastest computer I had access to - also it had a ton of memory and its very own SCSI hard disk. Plotting a Mandelbrot on the Mac would take a lot longer, even in C, than just making the printer do it. Usually overnight, because the program took a couple hours to run most of the time.
- rbanffy 5y ago> you've set yourself up for scaling/sharding sooner than later. OTOH, if you can pre-process the data (or shards) in the server (or servers), you won't need to move it across the network for processing. Hard problems are hard and one of the reasons databases outlive their systems and developers is because moving that data is a Royal Pain.
- FridgeSeal 5y agoData and its structure outlives the developer, and often the application. Tightly coupling a lot of the application-specific compute in with how the day is stored and accessed says you up for even more difficulties when you need to debug, scale, migrate storage/compute or evolve your application faster or more radically than your data organisation.
- snissn 5y agoIt’s so amazing to have the ability to enhance your database with custom methods. Keeps your data model really well organized across your infrastructure
- systemvoltage 5y agoThat's the job of an API IMO.
- uoaei 5y agoAn API makes sense for this case when you won't be throwing away a lot of the raw data in favor of the processed/transformed data. But doing a lot of these operations on-server makes sense when there's a significant volume of highly parallelizable transformations which need to be done on the data before it's usable. Of course the best solution is likely to be a happy medium between the two, where simple low-level transformations are done on-server and the rest of the data preparation is done as the data is transferred to the client.
- mrbungie 5y agoNot in data processing. There are cases where you have so much data that is better to bring computation to data rather than the other way around.
- FridgeSeal 5y agoHaving dealt with the fallout of a database that’s had hundreds of stored procedures and custom code stapled into it: never again. It was so hard to debug, so hard to safely maintain these procs, and a general nightmare
- arthurcolle 5y agoDo what thou wilt shall be the whole of the Law
- deleted 5y ago[deleted]
- earthscienceman 5y agoIt's interesting to me to see pandas used in this application. I'd be curious to see a more fully featured implementation. I'm a scientist by profession and I've been working on building out several different generalized data processing pipelines for some specific problems in my sub-field, to make gathering and formatting in-situ data easier and more standardized/open/version-controlled. It's going great, worlds better than the smattering of matlab code strewn across the hard drives in the lab written in a non-collaborative manner and shared by email... ... but. I'll admit, I've run into a lot of footguns in the pandas API in terms of efficiency. You'll do something it what seems to be the logical way, or in a way that the API funnels you towards (like the groupby calls in the OP), and you'll quickly realize that if you're working on large-ish tables (>10Gb in memory) that it was the stupid way to do things. In terms of readable code to share with colleagues, pandas can't be beat, but things get wonky when you reach significant complexity and I would be surprised if it made any sense to use in a 'real' recommendation engine when considering developer productivity.
- murphy214 5y agoMost of my workin pandas is with small to medium size < 1g tables what constraints do you run into with the larger data sets you are talking about?
- ketozhang 5y agoLook into Dask if you are attempting to process an entire data table that's larger than memory. For your last paragraph, you're conflating the need to share code with the need to build a robust scalable service. Most research code are only needed for the paper and rarely touched again.
- mooneater 5y agoSeems like a nice approach, I used to try things like this with postgres (embedding sklearn). Though it can be slow, harder to deal with memory usage issues, harder to debug in general, harder to extend/generalise. And for me postgres is now one of multiple datastores so doing this is not as helpful.
- VWWHFSfQ 5y agoI guess I'm wondering what the point of pandas is here. And maybe in general? We hired a data engineer awhile ago and he kept complaining that the "python kernel" was crashing and he needed bigger instances to run his pandas scripts. I took a look and realized he was trying to pull the entire database of a nearly a billion rows into python to do some trivial joins, groupings, and filtering. I'm thinking, what are you doing man. I think this is pretty common in the pandas world right
- stevofolife 5y agoPretty common and I think one of the main reasons why is that Pandas API has a very low entry barrier to get productive. It is very easy to do something without really knowing anything about computer science.
- ra 5y agoIt's very true that pandas is in memory and so not good with large datasets... it is good for playing around with data and plotting / visualising. Scikitlearn, statsmodels are very powerful for this. SQL is sometimes under appreciated as superior for grouping sorting and filtering... although with Bigquery and KSQL, SQL is definitely finding it's groove again.
- Grimm1 5y agoYikes, please tell me they were hired as a junior? Obviously in your case some trivial SQL would have sufficed, which I feel like should have been caught in an interview but could be excused for a junior eng. Also Pandas is not an amazing choice for data engineering tasks in general IMO, type conversion wonkiness is one reason I have questions about people who decide to use Pandas for legitimate data engineering tasks (as opposed to exploratory work or analysis).
- FridgeSeal 5y agoThe issue is a lot of the tools in the ecosystem are positioning themselves as “do everything” and the supporting marketing/services is all about increasing numbers of workarounds to enable this. Using pandas but your data doesn’t fit in memory anymore? Here just use this fragile series of packages to distribute it across processors. Don’t reevaluate whether Pandas/Python is still the best choice, just layer more things in. Run into issues with that approach? Here lift and shift your whole thing into Spark! Don’t ever optimise or actually engineer your process, just layer more things in!!!
- peterthehacker 5y agoIt recommends products that were most frequently included in the same order. So it’s not recommending similar or related products, but products frequently purchased with the product in question, so the wine use-case wouldn’t recommend wine, but it would recommend cheese, bottle openers, chocolate, etc. This makes sense in the context of the example the author copied from [0], where the dataset is sports equipment. Often purchased in bundles of related products for a specific sport. [0]https://github.com/scottfitzcodes/rec-engine-example https://github.com/scottfitzcodes/rec-engine-example
- rob_c 5y agoNice, now expand that to be a simple rolling trending algorithm and you've reinvented most of the now common place web tech
- Yajirobe 5y agoIsn't it better to write a postgres extension instead of running python inside?
- lysecret 5y agoI see some python / pandas hate here and I have to defend it a bit: We all agree a query like: tb_1 = pd.read_sql(big_table_1) tb_2 = pd.read_sql(big_table_2) table_i_want = pd.merge(tb_1, tb_2) table_i_want =table_i_want[table_i_want.my_id.isin([1,2,3])] is completely stupid, put that in the DB! And a lot of (very very) unexperienced people will write such a query, since they dont know anyhting about DBs and SQl. However, pandas and python is here for what comes after that! Like a very complicated groupby logic, or invoking some apis to enhance data or complicated string processing or some kind of error measure or invoking some ML models or sending a slack message or building features for ranking or whatever. I know a lot of people here love their SQL but queries like mentioned above are either impossible or become horrible horibly complicated and impossible to debug. EDIT: And I want to add one extremely important thing. Whenever you can put stress away from your DB it is a good thing. Since your DB has to run all of the time and will be already stressed from Frontend requests. Combined with the fact that we are often talking about non-time sensitive work wouldn't it be better if your data scientists work on a database dump instead of continously hitting the DB?
- beckingz 5y agoYou setup a read-replica so that the production database isn't hit by analytical queries. And then ingest that read-replica to a column store data warehouse so that you don't have to think about reliability until someone starts yelling about speed or cost.