(self,X)
| 194 | |
| 195 | |
| 196 | def predict(self,X): |
| 197 | if self._tree==None: |
| 198 | raise NotFittedError("Estimator not fitted, call `fit` first") |
| 199 | |
| 200 | #类型检查 |
| 201 | if isinstance(X,np.ndarray): |
| 202 | pass |
| 203 | else: |
| 204 | try: |
| 205 | X = np.array(X) |
| 206 | except: |
| 207 | raise TypeError("numpy.ndarray required for X") |
| 208 | |
| 209 | def _classify(tree,sample): |
| 210 | """ |
| 211 | 用训练好的决策树对输入数据分类 |
| 212 | 决策树的构建是一个递归的过程,用决策树分类也是一个递归的过程 |
| 213 | _classify()一次只能对一个样本(sample)分类 |
| 214 | To Do: 多个sample的预测怎样并行化? |
| 215 | """ |
| 216 | featIndex = tree.keys()[0] |
| 217 | secondDict = tree[featIndex] |
| 218 | key = sample[int(featIndex[1:])] |
| 219 | valueOfkey = secondDict[key] |
| 220 | if isinstance(valueOfkey, dict): |
| 221 | label = _classify(valueOfkey,sample) |
| 222 | else: label = valueOfkey |
| 223 | return label |
| 224 | |
| 225 | if len(X.shape)==1: |
| 226 | return _classify(self._tree,X) |
| 227 | else: |
| 228 | results = [] |
| 229 | for i in range(X.shape[0]): |
| 230 | results.append(_classify(self._tree,X[i])) |
| 231 | return np.array(results) |
| 232 | |
| 233 | def show(self): |
| 234 | if self._tree==None: |
no test coverage detected