Compare commits
No commits in common. "2e6f7162455f05bde1e9d50414e2fdfce165027b" and "a6b88f3f9b3c4199d74314bbd5047af0178957c9" have entirely different histories.
2e6f716245
...
a6b88f3f9b
@ -33,57 +33,32 @@ def error(gt, pred):
|
|||||||
err = (err+1) if gt[index]!=pred[index] else err
|
err = (err+1) if gt[index]!=pred[index] else err
|
||||||
return err/len(gt)
|
return err/len(gt)
|
||||||
|
|
||||||
def transform(features):
|
if __name__ == '__main__':
|
||||||
output_features = []
|
x, y = read_data(FILENAME)
|
||||||
for index, feature in enumerate(features):
|
x = form(x)
|
||||||
output_features.append([ 0 for _ in range(84) ])
|
prob = problem(y, x)
|
||||||
output_features[index][0] = 1
|
lambda_powers = [-6, -4, -2, 0, 2]
|
||||||
|
|
||||||
d_index = 1
|
results = []
|
||||||
# 1-order
|
for lambda_power in lambda_powers:
|
||||||
for i in feature:
|
lambda_value = 10 ** lambda_power
|
||||||
output_features[index][d_index] = i
|
param_C = 1/(2*lambda_value)
|
||||||
d_index += 1
|
param = parameter('-s 0 -c {} -e 0.000001 -q'.format(param_C))
|
||||||
|
model = train(prob, param)
|
||||||
|
p_label, p_acc, p_val = predict(y, x, model)
|
||||||
|
err = error(y, p_label)
|
||||||
|
print("0/1 error: ", err)
|
||||||
|
print()
|
||||||
|
results.append({'lambda': lambda_power, 'error': err})
|
||||||
|
|
||||||
# 2-orde
|
ans, min_err = None, 1
|
||||||
for i in range(len(feature)):
|
for i in results:
|
||||||
for j in range(i, len(feature)):
|
print(i['error'])
|
||||||
output_features[index][d_index] = feature[i]*feature[j]
|
if i['error'] <= min_err:
|
||||||
d_index += 1
|
min_err = i['error']
|
||||||
# 3-order
|
ans = i
|
||||||
for i in range(len(feature)):
|
|
||||||
for j in range(i, len(feature)):
|
|
||||||
for k in range(j, len(feature)):
|
|
||||||
output_features[index][d_index] = i*j*k
|
|
||||||
d_index += 1
|
|
||||||
return output_features
|
|
||||||
|
|
||||||
x, y = read_data(FILENAME)
|
print("the largest lambda: {}, log_10(lambda*): {}".format(10**ans['lambda'], ans['lambda']))
|
||||||
x = transform(x)
|
|
||||||
x = form(x)
|
|
||||||
prob = problem(y, x)
|
|
||||||
lambda_powers = [-6, -4, -2, 0, 2]
|
|
||||||
|
|
||||||
results = []
|
|
||||||
for lambda_power in lambda_powers:
|
|
||||||
lambda_value = 10 ** lambda_power
|
|
||||||
param_C = 1/(2*lambda_value)
|
|
||||||
param = parameter('-s 0 -c {} -e 0.000001 -q'.format(param_C))
|
|
||||||
model = train(prob, param)
|
|
||||||
p_label, p_acc, p_val = predict(y, x, model)
|
|
||||||
err = error(y, p_label)
|
|
||||||
print("0/1 error: ", err)
|
|
||||||
print()
|
|
||||||
results.append({'lambda': lambda_power, 'error': err})
|
|
||||||
|
|
||||||
ans, min_err = None, 1
|
|
||||||
for i in results:
|
|
||||||
print(i['error'])
|
|
||||||
if i['error'] <= min_err:
|
|
||||||
min_err = i['error']
|
|
||||||
ans = i
|
|
||||||
|
|
||||||
print("the largest lambda: {}, log_10(lambda*): {}".format(10**ans['lambda'], ans['lambda']))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -36,6 +36,7 @@ def error(gt, pred):
|
|||||||
return err/len(gt)
|
return err/len(gt)
|
||||||
|
|
||||||
def new_split(x, y):
|
def new_split(x, y):
|
||||||
|
random.seed(datetime.datetime.now().timestamp())
|
||||||
data = list(zip(x, y))
|
data = list(zip(x, y))
|
||||||
random.shuffle(data)
|
random.shuffle(data)
|
||||||
x, y = zip(*data)
|
x, y = zip(*data)
|
||||||
@ -43,37 +44,10 @@ def new_split(x, y):
|
|||||||
train_y, val_y = y[:120], y[120:]
|
train_y, val_y = y[:120], y[120:]
|
||||||
return (train_x, train_y), (val_x, val_y)
|
return (train_x, train_y), (val_x, val_y)
|
||||||
|
|
||||||
def transform(features):
|
|
||||||
output_features = []
|
|
||||||
for index, feature in enumerate(features):
|
|
||||||
output_features.append([ 0 for _ in range(84) ])
|
|
||||||
output_features[index][0] = 1
|
|
||||||
|
|
||||||
d_index = 1
|
|
||||||
# 1-order
|
|
||||||
for i in feature:
|
|
||||||
output_features[index][d_index] = i
|
|
||||||
d_index += 1
|
|
||||||
|
|
||||||
# 2-orde
|
|
||||||
for i in range(len(feature)):
|
|
||||||
for j in range(i, len(feature)):
|
|
||||||
output_features[index][d_index] = feature[i]*feature[j]
|
|
||||||
d_index += 1
|
|
||||||
# 3-order
|
|
||||||
for i in range(len(feature)):
|
|
||||||
for j in range(i, len(feature)):
|
|
||||||
for k in range(j, len(feature)):
|
|
||||||
output_features[index][d_index] = i*j*k
|
|
||||||
d_index += 1
|
|
||||||
return output_features
|
|
||||||
|
|
||||||
x, y = read_data(FILENAME)
|
x, y = read_data(FILENAME)
|
||||||
x = transform(x)
|
|
||||||
x = format(x)
|
x = format(x)
|
||||||
log_lambda = []
|
log_lambda = []
|
||||||
for index in range(128):
|
for _ in range(128):
|
||||||
random.seed(datetime.datetime.now().timestamp()+index)
|
|
||||||
(train_x, train_y), (val_x, val_y) = new_split(x, y)
|
(train_x, train_y), (val_x, val_y) = new_split(x, y)
|
||||||
prob = problem(train_y, train_x)
|
prob = problem(train_y, train_x)
|
||||||
|
|
||||||
|
|||||||
@ -52,33 +52,7 @@ def new_split(x, y):
|
|||||||
|
|
||||||
return folds
|
return folds
|
||||||
|
|
||||||
def transform(features):
|
|
||||||
output_features = []
|
|
||||||
for index, feature in enumerate(features):
|
|
||||||
output_features.append([ 0 for _ in range(84) ])
|
|
||||||
output_features[index][0] = 1
|
|
||||||
|
|
||||||
d_index = 1
|
|
||||||
# 1-order
|
|
||||||
for i in feature:
|
|
||||||
output_features[index][d_index] = i
|
|
||||||
d_index += 1
|
|
||||||
|
|
||||||
# 2-orde
|
|
||||||
for i in range(len(feature)):
|
|
||||||
for j in range(i, len(feature)):
|
|
||||||
output_features[index][d_index] = feature[i]*feature[j]
|
|
||||||
d_index += 1
|
|
||||||
# 3-order
|
|
||||||
for i in range(len(feature)):
|
|
||||||
for j in range(i, len(feature)):
|
|
||||||
for k in range(j, len(feature)):
|
|
||||||
output_features[index][d_index] = i*j*k
|
|
||||||
d_index += 1
|
|
||||||
return output_features
|
|
||||||
|
|
||||||
x, y = read_data(FILENAME)
|
x, y = read_data(FILENAME)
|
||||||
x = transform(x)
|
|
||||||
x = format(x)
|
x = format(x)
|
||||||
log_lambda = []
|
log_lambda = []
|
||||||
lambda_powers = [-6, -4, -2, 0, 2]
|
lambda_powers = [-6, -4, -2, 0, 2]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user