def test_random(self): """ python -m unittest tests.test_numpy.NumpyTest.test_random :return: """ v: np.array = np.random.uniform(0., 1000., 5) coutln(v)
def test_zeros(self): """ Learn numpy.zeros() python -m unittest tests.test_numpy.NumpyTest.test_zeros """ v: np.ndarray = np.zeros((5, ), dtype=float, order='C') coutln(v)
def test_matrix(self): """ python -m unittest tests.test_numpy.NumpyTest.test_matrix :return: """ m = np.zeros((2, 2)) m[1, 1] += 5. coutln(m)
def test_datetime(self): """ python -m unittest tests.test_datetime.DatetimeTest.test_datetime :return: """ now = datetime.datetime.now() coutln(str(now)) coutln(now.strftime("%d/%m/%Y %I:%M:%S %p")) coutln(now.strftime("%-d/%-m/%Y %I:%M:%S %p"))
def test_searchsorted(self): """ python -m unittest tests.test_numpy.NumpyTest.test_searchsorted :return: """ a: np.array = np.linspace(0., 1000., 9) coutln(a) v: np.array = np.random.uniform(0., 1000., 10) coutln(v) i: np.array = np.searchsorted(a, v) coutln(i)
def test_linspace(self): """ Learn numpy.linspace() python -m unittest tests.test_numpy.NumpyTest.test_linspace :return: """ v: np.ndarray = np.linspace(0., 1000., 5) coutln('>>> np.linspace(0., 1000., 5)') coutln(v) coutln(v.dtype)
def test_arange(self): """ Learn numpy.arange() python -m unittest tests.test_numpy.NumpyTest.test_arange """ v: np.ndarray = np.arange(10) coutln('>>> np.arange(10)') coutln(v) coutln('>>> v.dtype') coutln(v.dtype) # 1 dim array self.assertEqual(1, v.ndim) # shape = (10,) self.assertEqual(1, len(v.shape)) self.assertEqual(10, v.shape[0]) self.assertEqual(10, v.size) l: list = list(range(0, 10)) self.assertTrue(np.array_equal(l, v))
def test_scatter_demo2(self): """ https://matplotlib.org/gallery/lines_bars_and_markers/scatter_demo2.html#sphx-glr-gallery-lines-bars-and-markers-scatter-demo2-py python -m unittest tests.test_matplotlib.MatplotlibTest.test_scatter_demo2 :return: """ with cbook.get_sample_data('goog.npz') as datafile: # coutln(datafile) data: np.lib.npyio.NpzFile = np.load(datafile) price_data: np.recarray = data['price_data'].view(np.recarray) # coutln('>>> price_data') # coutln(price_data) coutln('>>> type(price_data)') coutln(type(price_data)) coutln('>>> price_data.dtype') coutln(price_data.dtype) # coutln(price_data.view(np.recarray)) # coutln('>>> price_data.size') # coutln(price_data.size) # for i in data.keys(): # coutln(i) price_data = price_data[-250:] # coutln('>>> price_data') # coutln(price_data) coutln(">>> price_data.adj_close.size") coutln(price_data.adj_close.size) # price_data.adj_close[:-1]: first to second to the last delta1: np.ndarray = np.diff( price_data.adj_close) / price_data.adj_close[:-1] coutln(">>> delta1.size") coutln(delta1.size) volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2 coutln('volume.size: {}'.format(volume.size)) close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2] fig: matplotlib.figure.Figure ax: matplotlib.axes.Axes fig, ax = plt.subplots() ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5) ax.set_xlabel(r'$\Delta_i$', fontsize=15) ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15) ax.set_title('Volume and percent change') ax.grid(True) fig.tight_layout() plt.show()