楼主: oliyiyi
1281 3

Decision Tree(实例代码 python 和 R) [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Decision Tree

This is one of my favorite algorithm and I use it quite frequently. It is a type of supervised learning algorithm that is mostly used for classification problems. Surprisingly, it works for both categorical and continuous dependent variables. In this algorithm, we split the population into two or more homogeneous sets. This is done based on most significant attributes/ independent variables to make as distinct groups as possible. For more details, you can read: Decision Tree Simplified.

source: statsexchange

In the image above, you can see that population is classified into four different groups based on multiple attributes to identify ‘if they will play or not’. To split the population into different heterogeneous groups, it uses various techniques like Gini, Information Gain, Chi-square, entropy.

The best way to understand how decision tree works, is to play Jezzball – a classic game from Microsoft (image below). Essentially, you have a room with moving walls and you need to create walls such that maximum area gets cleared off with out the balls.

So, every time you split the room with a wall, you are trying to create 2 different populations with in the same room. Decision trees work in very similar fashion by dividing a population in as different groups as possible.

More: Simplified Version of Decision Tree Algorithms

Python Code
  1. #Import Library
  2. #Import other necessary libraries like pandas, numpy...
  3. from sklearn import tree
  4. #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
  5. # Create tree object
  6. model = tree.DecisionTreeClassifier(criterion='gini') # for classification, here you can change the algorithm as gini or entropy (information gain) by default it is gini  
  7. # model = tree.DecisionTreeRegressor() for regression
  8. # Train the model using the training sets and check score
  9. model.fit(X, y)
  10. model.score(X, y)
  11. #Predict Output
  12. predicted= model.predict(x_test)
复制代码

R Code

  1. library(rpart)
  2. x <- cbind(x_train,y_train)
  3. # grow tree
  4. fit <- rpart(y_train ~ ., data = x,method="class")
  5. summary(fit)
  6. #Predict Output
  7. predicted= predict(fit,x_test)
复制代码



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Decision python Tree CISI Dec

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
MouJack007 发表于 2017-9-12 19:34:59 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

藤椅
MouJack007 发表于 2017-9-12 19:35:27 |只看作者 |坛友微信交流群

使用道具

板凳
minixi 发表于 2017-9-13 10:56:27 |只看作者 |坛友微信交流群
谢谢分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 11:27