parameters: (self, mat, start_iteration, num_iteration, predict_type)
docstring: Predict for a 2-D numpy matrix.
docstring_summary: Predict for a 2-D numpy matrix.
docstring_tokens: Predict for a 2 - D numpy matrix .
function: def __pred_for_np2d(self, mat, start_iteration, num_iteration, predict_type):
          """Predict for a 2-D numpy matrix."""
          if len(mat.shape) != 2:
              raise ValueError('Input numpy.ndarray or list must be 2 dimensional')
  
          def inner_predict(mat, start_iteration, num_iteration, predict_type, preds=None):
              if mat.dtype == np.float32 or mat.dtype == np.float64:
                  data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False)
              else:  # change non-float data to float data, need to copy
                  data = np.array(mat.reshape(mat.size), dtype=np.float32)
              ptr_data, type_ptr_data, _ = c_float_array(data)
              n_preds = self.__get_num_preds(start_iteration, num_iteration, mat.shape[0], predict_type)
              if preds is None:
                  preds = np.empty(n_preds, dtype=np.float64)
              elif len(preds.shape) != 1 or len(preds) != n_preds:
                  raise ValueError("Wrong length of pre-allocated predict array")
              out_num_preds = ctypes.c_int64(0)
              _safe_call(_LIB.LGBM_BoosterPredictForMat(
                  self.handle,
                  ptr_data,
                  ctypes.c_int(type_ptr_data),
                  ctypes.c_int32(mat.shape[0]),
                  ctypes.c_int32(mat.shape[1]),
                  ctypes.c_int(C_API_IS_ROW_MAJOR),
                  ctypes.c_int(predict_type),
                  ctypes.c_int(start_iteration),
                  ctypes.c_int(num_iteration),
                  c_str(self.pred_parameter),
                  ctypes.byref(out_num_preds),
                  preds.ctypes.data_as(ctypes.POINTER(ctypes.c_double))))
              if n_preds != out_num_preds.value:
                  raise ValueError("Wrong length for predict results")
              return preds, mat.shape[0]
  
          nrow = mat.shape[0]
          if nrow > MAX_INT32:
              sections = np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32)
              # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal
              n_preds = [self.__get_num_preds(start_iteration, num_iteration, i, predict_type) for i in np.diff([0] + list(sections) + [nrow])]
              n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum()
              preds = np.empty(sum(n_preds), dtype=np.float64)
              for chunk, (start_idx_pred, end_idx_pred) in zip(np.array_split(mat, sections),
                                                               zip(n_preds_sections, n_preds_sections[1:])):
                  # avoid memory consumption by arrays concatenation operations
                  inner_predict(chunk, start_iteration, num_iteration, predict_type, preds[start_idx_pred:end_idx_pred])
              return preds, nrow
          else:
              return inner_predict(mat, start_iteration, num_iteration, predict_type)
function_tokens: def __pred_for_np2d ( self , mat , start_iteration , num_iteration , predict_type ) : if len ( mat . shape ) != 2 : raise ValueError ( 'Input numpy.ndarray or list must be 2 dimensional' ) def inner_predict ( mat , start_iteration , num_iteration , predict_type , preds = None ) : if mat . dtype == np . float32 or mat . dtype == np . float64 : data = np . array ( mat . reshape ( mat . size ) , dtype = mat . dtype , copy = False ) else : # change non-float data to float data, need to copy data = np . array ( mat . reshape ( mat . size ) , dtype = np . float32 ) ptr_data , type_ptr_data , _ = c_float_array ( data ) n_preds = self . __get_num_preds ( start_iteration , num_iteration , mat . shape [ 0 ] , predict_type ) if preds is None : preds = np . empty ( n_preds , dtype = np . float64 ) elif len ( preds . shape ) != 1 or len ( preds ) != n_preds : raise ValueError ( "Wrong length of pre-allocated predict array" ) out_num_preds = ctypes . c_int64 ( 0 ) _safe_call ( _LIB . LGBM_BoosterPredictForMat ( self . handle , ptr_data , ctypes . c_int ( type_ptr_data ) , ctypes . c_int32 ( mat . shape [ 0 ] ) , ctypes . c_int32 ( mat . shape [ 1 ] ) , ctypes . c_int ( C_API_IS_ROW_MAJOR ) , ctypes . c_int ( predict_type ) , ctypes . c_int ( start_iteration ) , ctypes . c_int ( num_iteration ) , c_str ( self . pred_parameter ) , ctypes . byref ( out_num_preds ) , preds . ctypes . data_as ( ctypes . POINTER ( ctypes . c_double ) ) ) ) if n_preds != out_num_preds . value : raise ValueError ( "Wrong length for predict results" ) return preds , mat . shape [ 0 ] nrow = mat . shape [ 0 ] if nrow > MAX_INT32 : sections = np . arange ( start = MAX_INT32 , stop = nrow , step = MAX_INT32 ) # __get_num_preds() cannot work with nrow > MAX_INT32, so calculate overall number of predictions piecemeal n_preds = [ self . __get_num_preds ( start_iteration , num_iteration , i , predict_type ) for i in np . diff ( [ 0 ] + list ( sections ) + [ nrow ] ) ] n_preds_sections = np . array ( [ 0 ] + n_preds , dtype = np . intp ) . cumsum ( ) preds = np . empty ( sum ( n_preds ) , dtype = np . float64 ) for chunk , ( start_idx_pred , end_idx_pred ) in zip ( np . array_split ( mat , sections ) , zip ( n_preds_sections , n_preds_sections [ 1 : ] ) ) : # avoid memory consumption by arrays concatenation operations inner_predict ( chunk , start_iteration , num_iteration , predict_type , preds [ start_idx_pred : end_idx_pred ] ) return preds , nrow else : return inner_predict ( mat , start_iteration , num_iteration , predict_type )