parameters: (self, csr, start_iteration, num_iteration, predict_type)
docstring: Predict for a CSR data.
docstring_summary: Predict for a CSR data.
docstring_tokens: Predict for a CSR data .
function: def __pred_for_csr(self, csr, start_iteration, num_iteration, predict_type):
          """Predict for a CSR data."""
          def inner_predict(csr, start_iteration, num_iteration, predict_type, preds=None):
              nrow = len(csr.indptr) - 1
              n_preds = self.__get_num_preds(start_iteration, num_iteration, nrow, 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)
  
              ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr)
              ptr_data, type_ptr_data, _ = c_float_array(csr.data)
  
              assert csr.shape[1] <= MAX_INT32
              csr_indices = csr.indices.astype(np.int32, copy=False)
  
              _safe_call(_LIB.LGBM_BoosterPredictForCSR(
                  self.handle,
                  ptr_indptr,
                  ctypes.c_int(type_ptr_indptr),
                  csr_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
                  ptr_data,
                  ctypes.c_int(type_ptr_data),
                  ctypes.c_int64(len(csr.indptr)),
                  ctypes.c_int64(len(csr.data)),
                  ctypes.c_int64(csr.shape[1]),
                  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, nrow
  
          def inner_predict_sparse(csr, start_iteration, num_iteration, predict_type):
              ptr_indptr, type_ptr_indptr, __ = c_int_array(csr.indptr)
              ptr_data, type_ptr_data, _ = c_float_array(csr.data)
              csr_indices = csr.indices.astype(np.int32, copy=False)
              matrix_type = C_API_MATRIX_TYPE_CSR
              if type_ptr_indptr == C_API_DTYPE_INT32:
                  out_ptr_indptr = ctypes.POINTER(ctypes.c_int32)()
              else:
                  out_ptr_indptr = ctypes.POINTER(ctypes.c_int64)()
              out_ptr_indices = ctypes.POINTER(ctypes.c_int32)()
              if type_ptr_data == C_API_DTYPE_FLOAT32:
                  out_ptr_data = ctypes.POINTER(ctypes.c_float)()
              else:
                  out_ptr_data = ctypes.POINTER(ctypes.c_double)()
              out_shape = np.empty(2, dtype=np.int64)
              _safe_call(_LIB.LGBM_BoosterPredictSparseOutput(
                  self.handle,
                  ptr_indptr,
                  ctypes.c_int(type_ptr_indptr),
                  csr_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)),
                  ptr_data,
                  ctypes.c_int(type_ptr_data),
                  ctypes.c_int64(len(csr.indptr)),
                  ctypes.c_int64(len(csr.data)),
                  ctypes.c_int64(csr.shape[1]),
                  ctypes.c_int(predict_type),
                  ctypes.c_int(start_iteration),
                  ctypes.c_int(num_iteration),
                  c_str(self.pred_parameter),
                  ctypes.c_int(matrix_type),
                  out_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)),
                  ctypes.byref(out_ptr_indptr),
                  ctypes.byref(out_ptr_indices),
                  ctypes.byref(out_ptr_data)))
              matrices = self.__create_sparse_native(csr, out_shape, out_ptr_indptr, out_ptr_indices, out_ptr_data,
                                                     type_ptr_indptr, type_ptr_data, is_csr=True)
              nrow = len(csr.indptr) - 1
              return matrices, nrow
  
          if predict_type == C_API_PREDICT_CONTRIB:
              return inner_predict_sparse(csr, start_iteration, num_iteration, predict_type)
          nrow = len(csr.indptr) - 1
          if nrow > MAX_INT32:
              sections = [0] + list(np.arange(start=MAX_INT32, stop=nrow, step=MAX_INT32)) + [nrow]
              # __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(sections)]
              n_preds_sections = np.array([0] + n_preds, dtype=np.intp).cumsum()
              preds = np.empty(sum(n_preds), dtype=np.float64)
              for (start_idx, end_idx), (start_idx_pred, end_idx_pred) in zip(zip(sections, sections[1:]),
                                                                              zip(n_preds_sections, n_preds_sections[1:])):
                  # avoid memory consumption by arrays concatenation operations
                  inner_predict(csr[start_idx:end_idx], start_iteration, num_iteration, predict_type, preds[start_idx_pred:end_idx_pred])
              return preds, nrow
          else:
              return inner_predict(csr, start_iteration, num_iteration, predict_type)
