Fixed a cross-platform endian issue
diff --git a/doc/whats_new/v0.24.rst b/doc/whats_new/v0.24.rst
index b67004f1e8b00..335a7499ed21e 100644
--- a/doc/whats_new/v0.24.rst
+++ b/doc/whats_new/v0.24.rst
@@ -367,6 +367,10 @@ Changelog
   :meth:`tree.DecisionTreeRegressor.fit`, and has not effect.
   :pr:`17614` by :user:`Juan Carlos Alfaro Jiménez <alfaro96>`.
 
+- |Fix| Allow serialized tree based models to be unpickled on a machine
+  with different endianness.
+  :pr:`17644` by :user:`Qi Zhang <qzhang90>`.
+
 Code and Documentation Contributors
 -----------------------------------
 
diff --git a/sklearn/tree/_tree.pyx b/sklearn/tree/_tree.pyx
index 9d6f02bd16103..f4484ab1a3314 100644
--- a/sklearn/tree/_tree.pyx
+++ b/sklearn/tree/_tree.pyx
@@ -652,8 +652,15 @@ cdef class Tree:
 
         value_shape = (node_ndarray.shape[0], self.n_outputs,
                        self.max_n_classes)
+
+        if (node_ndarray.dtype != NODE_DTYPE):
+            # possible mismatch of big/little endian due to serialization
+            # on a different architecture. Try swapping the byte order.  
+            node_ndarray = node_ndarray.byteswap().newbyteorder()
+            if (node_ndarray.dtype != NODE_DTYPE):
+                raise ValueError('Did not recognise loaded array dytpe')
+
         if (node_ndarray.ndim != 1 or
-                node_ndarray.dtype != NODE_DTYPE or
                 not node_ndarray.flags.c_contiguous or
                 value_ndarray.shape != value_shape or
                 not value_ndarray.flags.c_contiguous or

This PR is to fix a problem that I encountered when loading a GradientBoostingClassifier/GradientBoostingRegressor model, which was trained on a little-endian machine, on a big-endian machine. The error occurred at https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx#L671

The reason was that the loaded `node_ndarray` was in little-endian byte order while the machine was expecting a big-endian one.

The fix in the PR is, before throwing out an error, try to swap the byte order of the `node_ndarray` and see whether it can satisfy the `dtype` checking.  