Example #1
0
def main():
    x = np.arange(1, 6)
    y = np.array([2470., 2510., 2410., 2350., 2240.])

    # Degree of curve fit for plotting
    deg = np.arange(1, 5)
    alpha = 1.0
    N = len(x)
    sigma = alpha * np.ones(N)

    yy = np.empty((len(deg), N))
    a_fit_all = {}
    y_pred = np.zeros(len(deg))

    for M in deg:
        if M == 1:
            [a_fit, sig_a, yy[M - 1, :], chisqr] = linreg(x, y, sigma)
        else:
            [a_fit, sig_a, yy[M - 1, :], chisqr] = pollsf(x, y, sigma, M + 1)
        a_fit_all.update({M: a_fit})
        for i in range(M + 1):
            y_pred[M - 1] += a_fit[i] * 6**i

    print(f'y_pred: {y_pred}')
    print(f'a_fit: {a_fit_all}')
    plt.plot(x, y, 'g*', label='Data')
    for i in range(len(deg)):
        plt.plot(x, yy[i, :], label=f'LSF line of degree {i+1}')
        plt.plot(6, y_pred[i], '*', label=f'Day 6 prediction, degree {i+1}')
    plt.legend()
    plt.show()
Example #2
0
    def findoffBB(self, t, plane, MADTwiss, betalist):

        print "METHOD => Starting to calculate off-momentum beta-beat"

        self.bpms = intersect(t)
        self.bpms = modelIntersect(self.bpms, MADTwiss)
        MADTwiss.chrombeat()

        self.slope = {}
        self.slopeE = {}
        self.slopeM = {}

        dpplist = []

        for i in range(len(betalist)):  #### for sorting

            dpplist.append(betalist[i]['DPP'])

        dpplist.sort()

        #if dpplist[0]>dpplist[1]
        #dpplist.reverse()

        for i in range(len(self.bpms)):

            bn = upper(self.bpms[i][1])
            check = 0
            slopei = 0.0
            slopeiM = 0.0
            beta = []
            k = 0

            for k in range(len(dpplist)):
                count = 0
                for j in range(len(betalist)):

                    if dpplist[k] == betalist[j]['DPP']:

                        if count == 0:

                            beta.append(betalist[j][bn][0])
                        count = 1

            [slope, offset, R, slope_error,
             offset_error] = linreg.linreg(dpplist, beta)

            a = FIT(dpplist, beta, 1, 1e-15)
            #print ' list '+str(dpplist)
            coef, error = a.linreg()

            if plane == 'H':
                slopeiM = MADTwiss.dbx[MADTwiss.indx[upper(bn)]]
            else:
                slopeiM = MADTwiss.dby[MADTwiss.indx[upper(bn)]]

            #self.slope[bn]=coef[0]
            self.slope[bn] = coef[0] / 100
            #self.slopeE[bn]=error[0]
            self.slopeE[bn] = error[0] / 100
            self.slopeM[bn] = slopeiM
Example #3
0
def residual_calculation(abscissae, ordinates):
    a, b = lin.linreg(abscissae, ordinates)
    k = len(abscissae)
    observed = []
    predicted = []
    residual = []
    for i in range(0, k):
        observed.append(ordinates[i])
        predicted.append(a + b * abscissae[i])
        residual.append(observed[i] - predicted[i])
    return residual
    def findchrom(self,t,plane):

        print "METHOD => Starting to calculate the chromaticity "

        

        # t is collection of twiss files

        if len(t)<2:
            print " Not enough files \n ... I cannot work like this! => sys.exit()"
            sys.exit()


        bpms=intersect(t)

        slopem=0.0
        error=[]
        slopeme=0.0

        self.slope={}
        self.slope_error={}
        self.chrom=0.0
        self.chrome=0.0
        self.bpms=bpms
        
        for x in range(len(bpms)):
             bpm=upper(bpms[x][1])
             chrom=0.0
             chrom_error=0.0
             tune=[]
             dpp=[]

             for i in range(len(t)):

                 if plane=='H':
                     tune.append(t[i].TUNEX[t[i].indx[bpm]])
                     dpp.append(t[i].DPP)
                 else:
                     tune.append(t[i].TUNEY[t[i].indx[bpm]])
                     dpp.append(t[i].DPP)
                     
             
             [slope, offset, R, slope_error, offset_error]=linreg.linreg(dpp,tune)

             slopem+=slope
             slopeme+=slope_error
             self.slope[bpm]=float(slope)
             self.slope_error[bpm]=(float(slope_error))

        self.chrom=slopem/len(bpms)
        self.chrome=slopeme/len(bpms)
