CN海棠依旧 2021-03-26 21:07 采纳率: 50%

# 数据处理时出现'float' object has no attribute 'shape'怎么办？

``````import pandas as pd
import pylab as plt
from netCDF4 import Dataset
import numpy as np
file_0 = 'G:\\Data\\TP_and_2mT_1950-1978_Monthly.nc'
file_A = 'G:\\Data\\cru_ts4.04.1901.2019.tmp.dat.nc'
a = Dataset(file_0)
b = Dataset(file_A)
t2m = a.variables["t2m"][:]
tmp = b.variables["tmp"][:]
t2m = t2m[-1]
num = 1
num0 = 1
for i in range(720):
t2m = np.delete(t2m, num, axis=1)
num = num + 1
for i in range(360):
t2m = np.delete(t2m, num0, axis=0)
num0 = num0 + 1
t2m = np.delete(t2m, -1, axis=0)
t2m = t2m - 273.15
t2m_xin = []
for i in range(0, len(t2m)):
for j in t2m[i]:
t2m_xin.append(j)
tmp = tmp[935]
tmp_new = []
for i in range(0, len(tmp)):
for j in tmp[i]:
tmp_new.append(j)
era5_list = t2m_xin
cru_ts_list = tmp_new

g_s_m = pd.Series(era5_list)  # 利用Series将列表转换成新的、pandas可处理的数据
g_a_d = pd.Series(cru_ts_list)

corr_gust = round(g_s_m.corr(g_a_d), 4)  # 计算标准差，round(a, 4)是保留a的前四位小数

print('corr_gust :', corr_gust)
# 最后画一下两列表散点图，直观感受下，结合相关系数揣摩揣摩
plt.scatter(era5_list, cru_ts_list)
plt.title('corr_gust :' + str(corr_gust), fontproperties='SimHei')  # 给图写上title
plt.show()``````

Traceback (most recent call last):
File "G:\Data_dispose\CC.py", line 37, in <module>
corr_gust = round(g_s_m.corr(g_a_d), 4)  # 计算标准差，round(a, 4)是保留a的前四位小数
File "C:\Users\Liu Huageng\AppData\Roaming\Python\Python39\site-packages\pandas\core\series.py", line 2327, in corr
return nanops.nancorr(
File "C:\Users\Liu Huageng\AppData\Roaming\Python\Python39\site-packages\pandas\core\nanops.py", line 71, in _f
return f(*args, **kwargs)
File "C:\Users\Liu Huageng\AppData\Roaming\Python\Python39\site-packages\pandas\core\nanops.py", line 1459, in nancorr
return f(a, b)
File "C:\Users\Liu Huageng\AppData\Roaming\Python\Python39\site-packages\pandas\core\nanops.py", line 1480, in func
return np.corrcoef(a, b)[0, 1]
File "<__array_function__ internals>", line 5, in corrcoef
File "C:\Users\Liu Huageng\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\function_base.py", line 2551, in corrcoef
c = cov(x, y, rowvar)
File "<__array_function__ internals>", line 5, in cov
File "C:\Users\Liu Huageng\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\function_base.py", line 2456, in cov
avg, w_sum = average(X, axis=1, weights=w, returned=True)
File "<__array_function__ internals>", line 5, in average
File "C:\Users\Liu Huageng\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\function_base.py", line 415, in average
if scl.shape != avg.shape:
AttributeError: 'float' object has no attribute 'shape'

``````import pandas as pd
import pylab as plt
from netCDF4 import Dataset
import numpy as np
file_0 = 'G:\\Data\\TP_and_2mT_1950-1978_Monthly.nc'
file_A = 'G:\\Data\\cru_ts4.04.1901.2019.tmp.dat.nc'
a = Dataset(file_0)
b = Dataset(file_A)
t2m = a.variables["t2m"][:]
tmp = b.variables["tmp"][:]
t2m = t2m[-1]
num = 1
num0 = 1
for i in range(720):
t2m = np.delete(t2m, num, axis=1)
num = num + 1
for i in range(360):
t2m = np.delete(t2m, num0, axis=0)
num0 = num0 + 1
t2m = np.delete(t2m, -1, axis=0)
t2m = t2m - 273.15
t2m_xin = []
for i in range(0, len(t2m)):
for j in t2m[i]:
t2m_xin.append(j)
tmp = tmp[935]
tmp_new = []
for i in range(0, len(tmp)):
for j in tmp[i]:
tmp_new.append(j)
tmp_new_0 = []
for k in range(len(tmp_new)):
k = float(k)
tmp_new_0.append(k)
era5_list = t2m_xin
cru_ts_list = tmp_new_0

g_s_m = pd.Series(era5_list)  # 利用Series将列表转换成新的、pandas可处理的数据
g_a_d = pd.Series(cru_ts_list)

corr_gust = round(g_s_m.corr(g_a_d), 4)  # 计算标准差，round(a, 4)是保留a的前四位小数

print('corr_gust :', corr_gust)
# 最后画一下两列表散点图，直观感受下，结合相关系数揣摩揣摩
plt.scatter(era5_list, cru_ts_list)
plt.title('corr_gust :' + str(corr_gust), fontproperties='SimHei')  # 给图写上title
plt.show()``````

• 写回答

#### 5条回答默认 最新

• 歇歇 2021-03-27 14:34
关注

g_s_m.corr(g_a_d),类型转换一下

g_s_m = pd.Series(era5_list，dtype=np.float64)

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 nslt的可用模型，或者其他可以进行推理的现有模型
• ¥15 arduino上连sim900a实现连接mqtt服务器