胃食管反流咳嗽吃什么药| 孩子恶心想吐是什么原因| 冰点脱毛的原理是什么| 谷丙转氨酶高吃什么药| 见不得别人好是什么心理| 5.11什么星座| 为难是什么意思| 高糖是什么| 蜜蜂的尾巴有什么作用| 嗅觉失灵是什么原因| 1997年是什么生肖| 什么是尊严| 长期吃二甲双胍有什么副作用| 肾五行属什么| 次日什么意思| 儿童细菌感染吃什么药| 梦见前男友死了是什么意思| 蛇的天敌是什么| 脚脱皮什么原因| 喉结是什么| 卵巢囊肿吃什么药| 呵呵哒是什么意思| 不现实什么意思| 狗屎运是什么意思| 大口什么字| 西红柿拌白糖又叫什么| 香芋是什么| 为什么感冒会咳嗽| hsv是什么病毒| 什么叫透析| hpc是什么| 乌龟浮水是什么原因| 舌苔厚有齿痕吃什么药| 熟石灰是什么| 嫣字五行属什么| 为什么孩子要跟爸爸姓| bpm是什么| fmc是什么意思| 经期吃凉的东西有什么影响| 脂肪瘤是什么| 下火喝什么茶| 告诉我们什么道理| 血氧饱和度是什么意思| 结婚5年是什么婚| 质变是什么意思| 挚友什么意思| 99新是什么意思| 山药补什么| 乳糖不耐受喝什么牛奶| 送孕妇什么礼物最贴心| 妄想症吃什么药| 高反吃什么药| 什么是回迁房| 明矾有什么作用| 蓝色五行属什么| 饮食清淡的标准是什么| 头眩晕吃什么药| 颐养天年是什么意思| 止盈什么意思| 山东吃什么主食| 什么是黄褐斑| 百合什么意思| 青少年流鼻血是什么原因引起的| 眩晕症吃什么药| 消化不良吃什么药最好| 吃什么可以提高免疫力和抵抗力| 海兔是什么| 黑色的鸟是什么鸟| syphilis是什么意思| 香蕉不能和什么一起吃| 为什么月经每个月提前| 红楼梦是一部什么小说| 马赛克是什么| 什么叫阴虚| 免疫球蛋白e高说明什么| 眼睛充血是什么原因引起的| 手为什么会长水泡| 风寒吃什么感冒药| 什么话| 栀子花什么季节开花| pettm和pe有什么区别| 什么尾花| 阅人无数什么意思| 什么叫窝沟封闭| 回民为什么不能吃猪肉| 睡眠不足会引起什么症状| 丑时是什么命| 孕妇梦见西瓜是什么意思| 什么的什么是什么的伞| 颧骨疼是什么原因| 舌头凉凉的是什么原因| 痈肿疮疖是什么意思| 杨梅不能与什么同吃| 水痘疫苗什么时候接种| 血象高是什么意思| 感冒有黄痰是什么原因| 什么是超度| 仕途是什么意思| 为什么越睡越困| 猪肉不能和什么一起吃| 腐女是什么意思| kobe是什么意思| 12颗珠子的手串什么意思| 梦见自己鞋子破了是什么意思| 脚趾缝脱皮是什么原因| 接触隔离什么意思| 神经根型颈椎病吃什么药| 带黄金对身体有什么好处| 肾火旺有什么症状| 525什么星座| 蛋白粉什么时候吃效果最好| 干净的反义词是什么| 左腿疼是什么原因| 社畜什么意思| 眩晕是什么原因引起的| 826是什么星座| 阴道炎用什么洗| abo是什么| 无水酥油是什么| 女人跑马是什么意思| 196是什么意思| 什么是穿刺| 碳酸钙是什么东西| 绿色裤子配什么上衣| 平五行属什么| 马瘦毛长是什么意思| 高筋面粉适合做什么| 脱发严重应该去医院挂什么科| 荷叶泡水喝有什么功效| 在屋顶唱着你的歌是什么歌| 玫瑰和月季有什么区别| 什么叫带状疱疹| 当局是什么意思| 湿气太重吃什么排湿最快| 五月一日是什么星座| 美元长什么样子图片| 奶油霜是什么| 膀胱炎吃什么药最见效| r商标是什么意思| 肩胛骨麻麻的什么原因| soeasy是什么意思| 领英是什么| 阳虚吃什么好| 股票杠杆是什么意思| 舌苔发黑是什么病的前兆| 拉肚子应该吃什么药| 胃息肉吃什么药| 热痱子用什么药| 小孩阑尾炎是由什么原因引起的| 苗侨伟为什么叫三哥| 念想是什么意思| 胸口闷闷的有点疼是什么原因| 龙须菜是什么| 喝什么解辣| 肾虚腰疼吃什么药最有效| 完璧归赵发生在什么时期| 梦到头发长长了是什么意思| 脸上白了一小块是什么原因| 巨门是什么意思| 计生用品是什么| 海水倒灌是什么意思| 动脉硬化吃什么药最好| 为什么总打喷嚏| 女人吃葛根粉有什么好处| 家长里短是什么意思| 人黑穿什么颜色的衣服好看| 宫颈糜烂什么症状| 八面玲珑是什么意思| 股骨头疼痛什么原因| 吃氨糖有什么好处和坏处| 杨新鸣包贝尔什么关系| 农历10月是什么星座| 精子对女性有什么好处| 2040年是什么年| as是什么元素| 勇者胜的上半句是什么| 卧轨什么意思| 吃维生素b1有什么好处和副作用| 彩金和黄金有什么区别| 赵本山是什么学历| 胆囊炎吃什么中成药| 心动过速吃什么药| 三个又读什么| 成人礼是什么意思| 抗体高是什么意思| 暑湿感冒吃什么药| 1950属什么生肖| 贩子是什么意思| ct 是什么| 脸上发痒是什么原因| 小腿肌肉痛是什么原因| 红参和高丽参有什么区别| lh是什么意思啊| 口腔溃疡反复发作是什么原因| 药鱼用什么药效果最好| 私生饭是什么意思| 梦见香蕉是什么意思| 慢性浅表性胃炎吃什么药| 肺结核吃什么食物好| 吃醋对身体有什么好处| 看山不是山看水不是水是什么意思| 推头是什么意思| 堆堆袜是什么| 脑供血不足做什么检查能查出来| 北戴河是什么海| 海参和辽参有什么区别| 88年属什么的| 什么去甲醛最快有效| 梦见自己生男孩是什么意思| 风致是什么意思| 葱白是什么| sheen是什么牌子的手表| 腋窝爱出汗是什么原因| 深圳少年宫有什么好玩的| 肠炎吃什么药好| 鸡冠花什么时候开花| decaf是什么意思| 草代表什么生肖| 老年斑长什么样| 金黄的稻田像什么| 孕妇便秘吃什么药| merry是什么意思| upup是什么意思| 张学良为什么不回大陆| 儒家思想是什么意思| 吃什么治失眠| 脂血是什么意思| 什么是介入手术| 西瓜配什么榨汁好喝| 苯丙酮尿症是什么| 长闭口是什么原因造成的| 无菌性前列腺炎吃什么药效果好| 大姨妈来了不能吃什么水果| 应激反应是什么意思| h的车标是什么牌子| 香蕉是什么季节的| 突然好想你你会在哪里是什么歌| 美帝是什么意思| 什么叫批次线| 卵巢结节是什么意思| 防字代表什么生肖| 12年一个轮回叫什么| 经常梦遗是什么原因| amk是什么品牌| 化疗和靶向有什么区别| 英国的全称是什么| 波霸是什么| 神志不清是什么意思| 一吃就吐是什么病症| 泡泡棉是什么面料| 品种是什么意思| 乳腺彩超能查出什么| 西方属于五行属什么| 为什么会长痘痘| 怄气是什么意思| 主动权是什么意思| 终其一生下一句是什么| 图谋不轨什么意思| 二月十号是什么星座| 什么烟最好抽| 什么样的伤口需要缝针| 罗勒叶在中国叫什么| 阀值是什么意思| 陈皮泡水喝有什么功效| 百度
 

