I used the code below to build train and test matrices so as to use them in my NN model.
from scipy.sparse import csr_matrix
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv('data.csv', names=['x', 'y', 'z'])
x = df.x.unique().shape[0]
y = df.y.unique().shape[0]
train_data, test_data = train_test_split(df, test_size=0.2)
train_data = pd.DataFrame(train_data)
test_data = pd.DataFrame(test_data)
#Build train matrix
train_x = []
train_y = []
train_z = []
for line in train_data.itertuples():
u = line[1] - 1
i = line[2] - 1
train_x.append(u)
train_y.append(i)
train_z.append(line[3])
train_matrix = csr_matrix((train_z, (train_x, train_y)), shape=(x, y))
#Build test matrix
test_x = []
test_y = []
test_z = []
for line in test_data.itertuples():
test_x.append(line[1] - 1)
test_y.append(line[2] - 1)
test_z.append(line[3])
test_matrix = csr_matrix((test_z, (test_x, test_y)), shape=(x, y))
When I work with small datasets, it works perfectly. However, when I use it to handle a little bit larger datasets (600 MB), it doesn't work. It rather shows me this error:
File "C:\Users\Mus\Anaconda3\lib\site-packages\scipy\sparse\compressed.py", line 51, in __init__
other = self.__class__(coo_matrix(arg1, shape=shape))
File "C:\Users\Mus\Anaconda3\lib\site-packages\scipy\sparse\coo.py", line 192, in __init__
self._check()
File "C:\Users\Mus\Anaconda3\lib\site-packages\scipy\sparse\coo.py", line 272, in _check
raise ValueError('row index exceeds matrix dimensions')
ValueError: row index exceeds matrix dimensions
When I tried the code below it showed me another error in the same line:
train_data, test_data = train_test_split(csr_matrix(df[z].values, (df[x].values, df[y].values)), test_size=0.2)
File "C:\Users\Mus\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:\Users\Mus\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\Mus\Anaconda3\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:\Users\Mus\Anaconda3\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\Users\Mus\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 5
I appreciate your help
This code proposed by @CJR replaces all the code of building the train and test matrices
train_matrix, test_matrix = train_test_split(csr_matrix((df['z'].values, (df['x'].values, df['y'].values))), test_size=0.2)