我有一个 466 x 1025 的数据框。1024 个变量和目标组成列。我正在对数据集使用随机森林回归,并尝试使用折叠来获得更一致的预测。我的目标是正确拆分,但是当应用于数据时,列被拆分而不是行。我得到 466 x 372 的训练数据和 466 x 94 的测试数据。我需要 372 x 1024 的训练数据和 94 x 1024 的测试数据。我该如何解决这个问题?注意:当我使用 train_test_split() 时它可以正常工作
编码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestRegressor
#read the data files, verify types
df = pd.read_csv('./allMolecules.csv')
#the data frame is ready, now it's time for the random forest.
#split data into train and test
xTrain, xTest, yTrain, yTest = train_test_split(finalDF.drop(['Target'], axis=1), finalDF['Target'],test_size=0.2)
model = RandomForestRegressor(n_estimators=1000)
output = model.fit(xTrain,yTrain)
score = model.score(xTest,yTest)
print('Model Settings:\n{0}\n'.format(output))
print('R2: {0}'.format(score))
folds = KFold(n_splits=5)
scores = []
data = finalDF.drop(['Target'], axis=1)
for trainIndex, testIndex in folds.split(finalDF.drop(['Target'], axis=1)):
print(trainIndex, testIndex)
xTrain = data[trainIndex]
xTest = (finalDF.drop(['Target'], axis=1))[testIndex]
yTrain = finalDF['Target'][trainIndex]
yTest = finalDF['Target'][testIndex]
print('\n\n{0}\n\n{1}\n\n{2}\n\n{3}'.format(xTrain,xTest,yTrain,yTest))
output = model.fit(xTrain, yTrain)
scores.append(model.score(xTest, yTest))
print(scores)