Example #5
0
    def findchrom(self, t, plane):

        print "METHOD => Starting to calculate the chromaticity "

        # t is collection of twiss files

        if len(t) < 2:
            print " Not enough files \n ... I cannot work like this! => sys.exit()"
            sys.exit()

        bpms = intersect(t)

        slopem = 0.0
        error = []
        slopeme = 0.0

        self.slope = {}
        self.slope_error = {}
        self.chrom = 0.0
        self.chrome = 0.0
        self.bpms = bpms

        for x in range(len(bpms)):
            bpm = upper(bpms[x][1])
            chrom = 0.0
            chrom_error = 0.0
            tune = []
            dpp = []

            for i in range(len(t)):

                if plane == 'H':
                    tune.append(t[i].TUNEX[t[i].indx[bpm]])
                    dpp.append(t[i].DPP)
                else:
                    tune.append(t[i].TUNEY[t[i].indx[bpm]])
                    dpp.append(t[i].DPP)

            [slope, offset, R, slope_error,
             offset_error] = linreg.linreg(dpp, tune)

            slopem += slope
            slopeme += slope_error
            self.slope[bpm] = float(slope)
            self.slope_error[bpm] = (float(slope_error))

        self.chrom = slopem / len(bpms)
        self.chrome = slopeme / len(bpms)
Example #6
0
def get_f( couplelist, dpplist, bpm_name, value):
    '''
    calculates the linear regression of 'value' for each
    dpp in dpplist

    :param couplelist: list of getcouple files (for each dpp)
    :param dpplist: list of all dpp values available
    :param bpm_name: name of bpm
    :param value: name of column (e.g. F1001R)
    '''
    lst=[]
    x=[]
    for dpp in dpplist:
        x.append(dpp)
        couplefile=couplelist[dpp]
        lst.append( getattr(couplefile, value)[ couplefile.indx[ bpm_name]])
    lreg=linreg.linreg(x,lst)
    return lreg[0],lreg[3]
Example #7
0
def linearRegression():

    if request.method == 'POST':
        # if user presses submit after uploading dataset and target
        if 'file' in request.files and 'target' in request.form:
            file = request.files['file']
            session['filename'] = file.filename
            data = pd.read_csv(file)
            data = cleaner.clean(data)
            session['target'] = request.form['target']
            session['target'] = cleaner.fix_target(session['target'])

        # if user only needs to uplaod target and presses submit
        elif 'get_target' in request.form:

            session['target'] = request.form['get_target']
            session['target'] = cleaner.fix_target(session['target'])
            data = pd.read_pickle(session['filename'])

        # perform linear regression
        linreg_model = linreg.linreg(data, session['target'])
        ans = ''
        eq = g.selected_features + ' * ' + str(linreg_model.coef_[0])

        if np.sign(linreg_model.coef_) > 0:
            ans = '+ ' + eq
        else:
            ans = '- ' + eq

        return render_template('linearreg.html',
                               intercept=linreg_model.intercept_,
                               coef_name=g.selected_features,
                               coef_num=linreg_model.coef_,
                               r_squared=g.r_squared,
                               mae=g.mae,
                               eq=ans)

    # if user needs to uplaod csv and target
    if request.method == 'GET':
        if not os.path.isfile(session['filename']):
            return render_template('linearreg.html')
        else:
            # if user only needs to upload target
            return render_template('linearreg.html', get_target=True)