【丽水日报】莲都科技特派员田间地头“传经送宝”

百度 “我们很多优秀机器人,都是拿过世界冠军的。

Set up your workstation, reduce workplace clutter, maintain a clean namespace, and effortlessly keep your dataset up-to-date.



By Shaw Lu, Data Scientist at Coupang


I knew SQL long before learning about Pandas, and I was intrigued by the way Pandas faithfully emulates SQL. Stereotypically, SQL is for analysts, who crunch data into informative reports, whereas Python is for data scientists, who use data to build (and overfit) models. Although they are almost functionally equivalent, I’d argue both tools are essential for a data scientist to work efficiently. From my experience with Pandas, I’ve noticed the following:

  • I end up with many CSV files when exploring different features.
  • When I aggregate over a big dataframe, the Jupyter kernel simply dies.
  • I have multiple dataframes with confusing (and long) names in my kernel.
  • My feature engineering codes look ugly and are scattered over many cells.

Those problems are naturally solved when I began feature engineering directly in SQL. So in this post, I’ll share some of my favorite tricks by working through a take-home challenge dataset. If you know a little bit of SQL, it’s time to put it into good use.

 

Installing MySQL

To start with, you need a SQL server. I’m using MySQL in this post. You can get MySQL server by installing one of the local desktop servers such as MAMP, WAMP or XAMPP. There are many tutorials online, and it’s worth going through the trouble.

