《机器学习实战》CART回归树源码问题:TypeError: list indices must be integers or slices, not tuple
2022/1/27 22:05:08
本文主要是介绍《机器学习实战》CART回归树源码问题:TypeError: list indices must be integers or slices, not tuple,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
书中代码1:
def binSplitDataSet(dataSet, feature, value): mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:][0] mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:][0] return mat0,mat1
改成:
def binSplitDataSet(dataSet, feature, value): featList = [] mat0 = [] mat1 = [] for featVec in dataSet: featList.append(featVec[feature]) for feat in featList: if feat > value: mat0.append(dataSet[featList.index(feat)]) else: mat1.append(dataSet[featList.index(feat)]) return mat0, mat1
书中代码2:
def regLeaf(dataSet): return mean(dataSet[:,-1])
改成:
def regLeaf(dataSet): valueList = [] for featVec in dataSet: valueList.append(featVec[-1]) return mean(valueList)
书中代码3:
def regErr(dataSet): return var(dataSet[:,-1]) * shape(dataSet)[0]
改成:
def regErr(dataSet): valueList = [] for featVec in dataSet: valueList.append(featVec[-1]) var = 0 mean = sum(valueList)/len(valueList) for value in valueList: var += (mean-value)**2 return var/len(valueList) * shape(dataSet)[0]
书中代码4:
def chooseBestSplit(dataSet, leafType=regLeaf, errType=regErr, ops=(1,4)): tolS = ops[0]; tolN = ops[1] if len(set(dataSet[:,-1].T.tolist()[0])) == 1: return None, leafType(dataSet) m,n = shape(dataSet) S = errType(dataSet) bestS = inf; bestIndex = 0; bestValue = 0 for featIndex in range(n-1): for splitVal in set(dataSet[:,featIndex]): mat0, mat1 = binSplitDataSet(dataSet, featIndex, splitVal) if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): continue newS = errType(mat0) + errType(mat1) if newS < bestS: bestIndex = featIndex bestValue = splitVal bestS = newS if (S - bestS) < tolS: return None, leafType(dataSet) #exit cond 2 mat0, mat1 = binSplitDataSet(dataSet, bestIndex, bestValue) if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): #exit cond 3 return None, leafType(dataSet) return bestIndex,bestValue
改成:
def chooseBestSplit(dataSet, leafType=regLeaf, errType=regErr, ops=(1, 4)): tolS = ops[0] tolN = ops[1] valueList = [] for featVec in dataSet: valueList.append(featVec[-1]) if len(list(set(valueList))) == 1: return None, leafType(dataSet) m, n = shape(dataSet) S = errType(dataSet) bestS = inf bestIndex = 0 bestValue = 0 for featIndex in range(n - 1): valueList = [] for featVec in dataSet: valueList.append(featVec[featIndex]) for splitVal in list(set(valueList)): mat0, mat1 = binSplitDataSet(dataSet, featIndex, splitVal) if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): continue newS = errType(mat0) + errType(mat1) if newS < bestS: bestIndex = featIndex bestValue = splitVal bestS = newS if (S - bestS) < tolS: return None, leafType(dataSet) mat0, mat1 = binSplitDataSet(dataSet, bestIndex, bestValue) if (shape(mat0)[0] < tolN) or (shape(mat1)[0] < tolN): return None, leafType(dataSet) return bestIndex, bestValue
运行结果:
这篇关于《机器学习实战》CART回归树源码问题:TypeError: list indices must be integers or slices, not tuple的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)