Example #8
0
def estimate(X, Y, X_CV, X_test, estimator, param, **kwargs):
    """ Handler method to distribute estimate to correct estimation method. """
    sys.path.insert(0, __path_to_source__)
    sys.path.insert(0, __path_to_elm__)
    assert 'estimator_id' in estimator, "estimate_handler: 'estimator_id' not in estimator"
    if estimator['estimator_id'] == 'knn':
        return knn(X, Y, X_CV, X_test, estimator, param, **kwargs)
    elif estimator['estimator_id'] == 'linreg':
        return linreg(X, Y, X_CV, X_test, estimator, param, **kwargs)
    elif estimator['estimator_id'] == 'sirknn':
        return sirknn(X, Y, X_CV, X_test, estimator, param, __path_to_source__,
                      **kwargs)
    elif estimator['estimator_id'] == 'ffnn':
        return ffnn(X, Y, X_CV, X_test, estimator, param, __path_to_source__,
                    **kwargs)
    elif estimator['estimator_id'] == 'isotron':
        return isotron(X, Y, X_CV, X_test, estimator, param,
                       __path_to_source__, **kwargs)
    elif estimator['estimator_id'] == 'elm':
        return elm_regressor(X, Y, X_CV, X_test, estimator, param,
                             __path_to_elm__, **kwargs)
    else:
        raise NotImplementedError("Estimator {0} is not implemented".format(
            estimator.get('estimator_id', 'NONE')))
Example #9
0
def a2b():
	auslauf_messung = loadtxt('../data/auslauf_2b')
	h = auslauf_messung[:,0]
	t = auslauf_messung[:,1]
	sigma_h_Messung = 1
	sigma_h_log = sqrt(sigma_h_Messung**2*(1/h)**2)
	fig1 = plt.figure(1)
	plt.xlabel(r'$t$')
	plt.ylabel(r'$\log{(h)}$')
	plt.grid()
	x = linspace(0,1400)
	plt.errorbar(t,log(h),sigma_h_log, fmt='ko', label="Messung")

	h_list=[];t_list=[]	
	for i in log(h):
		h_list.append(i)
	for i in t:
		t_list.append(i)
	[b,m,sigma_b,sigma_m]=linreg(t_list,h_list,sigma_h_log)
	print([m, sigma_m])
	plt.plot(x,m*x+b)
	plt.legend(numpoints=1,loc='upper right')
	plt.savefig('../pdfs/Plot_Aufg2b.pdf')
	plt.show()
Example #10
0
    indx = []

    c = []
    cs = []

    for file in tfilesc:
        ix = file.indx[el]
        indx.append(ix)

        c.append(file.F1001W[ix])
        cs.append(file.F1010W[ix])

    #print len(dpps),len(c)
    #print dpps
    print dpps, c
    cfit = linreg(dpps, c)
    csfit = linreg(dpps, cs)
    c = cfit[0]
    cerr = cfit[3]
    cs = csfit[0]
    cserr = csfit[3]

    print >> f, file.NAME[ix], file.S[ix], c, cerr, cs, cserr

    f1001free = upac / downac * c
    f1010free = upac / downac * cs
    f1001freeE = upac / downac * cerr
    f1010freeE = upac / downac * cserr

    Qup = cos(2 * pi * qx) - cos(2 * pi * qy)
    Qdown = pi * (sin(2 * pi * qx) + sin(2 * pi * qy))
Example #11
0
#import correlation
import pandas as pd
import linreg
import matplotlib
import matplotlib.pyplot as plt



er = pd.read_csv(r'D:\EURRUB_160101_180323.txt')
ur = pd.read_csv(r'D:\USDRUB_160101_180323.txt')

erd=[]
urd = []
for e,u in zip (er['close'], ur['close']):
    erd.append(e)
    urd.append(u)

