from sklearn.feature_extraction import DictVectorizer import csv from sklearn import preprocessing from sklearn import tree from sklearn.externals.six import StringIO allElectronicsData = open("F:\\tree.csv","r") reader = csv.reader(allElectronicsData) headers = next(reader) #The first line of data content # print(headers) featureList = [] labelList = [] for row in reader: labelList.append(row[len(row)-1]) rowDict = {} for i in range(1,len(row)-1): rowDict[headers[i]] = row[i] featureList.append(rowDict) # print(labelList) # print(featureList) vec = DictVectorizer () dummyX = vec.fit_transform(featureList).toarray() print("dummyX:\n"+str(dummyX)) # print(vec.get_feature_names()) # print("labelList:\n" + str(labelList)) lb = preprocessing.LabelBinarizer() dummyY = lb.fit_transform(labelList) print("dummyY:\n" + str(dummyY)) #train classifier clf = tree.DecisionTreeClassifier(criterion = 'entropy') clf = clf.fit(dummyX,dummyY) print("clf:\n" + str(clf)) with open("allElectronicsInformationGainOri.dot","w") as f: f = tree.export_graphviz(clf,feature_names = vec.get_feature_names(),out_file=f) oneRowX = dummyX[0,:] print("oneRowX:\n" + str(oneRowX)) newRow = oneRowX newRow[0] = 1 newRow[2] = 0 print("oneRowX:\n" + str(oneRowX)) predictedY = clf.predict(oneRowX) print("predictedY:" + str(predictedY))
Machine Learning----Decision Tree (2)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324806861&siteId=291194637
Recommended
Ranking