After setting up your server, make sure you have three items ready: username, password, port number. Login through Terminal by entering the following command (here we have username “root”, password 1234567).

mysql -uroot -p1234567

 

Then create a database called “Shutterfly” in the MySQL console (you can name it whatever you want). The two tables will be loaded into this database.

create database Shutterfly;

 

 

Install sqlalchemy

You’ll need Pandas and sqlalchemy to work with SQL in Python. I bet you already have Pandas. Then install sqlalchemy by activating your desired environment to launch Jupyter notebook, and enter:

pip install sqlalchemy

 

The sqlalchemy module also requires?MySQLdb?and?mysqlclient?modules. Depending on your OS, this can be installed using different?commands.

 

Load Dataset into MySQL Server

In this example, we’ll load data from two?CSV files, and engineer features directly in MySQL. To load datasets, we need to instantiate an?engine?object using username, password, port number, and database name. Two tables will be created:?Online?and?Order. A natural index will be created on each table.

In MySQL console, you can verify that the tables have been created.

 

Split Dataset

This may seem counter-intuitive since we haven’t built any feature yet. But it’s actually very neat because all we need to do is to split?dataset by index. By design, I also included the label (event 2) which we try to predict. When loading features, we will simply join the index with feature tables.

In MySQL console, you can verify that the training and test set are created.

 

Feature Engineering

This is the heavy lifting part. I write SQL code directly in Sublime Text, and debug my code by pasting them into MySQL console. Because this dataset is an event log, we must avoid leaking future information into each data point. As you can imagine, every feature needs to be aggregated over the history!

Joining table is the slowest operation, and so we want to get as many features as possible from each join. In this dataset, I implemented four types of join, resulting in four groups of features. The details are not important, but you can find all my SQL snippets?here. Each snippet creates a table.?The index is preserved and must match correctly to the response variable in the training set and test set.?Each snippet is structured like this:

To generate the feature tables, open a new Terminal, navigate to the folder containing the sql files, and enter the following commands and passwords. The first snippet creates some necessary indices that speed up the join operation. The next four snippets create four feature tables. Without the indices, the joining takes forever. With the indices, it takes about 20 minutes (not bad on a local machine).

mysql < add_index.sql -uroot -p1234567
mysql < feature_group_1.sql -uroot -p1234567
mysql < feature_group_2.sql -uroot -p1234567
mysql < feature_group_3.sql -uroot -p1234567
mysql < feature_group_4.sql -uroot -p1234567

 

