Python 如何在块中迭代两个数据帧
对于机器学习任务,我需要处理太大的数据集,这些数据集无法一次全部放入我的内存中,因此我需要将其分解为块。幸运的是,pandas.read_csv有一个参数chunk_size,在该参数中,您可以指定要用于分析的数据量,然后使用for循环分块循环数据集,如下所示:Python 如何在块中迭代两个数据帧,python,pandas,Python,Pandas,对于机器学习任务,我需要处理太大的数据集,这些数据集无法一次全部放入我的内存中,因此我需要将其分解为块。幸运的是,pandas.read_csv有一个参数chunk_size,在该参数中,您可以指定要用于分析的数据量,然后使用for循环分块循环数据集,如下所示: #This example can be found at http://pandas.pydata.org/pandas-docs/dev/io.html In [120]: reader = pd.read_table('tmp.
#This example can be found at http://pandas.pydata.org/pandas-docs/dev/io.html
In [120]: reader = pd.read_table('tmp.sv', sep='|', chunksize=4)
In [121]: reader
<pandas.io.parsers.TextFileReader at 0xaa94ad0>
In [122]: for chunk in reader:
.....: print(chunk)
.....:
Unnamed: 0 0 1 2 3
0 0 0.469112 -0.282863 -1.509059 -1.135632
1 1 1.212112 -0.173215 0.119209 -1.044236
2 2 -0.861849 -2.104569 -0.494929 1.071804
3 3 0.721555 -0.706771 -1.039575 0.271860
[4 rows x 5 columns]
Unnamed: 0 0 1 2 3
0 4 -0.424972 0.567020 0.276232 -1.087401
1 5 -0.673690 0.113648 -1.478427 0.524988
2 6 0.404705 0.577046 -1.715002 -1.039268
3 7 -0.370647 -1.157892 -1.344312 0.844885
[4 rows x 5 columns]
Unnamed: 0 0 1 2 3
0 8 1.075770 -0.10905 1.643563 -1.469388
1 9 0.357021 -0.67460 -1.776904 -0.968914
[2 rows x 5 columns].
更新:
所以我尝试了海登的任何解决方案,但当我尝试访问数据的特定部分时,它给了我一个新的错误:
print("getting train set")
train = pd.read_csv(os.path.join(dir,"Train.csv"),chunksize = 200000)
print("getting test set")
test = pd.read_csv(os.path.join(dir,"Test.csv"),chunksize = 200000)
result = []
for chunk in train:
print("transforming train,test,labels into numpy arrays")
labels = np.array(train)[:,3]
train = np.array(train)[:,2]
test = np.array(test)[:,2]
print("getting estimator and predictions")
result.append(stochastic_gradient(train,test))
print("got everything")
result = np.array(result)
回溯:
Traceback (most recent call last):
File "C:\Users\Ano\workspace\final_submission\src\rf.py", line 38, in <module>
main()
File "C:\Users\Ano\workspace\final_submission\src\rf.py", line 18, in main
labels = np.array(train)[:,3]
IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index
回溯(最近一次呼叫最后一次):
文件“C:\Users\Ano\workspace\final\u submission\src\rf.py”,第38行,在
main()
文件“C:\Users\Ano\workspace\final\u submission\src\rf.py”,第18行,主目录
标签=np.数组(列)[:,3]
索引器错误:0-d数组只能使用单个()或新轴列表(以及单个…)作为索引
在for循环中,您可以访问当前范围内的变量:
In [11]: a = [1, 2, 3]
In [12]: b = 4
In [13]: for L in a: # no need to "and b"
print L, b
1 4
2 4
3 4
小心,这意味着在for循环中赋值会覆盖变量:
In [14]: for b in a:
print b
1
2
3
In [15]: b
Out[15]: 3
要同时遍历两个iTerable,请使用zip:
In [21]: c = [4, 5, 6]
In [22]: zip(a, c)
Out[22]: [(1, 4), (2, 5), (3, 6)]
在Python2中,这是一个在内存中计算的列表(在Python3中不是这样)。你可以使用izip,它是迭代器的帮凶
In [23]: from itertools import izip # in python 3, just use zip
In [24]: for La, Lc in izip(a, c):
print La, Lb
1 4
2 5
3 6
只要去掉“and test”就行了?@Learner你的意思是在块上而不是火车上调用数组。更好的名字可能有助于避免这个错误(例如train_chunker)。我有点困惑。如果我调用块,我如何区分火车和测试集?你能给我看一个小的代码示例吗?@Learner目前,你正在调用array(train),train是一个chunker(而不是chunk)。我的建议是对train\u chunker中的train\u chunk使用
,…
而不是对train中的chunk使用,
Ok,但我仍然无法获得我需要的测试集的块。有什么方法可以让我同时获得列车和测试集的片段,还是我必须在记忆中完整地加载其中一个集?@Learner哦,我明白了!您希望同时遍历这些对象。我想您应该使用zip(来自itertools import izip
):对于训练块,在izip中测试块(训练块,测试块):…
In [23]: from itertools import izip # in python 3, just use zip
In [24]: for La, Lc in izip(a, c):
print La, Lb
1 4
2 5
3 6