我想從csv加載訓練和測試數據,在scikit/sklearn中運行隨機森林迴歸器,然後預測測試文件的輸出。Python Scikit隨機森林迴歸錯誤
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()
其實,'predict'的結果是一個浮點數組。 RandomForestRegressor是一個迴歸模型,而不是分類器。 –
當然,你是對的。 –