user3355594 - 1 year ago 67

Python Question

I have a set of 4 scripts I created using Python. They take data from an Excel workbook and plots on a 3 axis graph. They then calculated regression curves (surfaces) for each data set. I want to create a table that writes back to a new sheet in the same Excel workbook. The table would display the parameters calculated by the regression analysis. The table lay out would be:

`Model Parameter A Parameter B`

Sulphide

Mixed

Oxide

Leach

I am a geologist, not a programmer and I am also very new to Python. So please dumb it down for me if you can. I have copied part of the script in below hope it helps.

`'''`

==============================================================================

======================== Sulphide Model Fitting ==============================

'''

#TCu_col = 0 # Variable currently unused

ASCu_col = 1 # Variable that will determine the modeled data

AICu_col = 2 # Variable that will determine the modeled data

res_col = 3 # Recovery on "Y" Axis

ASCu = sulphideData[:,ASCu_col]

AICu = sulphideData[:,AICu_col]

res = sulphideData[:,res_col]

def residual(params, x1, x2, res_col):

a = params['a'].value

b = params['b'].value

#=============================================================================

model = ((a*(ASCu / (ASCu + AICu))) + (b*(AICu / (ASCu + AICu))))

#=============================================================================

return (res-model)

params = Parameters()

params.add('a', value=1)

params.add('b', value=1)

out = minimize(residual, params, args=(ASCu_col, AICu_col, res_col))

a = np.round(params['a'].value) # This is parameter 'a' i want reported in the excel table

b = np.round(params['b'].value) # This is parameter 'b' i want reported in the excel table

print(a,b)

x1 = np.linspace(min(sulphideData[:,ASCu_col]),max(sulphideData[:,ASCu_col]),100)

x2 = np.linspace(min(sulphideData[:,AICu_col]),max(sulphideData[:,AICu_col]),100)

X = x1

Y = x2

X,Y = np.meshgrid(X,Y)

Answer Source

You wrote that you are taking data from an excel workbook, but you have provided no code for that. So I am not sure, how you do that.

I would recommend http://www.python-excel.org/ for reading from and writing to excel. You find the the docs for writing into excel here: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html?p=4966. It is quite easy and straightforward, just have a look at it.

Just be aware that this site is for helping each other with knowledge and not by doing each others work. So I would not expect somebody to give you the finished code base, especially not with the limited information you provided.