爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 21969|回复: 7

[源代码] Python计算相关系数,一个是时间序列和格点的相关,一个是空间相关

[复制链接]

新浪微博达人勋

发表于 2021-1-14 22:32:50 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 洗雨 于 2021-10-20 08:48 编辑

纯属瞎折腾,希望得到指点
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Jan 13 16:56:43 2021

  4. @author: gaoyong
  5. """
  6. import xarray as xr
  7. import numpy as np
  8. import pandas as pd
  9. from scipy import stats
  10. import calendar as cal



  11. def time_series(filename,yearstart,monthstart,yearend,monthend):
  12.     file2 = pd.read_csv(filename,sep='\s+')
  13.     ser   = []
  14.   
  15.     for j in range(yearstart,yearend+1):
  16.         for i in range(monthstart,monthend+1):
  17.             imonth = cal.month_abbr[i]
  18.             ser.append(file2[imonth][j])
  19.     return ser

  20. def grid_series_corr(ncfilename,ser):
  21.     data = xr.open_dataset(ncfilename)
  22.     ser  = np.array(ser)
  23.     yl,ml,latl,lonl = data.pre.shape
  24.     data_new        = np.array((data.pre.values).reshape(yl*ml,latl*lonl))
  25.     latmin          = data.lat.min()
  26.     latmax          = data.lat.max()
  27.     lonmin          = data.lon.min()
  28.     lonmax          = data.lon.max()
  29.     lats            = np.linspace(latmax,latmin,latl)
  30.     lons            = np.linspace(lonmin,lonmax,lonl)
  31.     rr              = []
  32.     for i in range(latl*lonl):
  33.         r = np.corrcoef(np.array(data_new[:,i]),ser)[0,1]
  34.         rr.append(r)
  35.     lons,lats = np.meshgrid(lons,lats)
  36.     rr        = np.array(rr).reshape(lonl,latl)
  37.     return rr,lons,lats

  38. def spacc_corr(data1,data2):
  39.     '''data1和data2为2维数组,数据维度相同,还没写完,慎用'''
  40.     data1 = np.array(data1)
  41.     data2 = np.array(data2)
  42.     x,y  = data1.shape
  43.     co = []
  44.     for i in range(y):
  45.         r  = np.corrcoef(data1[:,i],data2[:,i])[0,1]
  46.         co.append(r)
  47.     co = np.array(co).reshape(x,y)
  48.     return co

  49. if __name__ == '__main__':
  50.     file   = r'E:\tem_test\tw\lqx\pre.mon.1961-2018.nc'
  51.     file2  = r'E:\tem_test\tw\lqx\AOI.txt'


  52.     ser = time_series(file2,1961,1,2018,12)
  53.     a,lons,lats   = grid_series_corr(file,ser)
  54.     print(a)
复制代码


tcorr.py

1.78 KB, 下载次数: 12, 下载积分: 金钱 -5

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2021-10-4 17:20:56 | 显示全部楼层
def spacc_corr(data1,data2): ##空间相关系数
    '''data1和data2为2维数组,数据维度相同'''
    data1 = np.array(data1)
    data2 = np.array(data2)
    x,y  = data1.shape
    data1.reshape(1,x*y)
    data2.reshape(1,x*y)
    r  = np.corrcoef(data1,data2)[0,1]
    return r
不知道空间相关系数改成这个对不对?
密码修改失败请联系微信:mofangbao
回复 支持 0 反对 1

使用道具 举报

新浪微博达人勋

发表于 2021-1-15 08:37:55 | 显示全部楼层
谢谢分享,看看
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2021-1-16 21:45:16 | 显示全部楼层
dongyi 发表于 2021-1-15 08:37
谢谢分享,看看

这个不是完整版,没有做检验,谨慎使用
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-1-17 02:02:13 | 显示全部楼层
发个带有示例数据的完整版呗
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-1-22 16:32:48 | 显示全部楼层
现在也在做这方面的内容,学习
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-11-30 15:20:37 | 显示全部楼层
  r  = np.corrcoef(data1[:,i],data2[:,i])[0,1]

请问都后面的[0,1]是什么意思?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-11-30 16:29:16 | 显示全部楼层
shower107 发表于 2021-10-4 17:20
def spacc_corr(data1,data2): ##空间相关系数
    '''data1和data2为2维数组,数据维度相同'''
    data1 ...

  r  = np.corrcoef(data1[:,i],data2[:,i])[0,1]

请问都后面的[0,1]是什么意思?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表