fix conflict & pull hw5

This commit is contained in:
Ting-Jun Wang 2023-12-06 15:33:28 +08:00
commit bfc3fbcd17
Signed by: snsd0805
GPG Key ID: D175E969960C4B16
5 changed files with 180 additions and 24 deletions

View File

@ -33,8 +33,33 @@ 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)
if __name__ == '__main__': 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 = form(x) x = form(x)
prob = problem(y, x) prob = problem(y, x)
lambda_powers = [-6, -4, -2, 0, 2] lambda_powers = [-6, -4, -2, 0, 2]
@ -53,7 +78,7 @@ if __name__ == '__main__':
ans, min_err = None, 1 ans, min_err = None, 1
for i in results: for i in results:
print(i) print(i['error'])
if i['error'] <= min_err: if i['error'] <= min_err:
min_err = i['error'] min_err = i['error']
ans = i ans = i

View File

@ -43,10 +43,37 @@ 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 i in range(128): for index 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)
random.seed(datetime.datetime.now().timestamp()+i) random.seed(datetime.datetime.now().timestamp()+i)
prob = problem(train_y, train_x) prob = problem(train_y, train_x)

View File

@ -52,7 +52,33 @@ 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]

48
hw5/hw5_10.py Normal file
View File

@ -0,0 +1,48 @@
import numpy as np
import datetime
import random
from libsvm.svmutil import *
import matplotlib.pyplot as plt
FILENAME = "satimage.scale"
TEST_FILENAME = "satimage.scale.t"
TARGET = 1
def new_label(y, target):
ans = []
for i in y:
if i == target:
ans.append(1)
else:
ans.append(0)
return ans
def error(predict, gt):
error_count = 0
for index in range(len(predict)):
if predict[index] != gt[index]:
error_count += 1
return error_count / len(predict)
if __name__ == '__main__':
y, x = svm_read_problem(FILENAME)
y = new_label(y, TARGET)
test_y, test_x = svm_read_problem(TEST_FILENAME)
test_y = new_label(test_y, TARGET)
for c in [0.01, 0.1, 1, 10, 100]:
print("C=", c)
prob = svm_problem(y, x)
param = svm_parameter('-s 0 -t 2 -g 1 -c {} -q'.format(c))
m = svm_train(prob, param)
p_label, p_acc, p_val = svm_predict(test_y, test_x, m)
my_error = error(p_label, test_y)
print("p_acc:", p_acc)
print("0/1 error:", my_error)
print("="*20)

30
hw5/hw5_9.py Normal file
View File

@ -0,0 +1,30 @@
import numpy as np
import datetime
import random
from libsvm.svmutil import *
import matplotlib.pyplot as plt
FILENAME = "satimage.scale"
TEST_FILENAME = "satimage.scale.t"
def new_label(y, target):
ans = []
for i in y:
if i == target:
ans.append(1)
else:
ans.append(0)
return ans
if __name__ == '__main__':
y, x = svm_read_problem(FILENAME)
y = new_label(y, 4)
for c in [0.1, 1, 10]:
for q in [2, 3, 4]:
print("(C, Q)=({}, {})".format(c, q))
prob = svm_problem(y, x)
param = svm_parameter('-s 0 -t 1 -d {} -c {}'.format(q, c))
m = svm_train(prob, param)
print("="*20)