code: def write_data_to_xlearn_format(X, y, filepath, fields=None):
      """Write data to xlearn format (libsvm or libffm). Modified from
      https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/datasets/svmlight_format.py
  
      :param X: array-like
                Feature matrix in numpy or sparse format
      :param y: array-like
                Label in numpy or sparse format
      :param filepath: file location for writing data to
      :param fields: An array specifying fields in each columns of X. It should have same length
          as the number of columns in X. When set to None, convert data to libsvm format else
          libffm format.
      """
  
      with open(filepath, "wb") as f_handle:
          X_is_sp = int(hasattr(X, "tocsr"))
          y_is_sp = int(hasattr(y, "tocsr"))
  
          if X.dtype.kind == "i":
              value_pattern = "%d:%d"
          else:
              value_pattern = "%d:%.16g"
  
          if fields is not None:
              is_ffm_format = True
              value_pattern = "%d:" + value_pattern
          else:
              is_ffm_format = False
  
          if y.dtype.kind == "i":
              label_pattern = "%d"
          else:
              label_pattern = "%.16g"
  
          line_pattern = "%s %s\n"
  
          for i in range(X.shape[0]):
              if X_is_sp:
                  span = slice(X.indptr[i], X.indptr[i + 1])
                  x_indices = X.indices[span]
                  row = (
                      zip(fields[x_indices], x_indices, X.data[span])
                      if is_ffm_format
                      else zip(x_indices, X.data[span])
                  )
              else:
                  nz = X[i] != 0
                  row = (
                      zip(fields[nz], np.where(nz)[0], X[i, nz])
                      if is_ffm_format
                      else zip(np.where(nz)[0], X[i, nz])
                  )
  
              if is_ffm_format:
                  s = " ".join(
                      value_pattern % (f, j, x) for f, j, x in row
                  )
              else:
                  s = " ".join(value_pattern % (j, x) for j, x in row)
  
              if y_is_sp:
                  labels_str = label_pattern % y.data[i]
              else:
                  labels_str = label_pattern % y[i]
  
              f_handle.write(
                  (line_pattern % (labels_str, s)).encode("ascii")
              )
  
imports: import os
  import shutil
  import tempfile
  import warnings
  import numpy
  from .xlearn import create_linear, create_fm, create_ffm
  from .data import DMatrix
  from sklearn.base import BaseEstimator
  from sklearn.utils import check_array, check_X_y
label_desc: Use image node_id 1 for calling a global function with example usage: write_data_to_xlearn_format(X, y, filepath, fields) without return types