a,b = linreg.linreg(erd, urd)

ypredict = []
for e in erd:
    ypredict.append(e*b+a)
plt.scatter(erd,urd)
plt.plot(erd, ypredict)
plt.show()
linreg.stderror(erd,urd)
Example #12
0
import numpy as np
import linreg

auto = np.genfromtxt('auto-mpg.data', comments='"')
auto = auto[~np.isnan(auto).any(axis=1)]

inputs = auto[:, 1:]
targets = auto[:, 0:1]

inputs -= inputs.mean(axis=0)
inputs /= inputs.std(axis=0)

#train = np.ones((inputs.shape[0], 1))
#train[::10] = 0
#train = np.argwhere(train)
#train_in = inputs[train[:, 0]]
#train_tgt = targets[train[:, 0]]

#test = np.zeros((inputs.shape[0], 1))
#test[::10] = 1
#test = np.argwhere(test)
#test_in = inputs[test[:, 0]]
#test_tgt = targets[test[:, 0]]

beta = linreg.linreg(inputs, targets)
test_in = np.concatenate((inputs, -np.ones((np.shape(inputs)[0], 1))), axis=1)
testout = np.dot(test_in, beta)
error = np.sum((testout - targets)**2)
print error
N = 50
x = np.arange(1, N + 1)
y = np.empty(N)
alpha = 2.
sigma = alpha * np.ones(N)  # Constant error bar
np.random.seed(42)
for i in range(N):
    r = alpha * np.random.normal()
    #print(r)
    y[i] = c[0] + c[1] * x[i] + c[2] * x[i]**2 + r

fit = int(input('Enter degree of polynomial (1 is linear): '))
fit += 1

if fit == 2:
    [a_fit, sig_a, yy, chisqr] = linreg(x, y, sigma)
else:
    [a_fit, sig_a, yy, chisqr] = pollsf(x, y, sigma, fit)

# Print out the fit parameters and their error bars:
print('Fit parameters: ')
for i in range(fit):
    print(f'a[{i}] = {a_fit[i]} +/- {sig_a[i]}')

# Graph the data, with error bars, and fitting function
plt.errorbar(x, y, sigma, None, 'o')
plt.plot(x, yy, '-')
plt.xlabel(r'$x_i$')
plt.ylabel(r'$y_i$ and $Y(x)$')
plt.title(f'$Chi^2$ = {chisqr:.3f}, N-M = {N-fit}')
plt.show()
    def findoffBB(self,t,plane,MADTwiss,betalist):

        print "METHOD => Starting to calculate off-momentum beta-beat"

        
	self.bpms=intersect(t)
	self.bpms=modelIntersect(self.bpms, MADTwiss)
	MADTwiss.chrombeat()

	self.slope={}
        self.slopeE={}
	self.slopeM={}

        dpplist=[]

        for i in range(len(betalist)): #### for sorting

            dpplist.append(betalist[i]['DPP'])

        
        dpplist.sort()
        
        #if dpplist[0]>dpplist[1]
        #dpplist.reverse()

        
	for i in range(len(self.bpms)):
            
            bn=upper(self.bpms[i][1])
            check=0
            slopei=0.0
            slopeiM=0.0
            beta=[]
            k=0
            

            for k in range(len(dpplist)):
                count=0
                for j in range(len(betalist)):
                    
                    if dpplist[k]==betalist[j]['DPP']:

                        if count==0:
                        
                            beta.append(betalist[j][bn][0])
                        count=1
                        
          
                  
            [slope, offset, R, slope_error, offset_error]=linreg.linreg(dpplist,beta)
           
            a=FIT(dpplist,beta,1,1e-15)
            #print ' list '+str(dpplist)
            coef,error=a.linreg()             
			
            if plane=='H':
                slopeiM=MADTwiss.dbx[MADTwiss.indx[upper(bn)]]
            else:
                slopeiM=MADTwiss.dby[MADTwiss.indx[upper(bn)]]
	
            #self.slope[bn]=coef[0]
            self.slope[bn]=coef[0]/100
            #self.slopeE[bn]=error[0]
            self.slopeE[bn]=error[0]/100
            self.slopeM[bn]=slopeiM
