Exemplo n.º 1
0
def makeSpatialVx(X, Xhat, thresholds, loc, projection, subset, timevals,
                  reggrid, Map, locbyrow, fieldtype, units, dataname, obsname,
                  modelname, q, qs):
    #参数:X, Xhat, threshold, loc, projection, subset, timevals,reggrid, map, locbyrow, fieldtype, units, dataname, obsname, modelname, q, qs
    '''
    #初始化参数
    thresholds = np.array([0.01, 20.01])    #清空threshold
    loc = None
    projection = False
    subset = None
    timevals = None
    reggrid = True    #logical, is the grid a regular grid?
    map = False    #logical, should a map be added to image plots of the data?
    locbyrow = False
    fieldtype = ""
    units = ""
    dataname = ""
    obsname = "X"
    modelname = "Xhat"    
    q = (0, 0.1, 0.25, 0.33, 0.5, 0.66, 0.75, 0.9, 0.95)
    qs = None
    '''

    #if (X.shape == None):
    if (type(X) == list):  #判断X是否为列表,X实际为二维矩阵
        nobs = X.shape[0] * X.shape[1]
        xdim = None
    else:
        nobs = 1
        xdim = X.shape

    #if (Xhat.shape == None):
    if (type(Xhat) == list):  #判断X是否为列表
        nforecast = Xhat.shape[0] * Xhat.shape[1]
        ydim = None
    else:
        nforecast = 1
        ydim = Xhat.shape

    if (xdim != ydim):
        try:
            sys.exit(0)
        except:
            print(
                "make.SpatialVx: dim of X{0}  must be the same as dim of Xhat{1} "
                .format(X.shape, Xhat.shape))

    out = {"X": X, "Xhat": Xhat}  #将矩阵存为列表

    if (all(thresholds) == None):
        if (nobs > 1):  #猜测是输入的观测数据大于一个时次,对每个时次的数据分别求分位数
            othresh = np.zeros((9, 1))  #定义othresh为空数组,长度与q一致,9
            for i in range(1, nobs):
                #X_1 = np.array(X)
                #X_flat = X_1.flatten()    #dataframe没有flatten属性,先转array
                quantile_X = np.quantile(X, q)  #计算X的分位数
                othresh = np.hstack([quantile_X, othresh])
        else:
            othresh = np.quantile(X, q)  #数据大小为(9,1)是写死的,因为所要求的的分位数的个数是确定的

        if (nforecast > 1):  #猜测是输入的观测数据大于一个时次,对每个时次的数据分别求分位数
            fthresh = np.zeros()  #定义fthresh为空数组,长度未知
            for i in range(1, nforecast):
                quantile_Xhat = np.quantile(Xhat, q)  #计算Xhat的q分位数
                fthresh = np.hstack([fthresh,
                                     quantile_Xhat])  #原函数里面直接生成的矩阵,没有赋值到任何变量
        else:
            fthresh = np.quantile(Xhat, q)  #数据大小为(9,1)是写死的,因为所要求的的分位数的个数是确定的
        qs = str(q)
        X = othresh
        Xhat = fthresh
        thresholds = [X, Xhat]

    elif (type(thresholds) == list):  #判断thresholds是否为一个list
        threshnames = ["X", "Xhat"]  #获取列表的名称
        if (("X" not in threshnames) or ("Xhat" not in threshnames)):
            try:
                sys.exit(0)
            except:
                print(
                    "make.SpatialVx: invalid thresholds argument.  List must have components named X and Xhat."
                )

        odim = np.array([len(thresholds),
                         np.ndim(thresholds)])  #thresholds 第一项(X)的数组长度赋值给odim
        fdim = np.array([len(thresholds), np.ndim(thresholds)
                         ])  #thresholds 第二项(Xhat)的数组长度赋值给fdim

        if (odim[0] != fdim[0]):
            try:
                sys.exit(0)
            except:
                print(
                    "make.SpatialVx: invalid thresholds argument.  X must have same number of thresholds (rows) as Xhat."
                )
        nth = odim[0]

        if (any(odim) == None):
            thresholds[0] = np.array(thresholds[0]).reshape(
                len(thresholds[0]), nobs)
        elif (odim[1] == 1 and nobs > 1):
            thresholds[0] = np.array(thresholds[0]).reshape(
                len(thresholds[0]), nobs)
        elif (odim[1] > 1 and odim[1] != nobs):
            try:
                sys.exit(0)
            except:
                print("make.SpatialVx: invalid thresholds argument.")
        threshold = 'threshold'
        qs = []
        for i in range(nth):
            qs.append(threshold + ' ' + str(i))

    elif (
            type(thresholds) == list
    ):  #判断thresholds是否为vector,R里面检查是否最多只有一个属性:name。即查看其属性,如果没有属性或者只有一个name属性,才返回TRUE

        qs = str(thresholds)
        nth = len(thresholds)
        thresholds = [
            np.array(thresholds).reshape(nth, nobs),
            np.array(thresholds).reshape(nth, nforecast)
        ]
        #else stop("make.SpatialVx: invalid thresholds argument.  Must be a vector or list.")
    else:
        try:
            sys.exit(0)
        except:
            print(
                "make.SpatialVx: invalid thresholds argument.  Must be a vector or list."
            )

    if (any(loc) == None):  #判断loc是否为空
        rep00_1 = np.arange(1, xdim[0], 1)
        rep00 = np.tile(rep00_1, xdim[1])
        rep0_1 = np.arnge(1, xdim[1], 1)
        rep0 = rep0_1.repeat(xdim[0], axis=0)  #按行进行元素重复
        loc = np.vstack(rep00, rep0)
    if (timevals == None):
        if (len(xdim) == 3):
            timevals = np.arnge(1, xdim[2])
        else:
            timevals = 1
    if (dataname != None):
        msg = dataname
    else:
        msg = ""
    if (fieldtype != "" and units != ""):
        msg = msg + '\n' + fieldtype + ' ' + '(' + units + ')'
    elif (fieldtype != ""):
        msg = msg + '\n' + fieldtype
    elif (units != ""):
        msg = msg + ' ' + '(' + units + ')'


    out = {"X": X, "Xhat": Xhat, "class":"SpatialVx", "xdim":xdim, "time":timevals,\
               "thresholds":thresholds, "loc":loc, "locbyrow":locbyrow, \
               "subset":subset, "dataname":dataname, "obsname":obsname, \
               "modelname":modelname, "nobs":nobs, "nforecast":nforecast, \
               "fieldtype":fieldtype , "units":units , "projection":projection , \
               "reggrid":reggrid , "Map":Map , "qs":qs, "msg":msg}
    return out