Now you should have the following tables in the database. Note that the derived features are stored separately from the original event logs, which help prevent confusion and disaster.

 

Load Features

Here I wrote a utility function that pulls data from the MySQL server.

  • The function takes table name “trn_set” (training set) or “tst_set” (test set) as input, and an optional?limit?clause, if you only want a subset of the data.
  • Unique columns, and columns with mostly missing values, are dropped.
  • Date column is mapped to month, to help capture seasonality effect.
  • Notice how the feature tables are joined in succession. This is actually efficient because we are always joining index on one-to-one mapping.

Finally, let’s take a look at 5 training examples, and their features.

Now you have a well-defined dataset and feature set. You can tweak the scale of each feature and missing values to suit your model’s requirement.

For tree-based methods, which are invariant to feature scaling, we can directly apply the model, and simply focus on tuning parameters! See an example of a plain-vanilla gradient boosting machine?here.

It is nice to see that the useful features are all engineered, except for the?category?feature. Our efforts paid off! Also, the most predictive feature of event2 is how many nulls value were observed in event2. This is an illustrative case?where we cannot replace null values by median or average, because the fact that they are missing is correlated with the response variable!

 

Summary

As you can see, we have no intermediate CSV files, a very clean namespace in our notebook, and our feature engineering codes are reduced to a few straightforward SQL statements. There are two situations in which the SQL approach is even more efficient:

  • If your dataset is deployed on the cloud, you may be able to run distributed query. Most SQL server supports distirbuted query today. In Pandas, you need some extension called?Dask DataFrame.
  • If you can afford to pull data real-time, you can create SQL?views?instead of tables. In this way, every time you pull data in Python,?your data will always be up-to-date.

One fundamental restriction of this approach is that you must be able to directly connect to your SQL server in Python. If this is not possible, you may have to download the query result as a CSV file and load it in Python.

I hope you find this post helpful. Though I’m not advocating method over another, it is necessary to understand the advantage and limitation of each method, and have both methods ready in our toolkit. So we can apply whichever method that works best under the constraints.

Bio: Shaw Lu is a Data Scientist at Coupang. He is an engineering graduate seeking to leverage data science to inform business management, supply chain optimization, growth marketing and operation research. Official author in Toward Data Science.

Original. Reposted with permission.

Related:



apc是什么意思 刻舟求剑的寓意是什么 尾盘放量拉升意味着什么 山梨糖醇是什么 小孩咬人是什么原因
肌肉纤维化是什么意思 溺爱是什么意思 6月18日是什么节 有小肚子是什么原因 七十岁老人装什么牙合适
鱼油是什么 细菌感染是什么引起的 经期吃什么食物比较好 房水是什么 黑色鸟是什么鸟
吃什么水果降火 桃花指什么生肖 什么人什么己 rot是什么意思 空调出现pl是什么意思
什么蜂蜜最好hcv9jop3ns2r.cn 出球小动脉流什么血clwhiglsz.com 头加一笔是什么字hcv9jop8ns2r.cn 电轴不偏是什么意思hcv8jop9ns1r.cn 93年属什么的生肖wmyky.com
百折不挠的意思是什么hcv8jop9ns5r.cn 吃什么能排湿气hcv8jop1ns4r.cn 甘油三酯高是什么huizhijixie.com 处女膜在什么位置hcv9jop0ns2r.cn 一桌餐是什么意思hcv9jop8ns0r.cn
什么是sphcv9jop4ns9r.cn 晔字为什么不能取名hcv9jop7ns2r.cn 什么是pihcv8jop5ns4r.cn 空腹洗澡有什么危害zhongyiyatai.com 眼仁发黄是什么原因hcv9jop0ns8r.cn
泸州老窖什么档次hcv9jop2ns6r.cn 书房字画写什么内容好hcv9jop3ns4r.cn 什么的柳树hcv8jop3ns2r.cn 奥康属于什么档次hcv8jop7ns1r.cn 每天早上起床口苦是什么原因hcv9jop3ns3r.cn
百度