Example #15
0
    c=[]
    cs=[]

    for file in tfilesc:
        ix=file.indx[el]
        indx.append(ix)

        c.append(file.F1001W[ix])
        cs.append(file.F1010W[ix])


    #print len(dpps),len(c)
    #print dpps
    print dpps,c
    cfit=linreg(dpps,c)
    csfit=linreg(dpps,cs)
    c=cfit[0]
    cerr=cfit[3]
    cs=csfit[0]
    cserr=csfit[3]
    

    print >>f,file.NAME[ix],file.S[ix],c,cerr,cs,cserr

    
    f1001free=upac/downac*c
    f1010free=upac/downac*cs
    f1001freeE=upac/downac*cerr
    f1010freeE=upac/downac*cserr
Example #16
0
targets1 = pima[::2, 8:9]
targets2 = pima[1::2, 8:9]

# Perceptron training on the preprocessed dataset
p1 = pcn.pcn(inputs1, targets1)
p1.pcntrain(inputs1, targets1, 0.25, 100)
cm1 = p1.confmat(inputs2, targets2)
p2 = pcn.pcn(inputs2, targets2)
p2.pcntrain(inputs2, targets2, 0.25, 100)
cm2 = p2.confmat(inputs1, targets1)
cm = cm1 + cm2
print("Perceptron classification accuracy: ")
print(np.trace(cm) / np.sum(cm))

# Linear regression on the preprocessed dataset
beta1 = linreg.linreg(inputs1, targets1)
beta2 = linreg.linreg(inputs2, targets2)
inputs1 = np.concatenate((inputs1, -np.ones((np.shape(inputs1)[0], 1))),
                         axis=1)
inputs2 = np.concatenate((inputs2, -np.ones((np.shape(inputs2)[0], 1))),
                         axis=1)
outputs1 = np.dot(inputs1, beta2)
outputs1[np.where(outputs1 > 0.5)] = 1
outputs1[np.where(outputs1 <= 0.5)] = 0
outputs2 = np.dot(inputs2, beta1)
outputs2[np.where(outputs2 > 0.5)] = 1
outputs2[np.where(outputs2 <= 0.5)] = 0
outputs = np.r_[outputs1, outputs2]
targets = np.r_[targets1, targets2]
print("Linear regression classification accuracy:")
print(np.sum(targets == outputs) / np.size(targets))
Example #17
0
import numpy as np
import linreg

auto = np.loadtxt('../Data/auto-mpg.data', comments='"')

# Normalise the data
auto[:,0:-1] *= 1./auto[:,0:-1].max(axis=0)

# Separate the data into training and testing sets

trainin = auto[0:round(np.shape(auto)[0]*0.4),:]
traintgt = trainin[:,-1].reshape(-1,1)
trainin = trainin[:,0:-1]

testin = auto[round(np.shape(auto)[0]*0.4):,:]
testtgt = testin[:,-1].reshape(-1,1)
testin = testin[:,0:-1]


# This is the training part
beta = linreg.linreg(trainin,traintgt)
testin = np.concatenate((testin,-np.ones((np.shape(testin)[0],1))),axis=1)
testout = np.dot(testin,beta)
error = np.sum((testout - testtgt)**2)
print error
Example #18
0
import pylab as pl


data = np.loadtxt("train.csv", delimiter = ',')
testData = np.loadtxt("test.csv",delimiter = ',')

x = data[:,:-1]
y = data[:,-1]
testx = testData[:,:-1]
testy = testData[:,-1]

##########################
# Experiment
##########################
lam = [0] + [10**i for i in range(-3,3)]
w = linreg.linreg(x,y,lam) # return linear regression parameters w
sse = linreg.sserr(x,y,w,lam) # return sum-squared error for tranining set
testsse = linreg.sserr(testx,testy,w,lam) # return sum-squared error for testing set
predicterr = linreg.test(testx,testy,w,lam)

