日晡是什么意思| 什么药可以流产| 头昏是什么原因| 九个月的宝宝吃什么辅食食谱| 贼是什么生肖| 肌肉僵硬是什么原因引起的| 伤风败俗是什么意思| 地球属于什么星系| 白玫瑰代表什么意思| 婴儿打嗝是什么原因引起的| 榧读什么| 疾控中心属于什么单位| sakose是什么牌子| 扁桃体肿大有什么症状| 湿热内蕴吃什么中成药| 古代男子成年叫什么| 小孩晚上睡觉流口水是什么原因| 海豹油有什么功效| 组织液是什么| 眼皮有痣代表什么| aqi是什么| 软冷冻室一般放什么东西| 脚麻是什么病的前兆| 前年是什么年| cma检测是什么| 坐北朝南什么意思| 温柔的动物是什么生肖| 幽闭恐惧症是什么| tory burch什么牌子| 天降横财什么意思| 全麦面包是什么意思| 嫡长子是什么意思| 蛋白高是什么病| 靶器官是什么意思| 梦见很多鱼是什么意思| 阴唇痒用什么药| 公务员五行属什么| 胃反酸吃什么药| 休学需要什么条件| 腰间盘突出是什么症状| 四川地震前有什么预兆| camper是什么牌子| 前列腺在什么地方| 仪表堂堂是什么生肖| 洛阳以前叫什么名字| 低血糖是什么引起的| 减肥期间可以吃什么水果| 黄体酮不足吃什么药| 赡养什么意思| 梦见被追杀是什么预兆| 消化道出血有什么症状| 日本有什么特产| 杨梅泡酒有什么功效和作用| 成人男性尿床是什么原因造成的| 白羊座什么性格| 大便是黑色的是什么原因| 怠工是什么意思| 西周王陵为什么找不到| 别开生面是什么意思| 鹦鹉吃什么东西| 什么时候可以领退休金| 花旦是什么意思| 八月二十是什么星座| 94年属狗的是什么命| 胃癌早期有什么症状| 总恶心是什么病的前兆| 小儿流清鼻涕吃什么药效果好| 果冻是什么做的| 3月5日是什么星座| 日本人什么时候投降的| 梦见迁祖坟有什么预兆| 物理压榨油是什么意思| 资生堂属于什么档次| 为什么有眼袋是什么原因引起的| 肺部气肿吃什么药能治好| 什么是远视眼| 老九门讲的是什么故事| 小孩风寒感冒吃什么药| 月经稀发是什么意思| 洋辣子蛰了用什么药| 尿浑浊是什么原因| 腰疼是什么病的前兆| 正事是什么意思| 四平八稳是什么生肖| 结婚5年是什么婚| 痣为什么会越来越多| 穿山甲到底说了什么| 神经痛吃什么药效果好| 蜂蜜变质是什么样子| sad什么意思| 天伦之乐什么意思| 粗脖子病是什么原因引起的| 朋友圈发女朋友照片配什么文字| bacon是什么意思| 为什么梦不到死去的亲人| 发芽土豆含有什么毒素| 逍遥丸主治什么病| 捉虫是什么意思| 什么动物不怕热| 丝状疣用什么药膏最好| 二次元谷子是什么意思| 右胸是什么器官| 什么矿泉水最好| 灵芝搭配什么煲汤最好| 丢包率是什么意思| 60岁男人喜欢什么样的女人| 梦见好多狗是什么预兆| 为什么打牌老输| 梦到捉鱼是什么意思| 红裤子配什么上衣| 牛仔蓝配什么颜色好看| 头晕有点恶心是什么原因| 马岱字什么| 十三太保什么意思| 起酥油是什么做的| 12.16是什么星座| 脍炙人口什么意思| 能量守恒是什么意思| vsc是什么意思| 宫寒是什么| 颈动脉斑块吃什么药效果最好| 11月26是什么星座| 为什么润月| 总流鼻血是什么原因| 子不孝父之过下一句是什么| 血糖用什么字母表示| 顺产1-3天吃什么好| 献血有什么坏处| 全身疼是什么病| 什么人容易得白血病| 食管炎吃什么药最好| 小儿发烧吃什么食物好| 中医考证需要什么条件| 两个大于号是什么车| 处女座幸运颜色是什么| 平安夜做什么| 肾结石不能吃什么| 一个月一个非念什么| 剪舌系带挂什么科| 嘴上长痘痘是什么原因| 茶歇是什么意思| 猴与什么属相相配最好| 西洋参吃多了有什么副作用| 俞伯牙摔琴谢知音摔的是什么乐器| 水是什么生肖| 蒲公英叶和根的功效有什么不同| 山药为什么煮熟了也麻口| cm2是什么单位| 医保什么时候到账| 什么风什么面| 嗜睡乏力没精神容易疲劳是什么原因| 腺肌瘤是什么病| 肉是什么意思| 立夏吃什么食物| 抗组胺药是什么意思| 减肥吃什么蔬菜| 猫藓长什么样| 龙须菜是什么菜| 漱口水有什么作用| 你掀起波澜抛弃了我是什么歌| 远山含黛是什么意思| 牙冠是什么样子的图片| camper是什么牌子| 头晕为什么做眼震检查| aoc是什么牌子| 值神是什么意思| 1129什么星座| 伏天从什么时候开始| 鸡飞狗跳是什么生肖| 歧路亡羊告诉我们什么道理| 今天开什么| 热惊厥病发是什么症状| 如意是干什么用的| pe医学上是什么意思| 梦见大胖小子是什么意思| 五音是什么意思| 缺蛋白质吃什么补得快| 热结旁流是什么意思| 高诊是什么意思| 男人吃什么补肾| 唯小人与女子难养也什么意思| 薛之谦的真名叫什么| 梨子和什么一起榨汁好喝| 三阳开泰是什么生肖| 左肾尿盐结晶是什么意思| 毛主席为什么不进故宫| 黑醋是什么醋| 新疆是什么民族| 摇头晃脑是什么生肖| 下肢肿胀是什么原因| 摧枯拉朽是什么意思| 吹空调头疼吃什么药| 7月23是什么星座| 为什么一喝水就出汗| pd是什么元素| 月经肚子疼是什么原因| 嘴角有痣代表什么| 兰州市区有什么好玩的地方| 电轴左偏是什么原因| 一月二十五号是什么星座| 什么是提示语| 蛆是什么| imei是什么意思| 好样的什么意思| 后循环缺血吃什么药| 基因病是什么意思| 糖宝是什么意思| 爱因斯坦是什么学家| 什么时候说什么话| 七月份有什么节日吗| 苏联什么时候解体| 久之的之是什么意思| 阴茎痒是什么原因| 自我安慰是什么意思| 舒张压偏高是什么原因造成的| 天天睡不着觉什么原因| 白细胞多是什么意思| 枇杷什么味道| 老实的动物是什么生肖| 仿生是什么意思| 什么是阴历| 秀气是什么意思| 女性潮热是什么症状| 三尖瓣轻度反流是什么意思| 醒酒是什么意思| 胎动频繁到什么程度说明缺氧| 23333是什么意思| 霍金是什么病| 部长是什么职位| 代孕是什么意思| 疝气吃什么药| 胰腺钙化灶是什么意思| 什么t恤质量好| 梦见血是什么意思| 腰花是什么部位| 桔子树用什么肥料最好| 支气管疾患是什么意思| 京东自营什么意思| 啄木鸟为什么不会脑震荡| 山药叶子长什么样图片| 什么动物睡觉不闭眼| 人生于世上有几个知己是什么歌| 发际线是什么| 腿上有白色条纹是什么| 突然眼睛充血是什么原因引起的| 中医湿气重是什么意思| hpa是什么意思| 为什么会头痛| 旁风草长什么样| 耳朵有回音是什么原因| 宝宝拉肚子吃什么| qcy是什么牌子| 梦里梦到蛇有什么预兆| 9月11号是什么星座| 甘之如饴什么意思| 女人吃什么养颜又美白| 一什么扇子| 吃维生素b族有什么好处| 黄鼻涕是什么类型的感冒| 吃甲硝唑有什么副作用| 女人怀孕的最佳时间是什么时间| 性向是什么意思| 什么东西越生气越大| 胸小是缺少什么营养| 百度
 

