import numpy as np import pandas as pd import xlrd import os import matplotlib.pyplot as plt def get_all_fill_paths(dir_path): file_paths = [] for root, _, files in os.walk(dir_path): for file in files: file_paths.append(os.path.join(root, file)) return file_paths dir_path = r'C:\Users\lxz15\Desktop\电流数据2' file_paths = get_all_fill_paths(dir_path) data = [] data = pd.DataFrame() # 定义一个空的 DataFrame all_a = [] for i, file_path in enumerate(file_paths): df = pd.read_excel(file_path) for j in range(0, 1): for k in range(0, 1): a = pd.DataFrame(df.iloc[2 + 3 * k:5 + 3 * k, 7 + j].values) # 将列表转换为 DataFrame data = pd.concat([data, a], axis=1) # 将 a 添加到 data 中 all_a.append(a) all_a = np.array(all_a) all_a_avg = np.mean(all_a) diff = all_a - all_a_avg # 计算差 diff_sum = np.sum(np.square(diff)) # 计算差的平方和 diff_sqrt = np.sqrt(diff_sum) print("差的平方和的平方根为:", diff_sqrt)怎么改
时间: 2024-02-05 13:03:17 浏览: 133
你可以将 `all_a_avg = np.mean(all_a)` 这一行代码修改为 `all_a_avg = np.mean(all_a, axis=(0, 1))`,指定均值计算的轴。这样就可以计算出 `all_a` 中所有元素的平均值。另外,由于 `all_a` 是一个数组,你需要使用 `np.square()` 和 `np.sqrt()` 函数来分别计算差的平方和和差的平方和的平方根,代码可以修改为:
```
import numpy as np
import pandas as pd
import xlrd
import os
import matplotlib.pyplot as plt
def get_all_fill_paths(dir_path):
file_paths = []
for root, _, files in os.walk(dir_path):
for file in files:
file_paths.append(os.path.join(root, file))
return file_paths
dir_path = r'C:\Users\lxz15\Desktop\电流数据2'
file_paths = get_all_fill_paths(dir_path)
data = pd.DataFrame() # 定义一个空的 DataFrame
all_a = []
for i, file_path in enumerate(file_paths):
df = pd.read_excel(file_path)
for j in range(0, 1):
for k in range(0, 1):
a = pd.DataFrame(df.iloc[2 + 3 * k:5 + 3 * k, 7 + j].values) # 将列表转换为 DataFrame
data = pd.concat([data, a], axis=1) # 将 a 添加到 data 中
all_a.append(a)
all_a = np.array(all_a)
all_a_avg = np.mean(all_a, axis=(0, 1)) # 计算所有元素的平均值
diff = all_a - all_a_avg
diff_sum = np.sum(np.square(diff))
diff_sqrt = np.sqrt(diff_sum)
print("差的平方和的平方根为:", diff_sqrt)
```
阅读全文