#####################################
# remove singular squared errors
#####################################
sings= 8
singus = np.sort(predicterr,axis=1)[:,-sings:]
sinerr = 0
for i in range(sings):
	sinerr += 0.5*singus[:,i]**2
rmsingular = testsse-sinerr

Example #19
0
bpms = intersect(tfilesx)
for bpm in bpms:
    el = bpm[1]
    indx = []
    b = []
    a = []
    betax0 = tzerox.BETX[tzerox.indx[el]]
    alfax0 = tzerox.ALFX[tzerox.indx[el]]
    alfax0err = tzerox.STDALFX[tzerox.indx[el]]
    for file in tfilesx:
        ix = file.indx[el]
        indx.append(ix)
        #b.append((file.BETX[ix]-betax0)/betax0)
        b.append(file.BETX[ix])
        a.append(file.ALFX[ix])
    bfit = linreg(dpps, b)
    afit = linreg(dpps, a)
    dbb = bfit[0] / betax0
    dbberr = bfit[3] / betax0
    da = afit[0]
    daerr = afit[3]
    AX = dbb  # FROM and FOR MAD
    AXerr = dbberr
    BX = da - alfax0 * dbb  # FROM and FOR MAD
    BXerr = sqrt(daerr**2 + (alfax0err * dbb)**2 + (alfax0 * dbberr)**2)
    wx = sqrt(AX**2 + BX**2)
    wxerr = sqrt((AXerr * AX / wx)**2 + (BXerr * BX / wx)**2)
    phix = atan2(BX, AX) / 2. / pi
    phixerr = 1. / (1. +
                    (AX / BX)**2) * sqrt((AXerr / BX)**2 +
                                         (AX / BX**2 * BXerr)**2) / 2. / pi
# Demonstration of the Perceptron and Linear Regressor on the basic logic functions

import numpy as np
import linreg

inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
testin = np.concatenate((inputs, -np.ones((np.shape(inputs)[0], 1))), axis=1)

# AND data
ANDtargets = np.array([[0], [0], [0], [1]])
# OR data
ORtargets = np.array([[0], [1], [1], [1]])
# XOR data
XORtargets = np.array([[0], [1], [1], [0]])

print "AND data"
ANDbeta = linreg.linreg(inputs, ANDtargets)
ANDout = np.dot(testin, ANDbeta)
print ANDout

print "OR data"
ORbeta = linreg.linreg(inputs, ORtargets)
ORout = np.dot(testin, ORbeta)
print ORout

print "XOR data"
XORbeta = linreg.linreg(inputs, XORtargets)
XORout = np.dot(testin, XORbeta)
print XORout
Example #21
0
bpms=intersect(tfilesx)
for bpm in bpms:
    el=bpm[1]
    indx=[]
    b=[]
    a=[]
    betax0=tzerox.BETX[tzerox.indx[el]]
    alfax0=tzerox.ALFX[tzerox.indx[el]]
    alfax0err=tzerox.STDALFX[tzerox.indx[el]]
    for file in tfilesx:
        ix=file.indx[el]
        indx.append(ix)
        #b.append((file.BETX[ix]-betax0)/betax0)
        b.append(file.BETX[ix])
        a.append(file.ALFX[ix])
    bfit=linreg(dpps, b)
    afit=linreg(dpps, a)
    dbb=bfit[0]/betax0
    dbberr=bfit[3]/betax0
    da=afit[0]
    daerr=afit[3]
    AX=dbb     # FROM and FOR MAD
    AXerr=dbberr
    BX=da-alfax0*dbb       # FROM and FOR MAD 
    BXerr=sqrt(daerr**2 + (alfax0err*dbb)**2 + (alfax0*dbberr)**2)
    wx=sqrt(AX**2+BX**2)
    wxerr=sqrt( (AXerr*AX/wx)**2 + (BXerr*BX/wx)**2  )
    phix=atan2(BX,AX)/2./pi
    phixerr=1./(1.+(AX/BX)**2)*sqrt( (AXerr/BX)**2 + (AX/BX**2*BXerr)**2)/2./pi
    print >>f, file.NAME[ix], file.S[ix],  dbb, dbberr, da, daerr, wx, wxerr, phix, phixerr
    
