Skip to main content
 首页 » 编程设计

python之在拟合随机森林分类器时遇到问题

2025年05月04日14zdz8207

我正在使用 IPython 的 UI 和适当的库(Anaconda、pandas、numpy)

我已经声明了一个 pandas 对象 df,我已经对其进行了更改,以便第一列是我希望预测的所需值,但我的代码仍然继续停在第 10 行。第 11 行没有运行。相反,终端 IPython 窗口退出并显示此错误:

~/anaconda/bin/python.app: line 3: 45581 Killed: 9               ~/anaconda/python.app/Contents/MacOS/python "$@" 

代码开始

In [1]: import pandas as pd 
 
In [2]: import numpy as np 
 
In [3]: df = pd.read_csv('train.csv', header=0) 
 
In [4]: cols = list(df) 
 
In [5]: cols.insert(0, cols.pop(cols.index('Cover_Type'))) %Cover type is column I wanted to bring to column position 1 or value I'm predicting 
 
In [6]: cols 
 
In [7]: df = df.ix[:, cols] 
 
In [8]: df 
 
In [9]: forest = RandomForestClassifier(n_estimators = 100) 
 
In [10]: forest = forest.fit(train_data[0::,1::],train_data[0::,0]) 
 
In [11]: output = forest.predict(test_data) 
 
In [12]: train_data = df.values 
 
In [13]: train_data 

请您参考如下方法:

您正在使用第 10 行中的火车数据,该数据不可用。您稍后分配它。

先分配训练数据,然后在forest.fit中使用