如何在Python中使用多类别变量创建类似R风格的预测模型

您知道如何创建类似R风格的集成方法分类器的预测模型吗?

ded.fit(formula="X ~ Y + Z**2", data=fed)

目前的代码看起来像这样:

from sklearn.ensemble import RandomForestClassifiermodel = RandomForestClassifier(n_estimators=100, min_samples_leaf=10,    random_state=1)model.fit(x_train, y_train)

您可能会问我为什么需要这样做?

  1. 我需要添加更多的变量,不仅是X和Y,还需要Z、P、Q和R。
  2. 我需要像在R中那样进行实验,看看给某个变量添加指数、乘法或除法是否会增加或减少预测的准确性,如下面的公式所示:

    X ~ Y + Z^2" 或 "X ~ Y + Z + (P*2) + Q**2

任何回答都将不胜感激。提前感谢您。


回答:

像下面这样的代码应该可以工作:

import pandas as pdimport numpy as npX = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('XZ'))y = np.random.randint(2,size=100) # 用于二分类的数据标签X['Z2'] = X.Z**2    # 添加更多特征print X.head() # 注意添加的特征 Z^2#    X   Z    Z2#0  88  90  8100#1  49  63  3969#2  27  23   529#3  47  71  5041#4  21  98  9604train_samples = 80  # 用于训练模型的样本数X_train = X[:train_samples]X_test = X[train_samples:]y_train = y[:train_samples]y_test = y[train_samples:]from sklearn.ensemble import RandomForestClassifierfrom pandas_ml import ConfusionMatriximport matplotlib.pyplot as pltmodel = RandomForestClassifier(n_estimators=100, min_samples_leaf=10, random_state=1)model.fit(X_train, y_train)y_pred = model.predict(X_test)#print confusion_matrix(y_test, y_pred)cm = ConfusionMatrix(y_test, y_pred)print cm# Predicted  0   1  __all__# Actual# 0          3   4        7# 1          4   9       13# __all__    7  13       20cm.plot()plt.show()

enter image description here

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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