<jupyter_start><jupyter_text>prepare data<jupyter_code># add intercept=1 for x0
X = np.insert(raw_X, 0, values=np.ones(raw_X.shape[0]), axis=1)
X.shape
# y have 10 categories here. 1..10, they represent digit 0 as category 10 because matlab index start at 1
# I'll ditit 0, index 0 again
y_matrix = []

for k in range(1, 11):
    y_matrix.append((raw_y == k).astype(int))

# last one is k==10, it's digit 0, bring it to the first position
y_matrix = [y_matrix[-1]] + y_matrix[:-1]
y = np.array(y_matrix)

y.shape<jupyter_output><empty_output><jupyter_text>train 1 model<jupyter_code>t0 = lr.logistic_regression(X, y[0])
print(t0.shape)
y_pred = lr.predict(X, t0)
print('Accuracy={}'.format(np.mean(y[0] == y_pred)))<jupyter_output>(401,)
Accuracy=0.9974<jupyter_text>Is this real......  train k model<jupyter_code>k_theta = np.array([lr.logistic_regression(X, y[k]) for k in range(10)])
print(k_theta.shape)<jupyter_output>(10, 401)<jupyter_text>making prediction* think about the shape of k_theta, now you are making $X\times\theta^T$> $(5000, 401) \times (10, 401).T = (5000, 10)$* after that, you run sigmoid to get probabilities and for each row, you find the highest prob as the answer<jupyter_code>prob_matrix = lr.sigmoid(X @ k_theta.T)
np.set_printoptions(suppress=True)
prob_matrix
y_pred = np.argmax(prob_matrix, axis=1)
y_answer = raw_y.copy()
y_answer[y_answer==10] = 0
print(classification_report(y_answer, y_pred))<jupyter_output>precision    recall  f1-score   support

          0       0.97      0.99      0.98       500
          1       0.95      0.99      0.97       500
          2       0.95      0.92      0.93       500
          3       0.95      0.91      0.93       500
          4       0.95      0.95      0.95       500
          5       0.92      0.92      0.92       500
          6       0.97      0.98      0.97       500
          7       0.95      0.95      0.95       500
          8       0.93      0.92      0.92       500
          9       0.92      0.92      0.92       500

avg / total       0.94      0.94      0.94      5000
{'fraction_non_alphanumeric': 0.11895551257253385, 'fraction_numerical': 0.10203094777562863, 'mean_word_length': 2.1879815100154083, 'pattern_counts': {'":': 0, '<': 14, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '439659', 'license': 'Apache-2.0', 'n_tokens_mistral': 881, 'n_tokens_neox': 770, 'n_words': 199, 'path': 'machinelearning/Coursera-ML-AndrewNg/pythonversion/ex3-neural network/2- one vs all logistic regression.ipynb', 'repo_name': 'Asurada2015/machinelearningpractice', 'size': '2068'}