You could "linearise" an electromagnet with distance sensors and code, but that's a bit of a Franken-Solution - not least because any delay in the closed loop could make the system unstable.
In this case a couple of minutes of research - or maybe asking a physicist - would have saved a lot of essentially rather pointless tinkering.
I mean - if you enjoy tinkering more than you enjoy producing an elegant solution, then tinker away. But this is noodling around for the sake of it, not solid engineering.
In real life it should be pretty close to linear, but it would be worth checking as I could imagine there being non-linearity or time dependence of a spring under what is probably a decent amount of load being applied quickly and repeatedly.