<jupyter_start><jupyter_text>Question 1<jupyter_code>output = pd.DataFrame()
for j in ['perceptron', 'voted', 'average']:
    train_err = []
    test_err = []
    for i in range(1, 5):
        clf = Perceptron(i, j)
        clf.fit(X_train_1_2, y_train_1_2)
        train_err += [1 - clf.score(X_train_1_2, y_train_1_2)]
        test_err += [1 - clf.score(X_test_1_2, y_test_1_2)]
    output[j,'training_error'] = train_err
    output[j,'test_error'] = test_err
output.index = range(1,5)
output.index.name = 'passes'
output<jupyter_output><empty_output><jupyter_text>Question 2<jupyter_code>clf = Perceptron(3, 'average')
clf.fit(X_train_1_2, y_train_1_2)
w_avg = np.zeros_like(X_train.iloc[0])
for i in range(len(clf.w)):
    w,c = clf.w[i]
    w_avg+=c*w
w_avg = w_avg.to_frame().reset_index()
w_avg.columns = ['word', 'value']
w_avg.index.name = 'index'
# highest three
print('words with highest coordinates')
display(w_avg.sort_values(by = 'value', ascending = False)[:3])

# lowest three
print('words with lowest coordinates')
display(w_avg.sort_values(by = 'value')[:3])
clf.classes<jupyter_output><empty_output><jupyter_text>Question 3<jupyter_code>C_list = []
for i in range(1,7):
    y = y_train.apply(lambda x: x if x == i else 99)
    clf = Perceptron(1, 'perceptron')
    clf.fit(X_train,y)
    C_list += [clf]
pred = np.zeros([y_test.shape[0], 6])
counter = 0
for i in C_list:
    pred[:,counter] = i.predict(X_test)
    counter += 1
np.place(pred, pred == 99, 0)
prediction = pd.DataFrame(pred).apply(lambda x: max(np.unique(x)) if len(np.unique(x)) == 2 else "Don't Know", axis = 1)
def confusion_matrix(y,predicted):
    df = pd.DataFrame({
    'predicted':prediction,
    'actual': y_test
    })
    
    return df.groupby('actual')['predicted'].value_counts(normalize = True).unstack().T.fillna(0)
confused_train = confusion_matrix(y_train,prediction)
display(confused_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16172646905876234, 'fraction_numerical': 0.022880915236609463, 'mean_word_length': 4.812688821752266, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '395663', 'license': 'MIT', 'n_tokens_mistral': 804, 'n_tokens_neox': 780, 'n_words': 140, 'path': 'perceptron/.ipynb_checkpoints/perceptron_performance-checkpoint.ipynb', 'repo_name': 'syeehyn/Machine-Learning-in-Practice', 'size': '1923'}