function_tokens: def __pred_for_csr ( self , csr , start_iteration , num_iteration , predict_type ) : def inner_predict ( csr , start_iteration , num_iteration , predict_type , preds = None ) : nrow = len ( csr . indptr ) - 1 n_preds = self . __get_num_preds ( start_iteration , num_iteration , nrow , 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 ) ptr_indptr , type_ptr_indptr , __ = c_int_array ( csr . indptr ) ptr_data , type_ptr_data , _ = c_float_array ( csr . data ) assert csr . shape [ 1 ] <= MAX_INT32 csr_indices = csr . indices . astype ( np . int32 , copy = False ) _safe_call ( _LIB . LGBM_BoosterPredictForCSR ( self . handle , ptr_indptr , ctypes . c_int ( type_ptr_indptr ) , csr_indices . ctypes . data_as ( ctypes . POINTER ( ctypes . c_int32 ) ) , ptr_data , ctypes . c_int ( type_ptr_data ) , ctypes . c_int64 ( len ( csr . indptr ) ) , ctypes . c_int64 ( len ( csr . data ) ) , ctypes . c_int64 ( csr . shape [ 1 ] ) , 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 , nrow def inner_predict_sparse ( csr , start_iteration , num_iteration , predict_type ) : ptr_indptr , type_ptr_indptr , __ = c_int_array ( csr . indptr ) ptr_data , type_ptr_data , _ = c_float_array ( csr . data ) csr_indices = csr . indices . astype ( np . int32 , copy = False ) matrix_type = C_API_MATRIX_TYPE_CSR if type_ptr_indptr == C_API_DTYPE_INT32 : out_ptr_indptr = ctypes . POINTER ( ctypes . c_int32 ) ( ) else : out_ptr_indptr = ctypes . POINTER ( ctypes . c_int64 ) ( ) out_ptr_indices = ctypes . POINTER ( ctypes . c_int32 ) ( ) if type_ptr_data == C_API_DTYPE_FLOAT32 : out_ptr_data = ctypes . POINTER ( ctypes . c_float ) ( ) else : out_ptr_data = ctypes . POINTER ( ctypes . c_double ) ( ) out_shape = np . empty ( 2 , dtype = np . int64 ) _safe_call ( _LIB . LGBM_BoosterPredictSparseOutput ( self . handle , ptr_indptr , ctypes . c_int ( type_ptr_indptr ) , csr_indices . ctypes . data_as ( ctypes . POINTER ( ctypes . c_int32 ) ) , ptr_data , ctypes . c_int ( type_ptr_data ) , ctypes . c_int64 ( len ( csr . indptr ) ) , ctypes . c_int64 ( len ( csr . data ) ) , ctypes . c_int64 ( csr . shape [ 1 ] ) , ctypes . c_int ( predict_type ) , ctypes . c_int ( start_iteration ) , ctypes . c_int ( num_iteration ) , c_str ( self . pred_parameter ) , ctypes . c_int ( matrix_type ) , out_shape . ctypes . data_as ( ctypes . POINTER ( ctypes . c_int64 ) ) , ctypes . byref ( out_ptr_indptr ) , ctypes . byref ( out_ptr_indices ) , ctypes . byref ( out_ptr_data ) ) ) matrices = self . __create_sparse_native ( csr , out_shape , out_ptr_indptr , out_ptr_indices , out_ptr_data , type_ptr_indptr , type_ptr_data , is_csr = True ) nrow = len ( csr . indptr ) - 1 return matrices , nrow if predict_type == C_API_PREDICT_CONTRIB : return inner_predict_sparse ( csr , start_iteration , num_iteration , predict_type ) nrow = len ( csr . indptr ) - 1 if nrow > MAX_INT32 : sections = [ 0 ] + list ( np . arange ( start = MAX_INT32 , stop = nrow , step = MAX_INT32 ) ) + [ nrow ] # __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 ( sections ) ] n_preds_sections = np . array ( [ 0 ] + n_preds , dtype = np . intp ) . cumsum ( ) preds = np . empty ( sum ( n_preds ) , dtype = np . float64 ) for ( start_idx , end_idx ) , ( start_idx_pred , end_idx_pred ) in zip ( zip ( sections , sections [ 1 : ] ) , zip ( n_preds_sections , n_preds_sections [ 1 : ] ) ) : # avoid memory consumption by arrays concatenation operations inner_predict ( csr [ start_idx : end_idx ] , start_iteration , num_iteration , predict_type , preds [ start_idx_pred : end_idx_pred ] ) return preds , nrow else : return inner_predict ( csr , start_iteration , num_iteration , predict_type )