我正在尝试从 csv 加载训练和测试数据,在 scikit/sklearn 中运行随机森林回归器,然后预测测试文件的输出。
TrainLoanData.csv 文件包含 5 列;第一列是输出,接下来的 4 列是特征。TestLoanData.csv 包含 4 列 - 特征。
当我运行代码时,我收到错误:
predicted_probs = ["%f" % x[1] for x in predicted_probs]
IndexError: invalid index to scalar variable.
这是什么意思?
这是我的代码:
import numpy, scipy, sklearn, csv_io //csv_io from https://raw.github.com/benhamner/BioResponse/master/Benchmarks/csv_io.py
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
def main():
#read in the training file
train = csv_io.read_data("TrainLoanData.csv")
#set the training responses
target = [x[0] for x in train]
#set the training features
train = [x[1:] for x in train]
#read in the test file
realtest = csv_io.read_data("TestLoanData.csv")
# random forest code
rf = RandomForestRegressor(n_estimators=10, min_samples_split=2, n_jobs=-1)
# fit the training data
print('fitting the model')
rf.fit(train, target)
# run model against test data
predicted_probs = rf.predict(realtest)
print predicted_probs
predicted_probs = ["%f" % x[1] for x in predicted_probs]
csv_io.write_delimited_file("random_forest_solution.csv", predicted_probs)
main()