Data Analysis (five)

Arithmetic mean

S = [s1, s2, ..., sn]

Each sample value is the true value and the error.

Arithmetic mean:
m = (S1 + S2 + ... + Sn) / n-

No it represents the arithmetic mean of an unbiased estimate of the true value.

np.mean(array)
array.mean()

Case: Calculate the arithmetic mean of the closing price.

#算数
import numpy as np
import matplotlib.pyplot as mp
import datetime as dt
import matplotlib.dates as md


def dmy2ymd(dmy):
  """
  把日月年转年月日
  :param day:
  :return:
  """
  dmy = str(dmy, encoding='utf-8')
  t = dt.datetime.strptime(dmy, '%d-%m-%Y')
  s = t.date().strftime('%Y-%m-%d')
  return s


dates, opening_prices, \
highest_prices, lowest_prices, \
closing_prices=\ 
  Np.loadtxt ( ' aapl.csv ' , 
             DELIMITER = ' , ' , 
             usecols = (. 1,. 3,. 4,. 5,. 6 ), 
             the unpack = True, 
             DTYPE = ' M8 [D], F8, F8, F8, F8 ' , 
             Converters = {. 1: dmy2ymd})   # DMY transfer date 
Print (a dates)
 # drawn closing price discount FIG 
mp.figure ( ' APPL ' , facecolor = ' LightGray ' ) 
mp.title (' APPL ' , 18 is fontSize = ) 
mp.xlabel ( ' a Date ' , fontSize = 14 ) 
mp.ylabel ( ' Price ' , fontSize = 14 ) 
mp.grid (lineStyle = " : " ) 

# Set scale locator 
# Mondays a main scale, a one day time scale 

AX = mp.gca () 
ma_loc = md.WeekdayLocator (byweekday = md.MO) 
ax.xaxis.set_major_locator (ma_loc) 
ax.xaxis.set_major_formatter (md.DateFormatter ( ' % Y-% % D M- ' )) 
ax.xaxis.set_minor_locator (md.DayLocator ())
 # 修改dates的dtype为md.datetime.datetiem
dates = dates.astype(md.datetime.datetime)
mp.plot(dates, closing_prices,
        color='dodgerblue',
        linewidth=2,
        linestyle='--',
        alpha=0.8,
        label='APPL Closing Price')
#计算收盘价的均值
mean = np.mean(closing_prices)
mean = closing_prices.mean()
mp.hlines(mean,dates[0],dates[-1],colors='orangered',
          label='mean')
mp.gcf().autofmt_xdate()
mp.show()

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11457290.html