Memory Error while fitting the RandomForestClassifier model

Could you please anyone help me to clear the below “error”


MemoryError Traceback (most recent call last)
in

~\Anaconda3\lib\site-packages\sklearn\ensemble_forest.py in fit(self, X, y, sample_weight)
440 # parallel_backend contexts set at a higher level,
441 # since correctness does not rely on using threads.
→ 442 trees = Parallel(
443 n_jobs=self.n_jobs,
444 verbose=self.verbose,

~\Anaconda3\lib\site-packages\joblib\parallel.py in call(self, iterable)
1052
1053 with self._backend.retrieval_context():
→ 1054 self.retrieve()
1055 # Make sure that we get a last message telling us we are done
1056 elapsed_time = time.time() - self._start_time

~\Anaconda3\lib\site-packages\joblib\parallel.py in retrieve(self)
931 try:
932 if getattr(self._backend, ‘supports_timeout’, False):
→ 933 self._output.extend(job.get(timeout=self.timeout))
934 else:
935 self._output.extend(job.get())

~\Anaconda3\lib\multiprocessing\pool.py in get(self, timeout)
769 return self._value
770 else:
→ 771 raise self._value
772
773 def _set(self, i, obj):

~\Anaconda3\lib\multiprocessing\pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
123 job, i, func, args, kwds = task
124 try:
→ 125 result = (True, func(*args, **kwds))
126 except Exception as e:
127 if wrap_exception and func is not _helper_reraises_exception:

~\Anaconda3\lib\site-packages\joblib_parallel_backends.py in call(self, *args, **kwargs)
593 def call(self, *args, **kwargs):
594 try:
→ 595 return self.func(*args, **kwargs)
596 except KeyboardInterrupt as e:
597 # We capture the KeyboardInterrupt and reraise it as

~\Anaconda3\lib\site-packages\joblib\parallel.py in call(self)
260 # change the default number of processes to -1
261 with parallel_backend(self._backend, n_jobs=self._n_jobs):
→ 262 return [func(*args, **kwargs)
263 for func, args, kwargs in self.items]
264

~\Anaconda3\lib\site-packages\joblib\parallel.py in (.0)
260 # change the default number of processes to -1
261 with parallel_backend(self._backend, n_jobs=self._n_jobs):
→ 262 return [func(*args, **kwargs)
263 for func, args, kwargs in self.items]
264

~\Anaconda3\lib\site-packages\sklearn\utils\fixes.py in call(self, *args, **kwargs)
209 def call(self, *args, **kwargs):
210 with config_context(**self.config):
→ 211 return self.function(*args, **kwargs)
212
213

~\Anaconda3\lib\site-packages\sklearn\ensemble_forest.py in _parallel_build_trees(tree, forest, X, y, sample_weight, tree_idx, n_trees, verbose, class_weight, n_samples_bootstrap)
173 tree.random_state, n_samples, n_samples_bootstrap
174 )
→ 175 sample_counts = np.bincount(indices, minlength=n_samples)
176 curr_sample_weight *= sample_counts
177

<array_function internals> in bincount(*args, **kwargs)

MemoryError: Unable to allocate 313. MiB for an array with shape (41009562,) and data type int64

If you’re using binder, use Google Colab instead, I think that should solve the memory issue.
If this error is happening on Colab, try lowering the number of n_estimators/max_depth of the random forest.

I had tried with all possible least hyperparameters, but the error is still arising. Please note my train dataset has 2.9million rows. I came to know that the error is arising due to size of training dataset. If the dataset as like above, how to fix the problem. Please see my system specification below.

HP laptop
AMD Ryzen-7 5700U with RadeonGraphics
8GB ram
64bit