7座丰田汉兰达2013款降价 北京丰田汉兰达促销优

百度 李少红表示:“每一年终评委的工作都很艰巨,好电影太多了,很多时候很难选择。

This post is about fast-tracking the study and explanation of tree concepts for the data scientists so that you breeze through the next time you get asked these in an interview.



Algorithms and data structures are an integral part of data science. While most of us data scientists don't take a proper algorithms course while studying, they are crucial all the same.

Many companies ask data structures and algorithms as part of their interview process for hiring data scientists.

Now the question that many people ask here is what is the use of asking a data scientist such questions.?The way I like to describe it is that a data structure question may be thought of as a coding aptitude test.

We all have given aptitude tests at various stages of our life, and while they are not a perfect proxy to judge someone, almost nothing ever really is.?So, why not a standard algorithm test to judge people's coding ability.

But let's not kid ourselves, they will require the same zeal to crack as your Data Science interviews, and thus, you might want to give some time for the study of algorithms and Data structure questions.

This post is about fast-tracking this study and explaining tree concepts for the data scientists so that you breeze through the next time you get asked these in an interview.

 

But First, Why are Trees important for Data Science?

 
To data scientists, Trees mean a different thing than they mean for a Software Engineer.

For a software engineer, a tree is just a simple Data Structure they can use to manage hierarchical relationships while for a Data Scientists trees form the basis of some of the most useful classification and regression algorithms.

