<jupyter_start><jupyter_text>04 向量化运算<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
x = np.array([1., 2., 3., 4., 5.])
y = np.array([1., 3., 2., 3., 5.])
from playML.SimpleLinearRegression import SimpleLinearRegression1

reg1 = SimpleLinearRegression1()
reg1.fit(x, y)
y_hat1 = reg1.predict(x)
plt.scatter(x, y)
plt.plot(x, y_hat1, color='r')
plt.axis([0, 6, 0, 6])
plt.show()<jupyter_output><empty_output><jupyter_text>向量化实现SimpleLinearRegression代码参见 [这里](playML/SimpleLinearRegression.py)<jupyter_code>from playML.SimpleLinearRegression import SimpleLinearRegression2

reg2 = SimpleLinearRegression2()
reg2.fit(x, y)
reg2.a_
reg2.b_
y_hat2 = reg2.predict(x)
plt.scatter(x, y)
plt.plot(x, y_hat2, color='r')
plt.axis([0, 6, 0, 6])
plt.show()<jupyter_output><empty_output><jupyter_text>向量化实现的性能测试<jupyter_code>m = 1000000
big_x = np.random.random(size=m)
big_y = big_x * 2 + 3 + np.random.normal(size=m)

%timeit reg1.fit(big_x, big_y)
%timeit reg2.fit(big_x, big_y)
reg1.a_
reg1.b_
reg2.a_
reg2.b_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1577437858508604, 'fraction_numerical': 0.04875717017208413, 'mean_word_length': 6.642335766423358, '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': '545969', 'license': 'Apache-2.0', 'n_tokens_mistral': 513, 'n_tokens_neox': 484, 'n_words': 81, 'path': '05-Linear-Regression/04-Vectorization/04-Vectorization.ipynb', 'repo_name': 'mtianyan/Mtianyan-Play-with-Machine-Learning-Algorithms', 'size': '1046'}