Example #22
0
#np.random.shuffle(inputs)
#np.random.shuffle(targets)

test = inputs[-800:, :]
testTargets = targets[-800:]
train = inputs[:-800:2, :]
#np.random.shuffle(train)
trainTargets = targets[:-800:2]
valid = inputs[1:-800:2, :]
#np.random.shuffle(valid)
validTargets = targets[1:-800:2]
# Train the network
net = mlp(train, trainTargets, 3, outtype='linear')
net.earlystopping(train, trainTargets, valid, validTargets, 0.25)

test = np.concatenate((test, -np.ones((np.shape(test)[0], 1))), axis=1)
testOutMlp = net.mlpfwd(test)

pl.figure()
pl.plot(np.arange(np.shape(test)[0]), testOutMlp, '.')
pl.plot(np.arange(np.shape(test)[0]), testTargets, 'x')
pl.legend(('Predictions', 'Targets'))
print("error for mlp", 0.5 * np.sum((testTargets - testOutMlp)**2))
pl.show()

#for linear regression
beta = linreg.linreg(train, trainTargets)
test1 = np.c_[test, -np.ones((np.shape(test)[0], 1))]
testout = np.dot(test, beta)
error = np.sum((testout - testTargets)**2)
print("sum of sqaured error for linear reg", error)
n, m = F.shape
nrf, m = G.shape

#print n, m, nrf

F = np.matrix(F)
G = np.matrix(G)
R = np.matrix(np.zeros((nrf, n+1)))


nepal_features = extract_indicators("Nepal", '2011')

recommendations = {}
for i in range(nrf):
    print "Training %d/%d"%(i+1, nrf)
    L = linreg.linreg()
    L.train(F.T, G[i,:].T,regularization_param=10, alpha = 0.05, normalize=False, epochs=1000)
    R[i, :]= L.theta.T
    recommendations[recommended_features_list[i]] = L


# predictions = R * nepal_features
# predictions =

# for i, indicator_name in enumerate(recommended_features_list):
    # print indicator_name, predictions[i, 0]
    # print
results = {}

for r in recommendations:
    results[r] = recommendations[r].predict(nepal_features.T)[0,0]
Example #24
0
n, m = F.shape
nrf, m = G.shape

#print n, m, nrf

F = np.matrix(F)
G = np.matrix(G)
R = np.matrix(np.zeros((nrf, n + 1)))

nepal_features = extract_indicators("Nepal", '2011')

recommendations = {}
for i in range(nrf):
    print "Training %d/%d" % (i + 1, nrf)
    L = linreg.linreg()
    L.train(F.T,
            G[i, :].T,
            regularization_param=10,
            alpha=0.05,
            normalize=False,
            epochs=1000)
    R[i, :] = L.theta.T
    recommendations[recommended_features_list[i]] = L

# predictions = R * nepal_features
# predictions =

# for i, indicator_name in enumerate(recommended_features_list):
# print indicator_name, predictions[i, 0]
# print
Example #25
0
# Code from Chapter 2 of Machine Learning: An Algorithmic Perspective
# by Stephen Marsland (http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html)

# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.

# Stephen Marsland, 2008

# This is the start of a script for you to complete
from pylab import *
from numpy import *
import linreg

auto = loadtxt('auto-mpg.data.txt',comments='"')

# Separate the data into training and testing sets

# Normalise the data

