如何将随机森林分类器应用于所有数据集,一次一小部分 python
How to apply a randomforest classifier to all of the dataset, a small section at a time in python
所以我正在进行 Kaggle 竞赛,测试数据集的大小为 880,000 行。我想在其中的 10,000 行部分应用随机森林分类器。但仍然适用于所有这些。
这是我的分类器的设置方式
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=100)
# Training data features, skip the first column 'Crime Category'
train_features = train[:, 1:]
# 'Crime Category' column values
train_target = train[:, 0]
clf = clf.fit(train_features, train_target)
score = clf.score(train_features, train_target)
"Mean accuracy of Random Forest: {0}".format(score)
我用它来训练我的模型并获得准确性。我使训练数据更小,这样我就能更快地得到结果。但是为了提交给 Kaggle,我需要预测测试数据。基本上我想这样做:
test_x = testing_data[:, 1:]
print('-',*38)
for every 10,000 rows in test_x
test_ y = clf.predict(value)
print(".")
add the values to an array then do the next 10,000 rows
对于我想要预测值的每 10,000 行,将预测值添加到某处,然后执行接下来的 10,000 行。每当我同时处理所有 880,000 行时,我的计算机就会死机。我希望通过一次执行 10,000 行并使用 print(".") 我会得到一个进度条。我使用 test= test.values
将 test.csv 从 pandas
dataframe
更改为 values
。
我提供了尽可能多的信息,如果您需要更多信息,请告诉我。
使用 pd.DataFrame
,您可以使用新的 DataFrame
迭代 index
和 concat
结果的块。对于 np.array
,使用 np.array_split
.
def chunks(l, n):
""" Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
test_x = pd.DataFrame(test_x)
test_result = pd.DataFrame()
for chunk in chunks(test_x.index, 10000):
test_data = test_x.ix[chunk]
test_result = pd.concat([test_result, pd.DataFrame(clf.predict(test_data))])
我假设您的索引是连续整数...
groups = test_x.groupby(test_x.index // 10000)
groups.apply(clf.predict)
如果索引不是连续整数,则有可能...
groups = test.groupby(test.reset_index().index // 10000)
这是一个完整的例子...
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
train, test = (df[:100], df[100:])
y_train, y_test = (iris.target[:100], iris.target[100:])
clf = RandomForestClassifier()
clf.fit(train, y_train)
groups = test.groupby(test.index // 10)
groups.apply(clf.predict)
输出是 Pandas 系列预测列表...
10 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
11 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
12 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
13 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2018 年,来自 fast.ai 的 fastai 0.7 库有一个 set_rf_samples() 函数,它具有一些特殊功能。如果您登陆此页面,强烈建议您对其进行调查。您可以在 Jeremy Howard 的 YouTube 频道上观看机器学习入门 MOOC 以及实施细节。
所以我正在进行 Kaggle 竞赛,测试数据集的大小为 880,000 行。我想在其中的 10,000 行部分应用随机森林分类器。但仍然适用于所有这些。
这是我的分类器的设置方式
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=100)
# Training data features, skip the first column 'Crime Category'
train_features = train[:, 1:]
# 'Crime Category' column values
train_target = train[:, 0]
clf = clf.fit(train_features, train_target)
score = clf.score(train_features, train_target)
"Mean accuracy of Random Forest: {0}".format(score)
我用它来训练我的模型并获得准确性。我使训练数据更小,这样我就能更快地得到结果。但是为了提交给 Kaggle,我需要预测测试数据。基本上我想这样做:
test_x = testing_data[:, 1:]
print('-',*38)
for every 10,000 rows in test_x
test_ y = clf.predict(value)
print(".")
add the values to an array then do the next 10,000 rows
对于我想要预测值的每 10,000 行,将预测值添加到某处,然后执行接下来的 10,000 行。每当我同时处理所有 880,000 行时,我的计算机就会死机。我希望通过一次执行 10,000 行并使用 print(".") 我会得到一个进度条。我使用 test= test.values
将 test.csv 从 pandas
dataframe
更改为 values
。
我提供了尽可能多的信息,如果您需要更多信息,请告诉我。
使用 pd.DataFrame
,您可以使用新的 DataFrame
迭代 index
和 concat
结果的块。对于 np.array
,使用 np.array_split
.
def chunks(l, n):
""" Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
test_x = pd.DataFrame(test_x)
test_result = pd.DataFrame()
for chunk in chunks(test_x.index, 10000):
test_data = test_x.ix[chunk]
test_result = pd.concat([test_result, pd.DataFrame(clf.predict(test_data))])
我假设您的索引是连续整数...
groups = test_x.groupby(test_x.index // 10000)
groups.apply(clf.predict)
如果索引不是连续整数,则有可能...
groups = test.groupby(test.reset_index().index // 10000)
这是一个完整的例子...
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
train, test = (df[:100], df[100:])
y_train, y_test = (iris.target[:100], iris.target[100:])
clf = RandomForestClassifier()
clf.fit(train, y_train)
groups = test.groupby(test.index // 10)
groups.apply(clf.predict)
输出是 Pandas 系列预测列表...
10 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
11 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
12 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
13 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2018 年,来自 fast.ai 的 fastai 0.7 库有一个 set_rf_samples() 函数,它具有一些特殊功能。如果您登陆此页面,强烈建议您对其进行调查。您可以在 Jeremy Howard 的 YouTube 频道上观看机器学习入门 MOOC 以及实施细节。