We propose a method for generalizing the Ising model in magnetic fields and calculating the partition function (exact solution) for the Ising model of an arbitrary shape. Specifically, the partition function is calculated using matrices that are created automatically based on the structure of the system. By generalizing this method, it becomes possible to calculate the partition function of various crystal systems (network shapes) in magnetic fields when N (scale) is infinite. Furthermore, we also connect this method for finding the solution to the Ising model in magnetic fields to a method for finding the solution to Bayesian networks in information statistical mechanics (applied to data mining, machine learning, and combinatorial optimization).