# This is the training part
beta = linreg.linreg(trainin,traintgt)
testin = concatenate((testin,-ones((shape(testin)[0],1))),axis=1)
testout = dot(testin,beta)
error = sum((testout - testtgt)**2)
print error
Example #26
0
def dolinregbet(fileobj,listx,listy,bpms,plane,zero,twiss):
    '''
    Calculates stuff and writes to the file in a table
    Closes the file afterwards

    :param fileobj: chromFileWriter for output table
    :param listx: List of variables...
    :param listy: List of variables...
    :param bpms: List of BPMs
    :param plane: Which plane (H/V)
    :param zero: Twiss for dp/p = 0
    :param twiss: Twiss
    '''
    for bpm in bpms:
        name=bpm[1]
        sloc=bpm[0]
        indx=[]
        b=[]
        a=[]
        bm=[]
        am=[]
        if "H" in plane:
            beta0=zero.BETX[zero.indx[name]]
            alfa0=zero.ALFX[zero.indx[name]]
            alfa0err=zero.STDALFX[zero.indx[name]]

            beta0m=twiss.BETX[twiss.indx[name]]
            alfa0m=twiss.ALFX[twiss.indx[name]]

            wmo=twiss.WX[twiss.indx[name]]
            pmo=twiss.PHIX[twiss.indx[name]]
        else:

            beta0=zero.BETY[zero.indx[name]]
            alfa0=zero.ALFY[zero.indx[name]]
            alfa0err=zero.STDALFY[zero.indx[name]]

            beta0m=twiss.BETY[twiss.indx[name]]
            alfa0m=twiss.ALFY[twiss.indx[name]]

            wmo=twiss.WY[twiss.indx[name]]
            pmo=twiss.PHIY[twiss.indx[name]]
        for dpp in listx:
            _file=listy[dpp]
            ix=_file.indx[name]
            indx.append(ix)
            if "H" in plane:
                b.append(_file.BETX[ix])
                a.append(_file.ALFX[ix])

                bm.append(_file.BETXMDL[_file.indx[name]])
                am.append(_file.ALFXMDL[_file.indx[name]])
            else:
                b.append(_file.BETY[ix])
                a.append(_file.ALFY[ix])

                bm.append(_file.BETYMDL[_file.indx[name]])
                am.append(_file.ALFYMDL[_file.indx[name]])

        bfit=linreg.linreg(listx, b)
        afit=linreg.linreg(listx, a)

        bfitm=linreg.linreg(listx, bm)
        afitm=linreg.linreg(listx, am)

        # measurement
        dbb=bfit[0]/beta0
        dbberr=bfit[3]/beta0
        da=afit[0]
        daerr=afit[3]
        A=dbb
        Aerr=dbberr
        B=da-alfa0*dbb
        Berr=math.sqrt(daerr**2 + (alfa0err*dbb)**2 + (alfa0*dbberr)**2)
        w=0.5*math.sqrt(A**2+B**2)
        werr=0.5*math.sqrt( (Aerr*A/w)**2 + (Berr*B/w)**2  )
        phi=math.atan2(B,A)/2./math.pi
        phierr=1./(1.+(A/B)**2)*math.sqrt( (Aerr/B)**2 + (A/B**2*Berr)**2)/2./math.pi

        #model
        dbbm=bfitm[0]/beta0m
        dbberrm=bfitm[3]/beta0m
        dam=afitm[0]
        daerrm=afitm[3]
        Am=dbbm
        Aerrm=dbberrm
        Bm=dam-alfa0m*dbbm
        Berrm=math.sqrt(daerrm**2 + (alfa0m*dbberrm)**2)
        wm=0.5*math.sqrt(Am**2+Bm**2)
        werrm=0.5*math.sqrt( (Aerrm*Am/wm)**2 + (Berrm*Bm/wm)**2  )
        phim=math.atan2(Bm,Am)/2./math.pi
        phierrm=1./(1.+(Am/Bm)**2)*math.sqrt( (Aerrm/Bm)**2 + (Am/Bm**2*Berrm)**2)/2./math.pi

        fileobj.writeLine(locals().copy())