def sampleTraining(X, Y, Xt, Yt=None): # Function used for testing various model of training # Class weight class_weight = {0:0.6, 1:0.4} linearRegression(X, Y, Xt, Yt) logisticRegression(X, Y, Xt, Yt, class_weight) sgdClassify(X, Y, Xt, Yt, class_weight)
def correlation(location, independentName, dependentName, locationName, sampleNumbers, timeSteps): location=boolean(location) name=independentName + '_' + dependentName + '_' + locationName tssFileIntercept = file("%s%s.tss" % (name,'_int'), "w") tssFileSlope = file("%s%s.tss" % (name,'_slope'), "w") tssFileRSquared = file("%s%s.tss" % (name,'_rSq'), "w") for step in timeSteps: values=[] for sample in sampleNumbers: smallValue=0.0000000000000000001 fileNameOne=generateNameST(dependentName,sample,step) valueOne=generalfunctions.getCellValueAtBooleanLocation(location,scalar(fileNameOne)) pairList=[valueOne + smallValue] fileNameTwo=generateNameST(independentName,sample,step) valueTwo=generalfunctions.getCellValueAtBooleanLocation(location,scalar(fileNameTwo)) pairList.append(valueTwo + smallValue) values.append(pairList) #print valueOne + smallValue, valueTwo + smallValue reg=regression.linearRegression(values, 1) rSq=regression.linearRSquared(values,reg) #print step, reg, rSq tssFileIntercept.write("%d %g\n" % (step, reg[0])) tssFileSlope.write("%d %g\n" % (step, reg[1])) tssFileRSquared.write("%d %g\n" % (step, rSq)) tssFileIntercept.close() tssFileSlope.close() tssFileRSquared.close()
def training(X, Y, Xt, filename): # Function actually used to generate predictions for final test sets # Class weight class_weight = {0:0.6, 1:0.4} YPredict1 = linearRegression(X, Y, Xt, None) YPredict2, YProb2 = logisticRegression(X, Y, Xt, None, class_weight) YPredict3, YProb3 = sgdClassify(X, Y, Xt, None, class_weight) util.savePred(YPredict1, YPredict1, "{0}1.csv".format(filename)) util.savePred(YPredict2, YProb2, "{0}2.csv".format(filename)) util.savePred(YPredict3, YProb2, "{0}3.csv".format(filename))
@author: fredrikgarsegmork """ import numpy as np import matplotlib.pyplot as plt #from scipy.interpolate import polyfit from regression import linearRegression, quadraticRegression, cubicRegression print('Oppgave 2 a): Grafen passer beskrivelsen meget.') T=[13.14,12.89,12.26,12.64,12.22,12.47,12.51,12.80,12.24,12.77,13.35,12.82,13.57,13.38,14.41,14.00,15.68,15.41,15.51,15.86,15.72] Tid=[0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60] plt.scatter(Tid, T, label= 'tempScatter', color='k') #Oppgave b) #Linear reggression [a, b] = linearRegression(Tid, T) xplot = Tid yplot = np.dot(a,xplot)+b plt.plot(xplot, yplot, 'r') #Determinant koeffisienten Sy2 = sum((T-np.mean(T))**2) SSELin = sum((T-np.dot(a,Tid)-b)**2) r2Linear = (Sy2-SSELin)/Sy2 print('\nLineære regresjon sin determinant koeffisient:') print(np.sqrt(r2Linear)) #print(np.corrcoef(Tid, T)[0][1]) #Oppgave c #Quadratic regression [a, b, c] = quadraticRegression(Tid, T) yplot = np.dot(a,np.power(xplot,2))+np.dot(b,xplot) + c
# Oppgave 2 T = [ 13.14, 12.89, 12.26, 12.64, 12.22, 12.47, 12.51, 12.80, 12.24, 12.77, 13.35, 12.82, 13.57, 13.38, 14.41, 14.00, 15.68, 15.41, 15.51, 15.86, 15.72 ] X = [x * 3 for x in range(0, len(T))] plt.figure(1) # a) plt.scatter(X, T) # b) [a_lin, b_lin] = regression.linearRegression(X, T) Sy2_lin = sum((T - np.mean(T))**2) SSE_lin = sum((T - (np.dot(a_lin, X) + b_lin))**2) R2_lin = (Sy2_lin - SSE_lin) / Sy2_lin print("Determinantkoeffisient for lineære", R2_lin) # -> 0.7452554736192584 xplot = np.array(list(range(min(X), max(X)))) yplot = np.dot(xplot, a_lin) + b_lin plt.plot(xplot, yplot) # c) [a_kva, b_kva, c_kva] = regression.quadraticRegression(X, T)
13.35, 12.82, 13.57, 13.38, 14.41, 14.00, 15.68, 15.41, 15.51, 15.86, 15.72 ] tid = [ 2.86, 2 * 2.86, 3 * 2.86, 4 * 2.86, 5 * 2.86, 6 * 2.86, 7 * 2.86, 8 * 2.86, 9 * 2.86, 10 * 2.86, 11 * 2.86, 12 * 2.86, 13 * 2.86, 14 * 2.86, 15 * 2.86, 16 * 2.86, 17 * 2.86, 18 * 2.86, 19 * 2.86, 20 * 2.86, 21 * 2.86 ] plt.figure(0) plt.scatter(tid, temp) # Ja, det ser ut som det passer. # b) [a, b] = linearRegression(tid, temp) xplot = np.array(list(range(0, 60))) yplot = np.dot(a, xplot) + b plt.plot(xplot, yplot) Sy2 = sum((temp - np.mean(temp))**2) SSELin = sum((temp - np.dot(a, tid) - b)**2) r2Linear = (Sy2 - SSELin) / Sy2 # Determinasjonskoeffisienten = 0.745 # c) [a, b, c] = quadraticRegression(tid, temp) yplot = np.dot(a, np.power(xplot, 2)) + np.dot(b, xplot) + c plt.plot(xplot, yplot)
T = [ 13.14, 12.89, 12.26, 12.64, 12.22, 12.47, 12.51, 12.80, 12.24, 12.77, 13.35, 12.82, 13.57, 13.38, 14.41, 14.00, 15.68, 15.41, 15.51, 15.86, 15.72 ] X = [] for i in range(0, 62, 3): X.append(i) plt.scatter(X, T) # Svar: plottet viser at temperaturen er på vei ned, før den snur og stiger igjen # vi ser den sikkert snu når ovnen ble skrudd på. Dataene ser ut til å passe plottet # b) [a, b] = reg.linearRegression(X, T) # plt.figure(0) # xplot = np.array(list(range(-1,6))) # litt større område enn x-ene yplot = np.dot(a, X) + b plot_lin = plt.plot(X, yplot, label="Linear") Sy2 = sum((T - np.mean(T))**2) SSELin = sum((T - np.dot(a, X) - b)**2) r2Linear = (Sy2 - SSELin) / Sy2 print("\n2b)\nr² = ", r2Linear) # c) # Kvadratisk tilnærming: [a, b, c] = reg.quadraticRegression(X, T) # så kvadratisk
from __future__ import division import numpy as np import scipy.linalg as sl import matplotlib.pyplot as plt import scipy.io import regression data = scipy.io.loadmat("Arbeidskrav3.mat") x = np.array(data['x']).astype(float).reshape(11) y = np.array(data['y']).astype(float).reshape(11) omega = data["omega"][0][0].astype(float) # 1a - Se 1a.png for figur # Linear regression [a, b] = regression.linearRegression(x, y) plt.figure(0) plt.scatter(x, y) xplot = np.linspace(-2, 12) yplot = np.dot(a, xplot) + b plt.plot(xplot, yplot) Sy2 = sum((y - np.mean(y))**2) SSELin = sum((y - np.dot(a, x) - b)**2) r2Linear = (Sy2 - SSELin) / Sy2 # Quadratic regression [a, b, c] = regression.quadraticRegression(x, y) yplot = np.dot(a, np.power(xplot, 2)) + np.dot(b, xplot) + c plt.plot(xplot, yplot) SSEQuad = sum((y - (np.dot(a, np.power(x, 2)) + np.dot(b, x) + c))**2) r2Quadratic = (Sy2 - SSEQuad) / Sy2
#plotFeatureAnalysis(car_data) #plotAllFeatures(car_data) #select variables for Model #columnsModel1 = ["curb-weight", "engine-size", "city-mpg", "price"] columnsModel1 = ["curb-weight", "engine-size", "city-mpg", "price"] columnsModel2 = ["curb-weight", "horsepower", "city-mpg", "price"] columnsModel3 = ["curb-weight", "engine-size", "highway-mpg", "price"] dataModel1 = car_data[columnsModel1] dataModel2 = car_data[columnsModel2] dataModel3 = car_data[columnsModel3] print(dataModel1) #modify data dataModel1['curb-weight'] = dataModel1['curb-weight'] ** 2 dataModel2['curb-weight'] = dataModel2['curb-weight'] ** 2 dataModel3['curb-weight'] = dataModel3['curb-weight'] ** 2 #dataModel1['city-mpg'] = 1 / dataModel1['city-mpg'] dataModel2['city-mpg'] = 1 / dataModel2['city-mpg'] dataModel3['highway-mpg'] = 1 / dataModel3['highway-mpg'] #perform regressions from regression import linearRegression linearRegression(dataModel1,'model 1', 'predicted price ($)', 'actual price ($)') linearRegression(dataModel2,'model 2', 'predicted price ($)', 'actual price ($)') linearRegression(dataModel3,'model 3', 'predicted price ($)', 'actual price ($)')