<jupyter_start><jupyter_text>Q4和Q5的程序<jupyter_code>n = np.arange(3, 10000)
f1 = np.sqrt(8/n*(np.log(80)+50*np.log(2*n)))
print('Original VC bound (10000) and (5): ', f1[-1], '\t', f1[2])
f2 = np.sqrt(2/n*(np.log(2*n)+50*np.log(n)))+np.sqrt(2/n*math.log(20))+1/n
print('Rademacher Penalty Bound (10000) and (5): ', f2[-1], '\t', f2[2])
f3 = 1/n+np.sqrt(1/np.power(n, 2)+1/n*(np.log(120)+50*np.log(2*n)))
print('Parrondo and <NAME> (10000) and (5): ', f3[-1], '\t', f3[2])
f4 = 1/(n-2)+np.sqrt(1/np.power(n-2, 2)+1/(2*n-4)*(np.log(80)+100*np.log(n)))
print('Devroye (10000) and (5): ', f4[-1], '\t', f4[2])
f5 = np.sqrt(16/n*(np.log(2/math.sqrt(0.05))+50*np.log(n)))
print('Variant VC bound (10000) and (5): ', f5[-1], '\t', f5[2])
plt.plot(n, f1, label='Original VC bound')
plt.plot(n, f2, label='Rademacher Penalty Bound')
plt.plot(n, f3, label='Parrondo and <NAME>')
plt.plot(n, f4, label='Devroye')
plt.plot(n, f5, label='Variant VC bound')
plt.legend()
plt.show()<jupyter_output>Original VC bound (10000) and (5):  0.632203362312 	 13.828161485
Rademacher Penalty Bound (10000) and (5):  0.33132369478 	 7.04877656418
Parrondo and <NAME> (10000) and (5):  0.223708366246 	 5.10136198199
Devroye (10000) and (5):  0.215237656725 	 5.59312554318
Variant VC bound (10000) and (5):  0.860464345894 	 16.264111061<jupyter_text>Q16-Q20的程序<jupyter_code># 导入数据函数
def loadData(filename):
    data = pd.read_csv(filename, sep='\s+', header=None)
    data = data.as_matrix()
    col, row = data.shape
    X = np.c_[np.ones((col, 1)), data[:, 0: row-1]]
    Y = data[:, row-1:row]
    return X, Y
# 生成数据函数
def generateData():
    x = np.random.uniform(-1, 1, 20)
    y = np.sign(x)
    y[y == 0] = -1
    prop = np.random.uniform(0, 1, 20)
    y[prop >= 0.8] *= -1
    return x, y
# 单维度决策树桩算法
def decision_stump(X, Y):
    theta = np.sort(X)
    num = len(theta)
    Xtemp = np.tile(X, (num, 1))
    ttemp = np.tile(np.reshape(theta, (num, 1)), (1, num))
    ypred = np.sign(Xtemp - ttemp)
    ypred[ypred == 0] = -1
    err = np.sum(ypred != Y, axis=1)
    if np.min(err) <= num-np.max(err):
        return 1, theta[np.argmin(err)], np.min(err)/num
    else:
        return -1, theta[np.argmax(err)], (num-np.max(err))/num
# 多维度决策树桩算法
def decision_stump_multi(X, Y):
    row, col = X.shape
    err = np.zeros((col,)); s = np.zeros((col,)); theta = np.zeros((col,))
    for i in range(col):
        s[i], theta[i], err[i] = decision_stump(X[:, i], Y[:, 0])
    pos = np.argmin(err)
    return pos, s[pos], theta[pos], err[pos]<jupyter_output><empty_output><jupyter_text>Q17<jupyter_code># Q17和Q18
totalin = 0; totalout = 0
for i in range(5000):
    X, Y = generateData()
    theta = np.sort(X)
    s, theta, errin = decision_stump(X, Y)
    errout = 0.5+0.3*s*(math.fabs(theta)-1)
    totalin += errin
    totalout += errout
print('训练集平均误差: ', totalin/5000)
print('测试集平均误差: ', totalout/5000)
# Q19和Q20
X, Y = loadData('hw2_train.dat')
Xtest, Ytest = loadData('hw2_test.dat')
pos, s, theta, err = decision_stump_multi(X, Y)
print('训练集误差: ', err)
ypred = s*np.sign(Xtest[:, pos]-theta)
ypred[ypred == 0] = -1
row, col = Ytest.shape
errout = np.sum(ypred != Ytest.reshape(row,))/len(ypred)
print('测试集误差: ', errout)<jupyter_output>训练集误差:  0.25
测试集误差:  0.355
{'fraction_non_alphanumeric': 0.21670776818742293, 'fraction_numerical': 0.1032675709001233, 'mean_word_length': 3.9466463414634148, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '586744', 'license': 'MIT', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1541, 'n_words': 298, 'path': 'hw2/hw2.ipynb', 'repo_name': 'AceCoooool/MLF', 'size': '3265'}