Exemplo n.º 2
0
            print("No date found for ind: ", ind) 
        try:
            if len(record["MedlineCitation"]["Article"]["ArticleTitle"].split(COI)) > 1:
                yearcolondict[year].appepnd(1)
            else:
                yearcolondict[year].append(0)
        except:
            print("The next try statement didn"t work.") 

# Plot.
fig = plt.figure()
ax = fig.add_subplot(111)
x = []; y = []
for year in yearcolondict.keys():
    totescolons = float(sum(yearcolondict[year]))
    pubsthisyear = float(len(yearcolondict[year]))
    ax.scatter(year, totescolons/pubsthisyear, c="k", s=80, edgecolor="w")
    x.append(year)
    y.append(totescolons/pubsthisyear)

# Linear regression
slope, intercept, rvalue, pvalue, stderr = stats.linregress(x, y)
xacls = np.arnge(min(x), max(x), 1)
yvals = slope*xvals + intercept
ax.plot(xvals, yvals, c="k")
ax.set_title("Proportions per year for search term " + searchterm + ", slope =" + str(slope)[:6] + "/yr", style="italic")
ax.set_xlabel("Year", style="italic")
ax.set_ylabel("Proportion of the titles containing search term", style="italic")
ax.set_ylim([0, .55])
plt.savefig("searchterms.png")
Exemplo n.º 3
0
def make_spatialVx(grd_ob,
                   grd_fo,
                   loc,
                   fieldtype="",
                   units="",
                   dataname="",
                   obsname="ob",
                   modelname="fo"):
    #参数:X, Xhat, threshold, loc, projection, subset, timevals,reggrid, map, locbyrow, fieldtype, units, dataname, obsname, modelname, q, qs
    thresholds = [None, None]  #画图阈值
    ob = grd_ob
    fo = grd_fo
    projection = False
    subset = None
    timevals = None
    reggrid = True
    Map = False
    locbyrow = False
    qs = None
    q = (0, 0.1, 0.25, 0.33, 0.5, 0.66, 0.75, 0.9, 0.95)

    #if (X.shape == None):
    if (type(ob) == list):  #判断X是否为列表,X实际为二维矩阵
        nobs = ob.shape[0] * ob.shape[1]
        xdim = None
    else:
        nobs = 1
        xdim = ob.shape

    #if (Xhat.shape == None):
    if (type(fo) == list):  #判断X是否为列表
        nforecast = fo.shape[0] * fo.shape[1]
        ydim = None
    else:
        nforecast = 1
        ydim = fo.shape

    if (xdim != ydim):
        try:
            sys.exit(0)
        except:
            print(
                "make.SpatialVx: dim of ob{0}  must be the same as dim of fo{1} "
                .format(ob.shape, fo.shape))

    out = {"ob": ob, "fo": fo}  #将矩阵存为列表

    if (all(thresholds) == None):
        if (nobs > 1):  #猜测是输入的观测数据大于一个时次,对每个时次的数据分别求分位数
            othresh = np.zeros((9, 1))  #定义othresh为空数组,长度与q一致,9
            for i in range(1, nobs):
                #X_1 = np.array(X)
                #X_flat = X_1.flatten()    #dataframe没有flatten属性,先转array
                quantile_X = np.quantile(ob, q)  #计算X的分位数
                othresh = np.hstack([quantile_X, othresh])
        else:
            othresh = np.quantile(ob, q)  #数据大小为(9,1)是写死的,因为所要求的的分位数的个数是确定的

        if (nforecast > 1):  #猜测是输入的观测数据大于一个时次,对每个时次的数据分别求分位数
            fthresh = np.zeros()  #定义fthresh为空数组,长度未知
            for i in range(1, nforecast):
                quantile_Xhat = np.quantile(fo, q)  #计算Xhat的q分位数
                fthresh = np.hstack([fthresh,
                                     quantile_Xhat])  #原函数里面直接生成的矩阵,没有赋值到任何变量
        else:
            fthresh = np.quantile(fo, q)  #数据大小为(9,1)是写死的,因为所要求的的分位数的个数是确定的
        qs = str(q)
        #X = othresh
        #Xhat = fthresh
        thresholds = [othresh, fthresh]

    elif (type(thresholds) == list):  #判断thresholds是否为一个list
        threshnames = ["ob", "fo"]  #获取列表的名称
        if (("ob" not in threshnames) or ("fo" not in threshnames)):
            try:
                sys.exit(0)
            except:
                print(
                    "make.SpatialVx: invalid thresholds argument.  List must have components named ob and fo."
                )

        odim = np.array([len(thresholds),
                         np.ndim(thresholds)])  #thresholds 第一项(X)的数组长度赋值给odim
        fdim = np.array([len(thresholds), np.ndim(thresholds)
                         ])  #thresholds 第二项(Xhat)的数组长度赋值给fdim

        if (odim[0] != fdim[0]):
            try:
                sys.exit(0)
            except:
                print(
                    "make.SpatialVx: invalid thresholds argument.  ob must have same number of thresholds (rows) as fo."
                )
        nth = odim[0]

        if (any(odim) == None):
            thresholds[0] = np.array(thresholds[0]).reshape(
                len(thresholds[0]), nobs)
        elif (odim[1] == 1 and nobs > 1):
            thresholds[0] = np.array(thresholds[0]).reshape(
                len(thresholds[0]), nobs)
        elif (odim[1] > 1 and odim[1] != nobs):
            try:
                sys.exit(0)
            except:
                print("make.SpatialVx: invalid thresholds argument.")
        threshold = 'threshold'
        qs = []
        for i in range(nth):
            qs.append(threshold + ' ' + str(i))

    elif (
            type(thresholds) == list
    ):  #判断thresholds是否为vector,R里面检查是否最多只有一个属性:name。即查看其属性,如果没有属性或者只有一个name属性,才返回TRUE

        qs = str(thresholds)
        nth = len(thresholds)
        thresholds = [
            np.array(thresholds).reshape(nth, nobs),
            np.array(thresholds).reshape(nth, nforecast)
        ]
        #else stop("make.SpatialVx: invalid thresholds argument.  Must be a vector or list.")
    else:
        try:
            sys.exit(0)
        except:
            print(
                "make.SpatialVx: invalid thresholds argument.  Must be a vector or list."
            )

    if (loc.any() == None):  #判断loc是否为空,如果为空的话,生成坐标信息
        rep00_1 = np.arange(1, xdim[0], 1)
        rep00 = np.tile(rep00_1, xdim[1])
        rep0_1 = np.arange(1, xdim[1], 1)
        rep0 = rep0_1.repeat(xdim[0], axis=0)  #按行进行元素重复
        loc = np.vstack((rep00, rep0))
    if (timevals == None):
        if (len(xdim) == 3):
            timevals = np.arnge(1, xdim[2])
        else:
            timevals = 1
    if (dataname != None):
        msg = dataname
    else:
        msg = ""
    if (fieldtype != "" and units != ""):
        msg = msg + '\n' + fieldtype + ' ' + '(' + units + ')'
    elif (fieldtype != ""):
        msg = msg + '\n' + fieldtype
    elif (units != ""):
        msg = msg + ' ' + '(' + units + ')'
    '''   
    out = {"ob": ob, "fo": fo, "class":"SpatialVx", "xdim":xdim, "time":timevals,
               "thresholds":thresholds, "loc":loc, "locbyrow":locbyrow, 
               "subset":subset, "dataname":dataname, "obsname":obsname, 
               "modelname":modelname, "nobs":nobs, "nforecast":nforecast, 
               "fieldtype":fieldtype , "units":units , "projection":projection , 
               "reggrid":reggrid , "Map":Map , "qs":qs, "msg":msg}
    '''
    out = {
        "grd_ob": ob,
        "grd_fo": fo,
        "class": "SpatialVx",
        "xdim": xdim,
        "loc": loc,
        "dataname": dataname,
        "obsname": obsname,
        "modelname": modelname,
        "fieldtype": fieldtype,
        "units": units,
        "msg": msg
    }

    return out
Exemplo n.º 4
0
b = pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])
b

c = pd.Series(25, index=['a', 'b', 'c'])
c

a = pd.Series({"a": 1, "b": 2, "c": 3, "d": 4})
print(a)
print(a[1])
print(a["b"])

d = pd.Series(range(20))
print(d)
d.cumsum()

e = pd.Series(np.arange(5))
e

n = pd.Series(np.arange(5), index=np.arnge(9, 4, -1))
n

k = pd.Series([1, 2, 3], ['c', 'd', 'e'])
v = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
k + v

v = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
v['b'] = 15
v.name = "Series"
v['a', 'c'] = 20
Exemplo n.º 5
0
import numpy as np
import matplotlib.pyplot as plt

# 다양한 그래프 그리기

# 343p

days = np.arnge(1, 11)
weight = ([10, 14, 16, 17, 13])

plt.plot(days,
         weight,
         marker="o",
         markerfacecolor="k",
         linestyle="--",
         color="b")

# 막대그래프 만들기

x = np.arange(1, 11)
y1 = np.arange(11, 21)
y2 = np.arange(5, 10)

labels = ([1, 2, 3, 4, 5])

plt.bar(x, y1, tick_label=labels)
plt.bar(x, y2, bottom=y1)  # 누적막대그래프

# 히스토그램

data = np.random.randn(1000)