深度基础学习:Decision Tree 代码实现
2016-05-11 19:31
555 查看
python+anaconda
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from sklearn.feature_extraction import DictVectorizer
import xlrd
from sklearn import preprocessing
from sklearn import tree
from sklearn.externals.six import StringIO
data = xlrd.open_workbook('/home/sanmao/workspace/test2.xls')
table = data.sheet_by_index(0)
print table.ncols
print table.nrows
print table.cell(0,1).value
print table.row_values(0)
featureList = []
labelList = []
for row in range(1,table.nrows):
labelList.append(table.cell(row,table.ncols-1).value)
rowDict = {}
for i in range(1,table.ncols-1):
rowDict[table.cell(0,i).value] = table.cell(row,i).value
featureList.append(rowDict)
print labelList
print featureList
#Vectorizer featureList
vec = DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()
print dummyX
#Vectorizer labelList
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)
print dummyY
#entropy xinxisahng
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummyX,dummyY)
print clf
with open("/home/sanmao/workspace/test2.dot","w") as f:
f = tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file = f)
oneRowX = dummyX[0,:]
#print oneRowX
#newRowX = oneRowX
#print newRowX
#oneRowX[0] = 1
#oneRowX[2] = 0
#print newRowX
#print oneRowX
print oneRowX
newRowX = oneRowX
newRowX[0] = 1
newRowX[1] = 0
print newRowX
predicredY = clf.predict(newRowX)
print predictedY
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from sklearn.feature_extraction import DictVectorizer
import xlrd
from sklearn import preprocessing
from sklearn import tree
from sklearn.externals.six import StringIO
data = xlrd.open_workbook('/home/sanmao/workspace/test2.xls')
table = data.sheet_by_index(0)
print table.ncols
print table.nrows
print table.cell(0,1).value
print table.row_values(0)
featureList = []
labelList = []
for row in range(1,table.nrows):
labelList.append(table.cell(row,table.ncols-1).value)
rowDict = {}
for i in range(1,table.ncols-1):
rowDict[table.cell(0,i).value] = table.cell(row,i).value
featureList.append(rowDict)
print labelList
print featureList
#Vectorizer featureList
vec = DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()
print dummyX
#Vectorizer labelList
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)
print dummyY
#entropy xinxisahng
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummyX,dummyY)
print clf
with open("/home/sanmao/workspace/test2.dot","w") as f:
f = tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file = f)
oneRowX = dummyX[0,:]
#print oneRowX
#newRowX = oneRowX
#print newRowX
#oneRowX[0] = 1
#oneRowX[2] = 0
#print newRowX
#print oneRowX
print oneRowX
newRowX = oneRowX
newRowX[0] = 1
newRowX[1] = 0
print newRowX
predicredY = clf.predict(newRowX)
print predictedY
相关文章推荐
- leetcode(6),Excel Sheet Column Title和Excel Sheet Column Number(python)
- spring+struct2+hibernate
- 数据库为DATETIME java存值
- java 多线程循环输出ABC
- QT绘图应用:QT绘制一个盒图
- Java 垃圾回收器学习
- C++——C/C++字符串处理大集合
- java七种反转字符串的方法
- POJ 2484-A Funny Game硬币排成环(博弈-找规律)
- C语言——数组指针和通过指针引用数组元素的方法总结
- C++实验5数组选择
- 【MVC框架整合】之 SpringMVC3.2.0+MyBatis3.1.1+Spring3.2创建测试目录
- 在Java中怎么描述一个事物?
- C++ 无锁队列 ABA <2>
- spring @Transactional 方法内事务不起作用的解决办法
- Leetcode 20. Valid Parentheses
- python super
- PHPExcel_CI操作实例
- java中精品总结关于函数的区别
- java基础中的线程问题