Python: 如何使用用户定义的函数拟合模型

我正在研究孤立森林。我实现了以下代码来构建包含iTrees的孤立森林。

import pandas as pdimport numpy as npimport randomfrom sklearn.model_selection import train_test_splitclass ExNode:    def __init__(self,size):        self.size=sizeclass InNode:    def __init__(self,left,right,splitAtt,splitVal):        self.left=left        self.right=right        self.splitAtt=splitAtt        self.splitVal=splitValdef iForest(X,noOfTrees,sampleSize):forest=[]hlim=int(np.ceil(np.log2(max(sampleSize, 2))))for i in range(noOfTrees):    X_train=X.sample(sampleSize)    forest.append(iTree(X_train,0,hlim))return forestdef iTree(X,currHeight,hlim):if currHeight>=hlim or len(X)<=1:    return ExNode(len(X))else:    Q=X.columns    q=random.choice(Q)    p=random.choice(X[q].unique())    X_l=X[X[q]<p]    X_r=X[X[q]>=p]    return InNode(iTree(X_l,currHeight+1,hlim),iTree(X_r,currHeight+1,hlim),q,p)def pathLength(x,Tree,currHeight):if isinstance(Tree,ExNode):    return currHeighta=Tree.splitAttif x[a]<Tree.splitVal:    return pathLength(x,Tree.left,currHeight+1)else:    return pathLength(x,Tree.right,currHeight+1)def _h(i):    return np.log2(i) + 0.5772156649 def _c(n):    if n > 2:        h = _h(n-1)        return 2*h - (2*(n - 1)/n)    if n == 2:        return 1    else:        return 0def _anomaly_score(score, n_samples):    score = -score/_c(n_samples)    return 2**scoredf=pd.read_csv("db.csv")y_true=df['Target']df_data=df.drop('Target',1)sampleSize=256X_train, X_test, y_train, y_test = train_test_split(df_data, y_true, test_size=0.3)ifor=iForest(X_train,100,sampleSize)for index, row in test.iterrows():        sxn = 0;    testLenLst = []    for tree in ifor:        testLenLst.append(pathLength(row,tree,0))                 if(len(testLenLst) != 0):        ehx = (sum(testLenLst) / float(len(testLenLst)))          if(_anomaly_score(ehx,sampleSize) >= .5):            print("Anomaly S(x,n) " + str(_anomaly_score(ehx,sampleSize)))        else:            print("Normal S(x,n) " + str(_anomaly_score(ehx,sampleSize)))

事实上,真正的挑战在于我想展示一个iTree。为了做到这一点,我使用.fit()函数来构建模型。但是.fit()只能用于基于Python预定义算法构建的模型。而在我的案例中,是我自己开发了孤立森林算法。下面是我尝试构建模型以及展示iTree的方式。

from sklearn.tree import export_graphvizifor.fit(X_train)estimator = ifor.tree[1]export_graphviz(estimator,                 out_file='tree.dot',                 feature_names = df.feature_names,                class_names = df.target_names,                rounded = True, proportion = False,                 precision = 2, filled = True)from subprocess import callcall(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])from IPython.display import Image Image(filename = 'tree.png')

它显示了以下错误:尝试展示iTree时遇到的错误


回答:

你的问题不够清晰,但最佳实践是遵循如何在sklearn中编写自定义估计器并对其使用交叉验证?来编写自定义估计器,并编写fit()方法的实现,附带适当的规则,否则会非常 confusing,

由于Python使用鸭子类型,尝试避免这种复杂性,并使用sklearn.BaseEstimator

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注