Hướng dẫn minimize chi-square python

I am trying to minimize a Chi_Squared function.

Following Python - Minimizing Chi-squared

yields no results since a lot has changed since then. Code:

import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt

filename = 'exportklee.csv'
data = np.genfromtxt('exportklee.csv', dtype=float, delimiter=',')

magX = data[:, 3] #* 0.080
magY = data[:, 4] #* 0.080
magZ = data[:, 5] #* 0.080
f1=magX
f2=magY
f3=magZ
T=np.array([magX,magY,magZ])
B=np.linalg.norm(T,axis=0)
mittel=B.mean()
target=np.ones_like(B)*mittel
pi=np.pi

def sind(alpha): 
    return np.sin(np.deg2rad(alpha))

def cosd(alpha): 
    return np.cos(np.deg2rad(alpha))


def chisqfunc(x):


model=(((f1 - x(0))*((f1 - x(0))/x(6) + (sind((pi*x(3))/180)*((f2 - x(1))/(x(7)*cosd((pi*x(3))/180)) + (sind((pi*x(3))/180)*(f1 - x(0)))/(x(6)*cosd((pi*x(3))/180))))/cosd((pi*x(3))/180) + ((cosd((pi*x(3))/180)*sind((pi*x(4))/180) + sind((pi*x(3))/180)*sind((pi*x(5))/180))*(((cosd((pi*x(3))/180)*sind((pi*x(4))/180) + sind((pi*x(3))/180)*sind((pi*x(5))/180))*(f1 - x(0)))/(x(6)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) - (f3 - x(2))/(x(8)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) + (sind((pi*x(5))/180)*(f2 - x(1)))/(x(7)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2))))/(cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2))))/x(6) + ((((f2 - x(1))/(x(7)*cosd((pi*x(3))/180)) + (sind((pi*x(3))/180)*(f1 - x(0)))/(x(6)*cosd((pi*x(3))/180)))/cosd((pi*x(3))/180) + (sind((pi*x(5))/180)*(((cosd((pi*x(3))/180)*sind((pi*x(4))/180) + sind((pi*x(3))/180)*sind((pi*x(5))/180))*(f1 - x(0)))/(x(6)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) - (f3 - x(2))/(x(8)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) + (sind((pi*x(5))/180)*(f2 - x(1)))/(x(7)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2))))/(cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)))*(f2 - x(1)))/x(7) - ((f3 - x(2))*(((cosd((pi*x(3))/180)*sind((pi*x(4))/180) + sind((pi*x(3))/180)*sind((pi*x(5))/180))*(f1 - x(0)))/(x(6)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) - (f3 - x(2))/(x(8)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)) + (sind((pi*x(5))/180)*(f2 - x(1)))/(x(7)*cosd((pi*x(3))/180)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2))))/(x(8)*(- sind((x(4)*pi)/180)**2 - sind((x(5)*pi)/180)**2 + 1)**(1/2)))**(1/2)
  chisq=np.sum(((target - model))**2)
    return chisq

x0 = np.array([1,1,1,0.1,0.1,0.1,22,22,22])

modelv=opt.minimize(chisqfunc, x0)

This old code doesn't work in 3.7 anymore.

Now the error that this yields is

'numpy.ndarray' object is not callable

I am pretty sure that this is due to me throwing the data(f1,f2,f3,target) into the function as a global turns it into a np_array which then can not be accessed by the minimize library.

The question is, how else do I put my data in there?