So where do these two meet?

They are necessarily the same thing. Don't be surprised. Below is how data scientists and software engineer's look at trees.

Figure

They are essentially the same

 

The only difference is that Data science tree nodes keep much more information that helps us in identifying how to traverse the tree. For example, in the case of Data science tree for prediction, we will look at the feature in the node and determine which way we want to move based on the split value.

If you want to write your decision tree from scratch, you might need to understand how trees work from a software engineering perspective too.

 

Types of Trees:

 
In this post, I will only be talking about two kinds of trees that get asked a lot in Data Science interview questions. Binary Trees(BT) and an extension of Binary Trees called Binary Search Trees(BST).

 

1. Binary Trees:

 
A binary tree is simply a tree in which each node has up to two children. A decision tree is an example we see in our day to day lives.

Figure

Binary Tree: Each Node has up to 2 children

 

 

2. Binary Search Tree(BST):

 
A binary search tree is a binary tree in which:

  • All left descendants of a node are less than or equal to the node, and
  • All right descendants of the node are greater than the node.

There are variations to this definition when it comes to equalities. Sometimes the equalities are on the right-hand side or either side. Sometimes only distinct values are allowed in the tree.

 

8 is greater than all the elements in the left subtree and smaller than all elements in the right subtree. The same could be said for any node in the tree.

 

Creating a Simple Tree:

 
So How do we construct a simple tree?

By definition, a tree is made up of nodes. So we start by defining the?node?class which we will use to create nodes. Our node class is pretty simple as it holds value for a node, the location of the left child and the location of the right child.

class node:
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None


We can create a simple tree now as:

root = node(1)
root.left = node(2)
root.right = node(3)


Now I have noticed that we cannot really get the hang of Tree-based questions without doing some coding ourselves.

So let us get a little deeper into the coding part with some problems I found most interesting when it comes to trees.

 

Inorder Tree Traversal:

 
There are a variety of ways to traverse a tree, but I find the inorder traversal to be most intuitive.

When we do an inorder traversal on the root node on a Binary Search tree, it visits/prints the node in ascending order.

def inorder(node):
    if node:
        inorder(node.left)
        print(node.val)
        inorder(node.right)


This above method is pretty important as it allows us to visit all the nodes.

So if we want to search for a node in any binary tree, we might try to use inorder tree traversal.

 

Creating a Binary Search Tree from a Sorted array

 
What kind of coders will we be if we need to create a tree piece by piece manually as we did above?

So can we create a BST from a sorted array of unique elements?

def create_bst(array,min_index,max_index):
    if max_index<min_index:
        return None
    mid = int((min_index+max_index)/2)
    root = node(array[mid])
    leftbst = create_bst(array,min_index,mid-1)
    rightbst = create_bst(array,mid+1,max_index)
    root.left = leftbst
    root.right = rightbst
    return root
a = [2,4,5,6,7]
root = create_bst(a,0,len(a)-1)


Trees are inherently recursive, and so we use recursion here. We take the mid element of the array and assign it as the node. We then apply the?create_bst?function to the left part of the array and assign it to node.left and do the same with the right part of the array.

And we get our BST.

Have we done it right? We can check it by creating the BST and then doing an inorder traversal.

inorder(root)
------------------------------------------------------------
2
4
5
6
7


Seems Right!

 

Let's check if our tree is a Valid BST

 

Figure

Think Recursion!!!

 

But again what sort of coders are we if we need to print all the elements and check manually for the BST property being satisfied?

Here is a simple code to check if our BST is valid or not. We assume strict inequality in our Binary Search Tree.

def isValidBST(node, minval, maxval):
    if node:
        # Base case
        if node.val<=minval or node.val>=maxval:
            return False
        # Check the subtrees changing the min and max values
        return isValidBST(node.left,minval,node.val) &   isValidBST(node.right,node.val,maxval)
    return True
isValidBST(root,-float('inf'),float('inf'))
--------------------------------------------------------------
True


We check the subtrees recursively if they satisfy the Binary Search tree property or not. At each recursive call, we change the?minval?or?maxval?for the call to provide the function with the range of allowed values for the subtree.

 

Conclusion

 
In this post, I talked about Trees from a software engineering perspective. If you want to see trees from a data science perspective, you might take a look at this post.

 
The Simple Math behind 3 Decision Tree Splitting criterions
Understanding Splitting Criterions
 

Trees form the basis of some of the most asked questions in Data Science algorithmic interviews. I used to despair such tree-based questions in the past, but now I have grown to like the mental exercise involved in them. And I love the recursive structure involved in such problems.

And while you can go a fair bit in data science without learning them, you can learn them just for a little bit of fun and maybe to improve your programming skills.

Here is a small?notebook?for you where I have put all these small concepts for you to try and run.
Take a look at my other posts in the?Algorithmic Interviews Series, if you want to learn about?Recursion,?Dynamic Programming?or?Linked Lists.

 

Continue Learning

 
If you want to read up more on Algorithms and Data structures, here is an?Algorithm Specialization on Coursera by UCSanDiego,?which I highly recommend.

Thanks for the read. I am going to be writing more beginner-friendly posts in the future too. Follow me up at?Medium?or Subscribe to my?blog?to be informed about them. As always, I welcome feedback and constructive criticism and can be reached on Twitter?@mlwhiz.

Also, a small disclaimer: There might be some affiliate links in this post to relevant resources, as sharing knowledge is never a bad idea.

 
Bio: Rahul Agarwal is Senior Statistical Analyst at WalmartLabs. Follow him on Twitter?@mlwhiz.

Original. Reposted with permission.

Related:



前哨是什么意思 2.16是什么星座 消炎药不能和什么一起吃 谛听是什么意思 21金维他有什么作用
女人怕冷是什么原因 独角仙生活在什么地方 净土的意思是什么 天蝎座女生配什么星座 后背痛是什么病的先兆
吃什么都苦是什么原因 耳耵聍是什么东西 心功能iv级是什么意思 圣罗兰属于什么档次 葫芦代表什么寓意
肌酸激酶偏高吃什么药 红鸾星动是什么意思 顽固是什么意思 甲亢吃什么盐 贡菜是什么菜做的
3月9日什么星座hcv9jop8ns0r.cn 什么国家的钱最值钱hcv9jop7ns4r.cn 耳朵痒是什么预兆hcv9jop3ns6r.cn badus是什么牌子的手表hcv9jop4ns5r.cn 指甲黑是什么原因hcv9jop1ns9r.cn
什么猫不掉毛hcv8jop1ns5r.cn 低聚果糖是什么东西jiuxinfghf.com 接触隔离什么意思520myf.com 生死劫是什么意思hcv9jop3ns6r.cn 文曲星什么意思hcv8jop3ns0r.cn
什么东西掉进水里不会湿hcv7jop6ns9r.cn 口腔经常溃疡是什么原因hcv8jop0ns3r.cn 收阴是什么意思hcv8jop7ns6r.cn 手掌痒是什么原因hcv9jop2ns6r.cn 慢性非萎缩性胃炎吃什么药效果好hcv9jop3ns6r.cn
1964年是什么命hcv9jop6ns8r.cn 心里紧张吃什么药缓解hcv9jop7ns3r.cn 兰花长什么样hcv9jop2ns5r.cn 梦见和老公结婚是什么意思hcv7jop9ns4r.cn 舌头麻是什么原因hcv8jop4ns5r.cn
百度