import os import pandas as pd from sklearn.neighbors import KNeighborsRegressor from sklearn.metrics import r2_score # 读取第一个文件夹中的所有csv文件 folder1_path = "/path/to/folder1" files1 = os.listdir(folder1_path) dfs1 = [] for file1 in files1: if file1.endswith(".csv"): file1_path = os.path.join(folder1_path, file1) df1 = pd.read_csv(file1_path, usecols=[1,2,3,4]) dfs1.append(df1) # 将第一个文件夹中的所有数据合并为一个DataFrame df_X = pd.concat(dfs1, ignore_index=True) # 读取第二个文件夹中的所有csv文件 folder2_path = "/path/to/folder2" files2 = os.listdir(folder2_path) dfs2 = [] for file2 in files2: if file2.endswith(".csv"): file2_path = os.path.join(folder2_path, file2) df2 = pd.read_csv(file2_path, usecols=[1]) dfs2.append(df2) # 将第二个文件夹中的所有数据合并为一个DataFrame df_X["X5"] = pd.concat(dfs2, ignore_index=True) # 读取第三个文件夹中的所有csv文件 folder3_path = "/path/to/folder3" files3 = os.listdir(folder3_path) dfs3 = [] for file3 in files3: if file3.endswith(".csv"): file3_path = os.path.join(folder3_path, file3) df3 = pd.read_csv(file3_path, usecols=[2,6]) dfs3.append(df3) # 将第三个文件夹中的所有数据合并为一个DataFrame df_y = pd.concat(dfs3, ignore_index=True) # 训练k邻近回归模型 k = 5 model = KNeighborsRegressor(n_neighbors=k) model.fit(df_X, df_y) # 读取测试数据 test_folder_path = "/path/to/test/folder" test_files = os.listdir(test_folder_path) test_dfs = [] for test_file in test_files: if test_file.endswith(".csv"): test_file_path = os.path.join(test_folder_path, test_file) test_df = pd.read_csv(test_file_path, usecols=[1,2,3,4]) test_dfs.append(test_df) # 将测试数据合并为一个DataFrame test_X = pd.concat(test_dfs, ignore_index=True) # 对测试数据进行预测 test_y_pred = model.predict(test_X) # 计算模型准确率 test_y_true = pd.read_csv(test_file_path, usecols=[2,6]).values r2 = r2_score(test_y_true, test_y_pred) print("模型准确率:", r2)这段代码为什么不划分训练集和测试集进行训练再做预测
时间: 2024-04-01 11:37:26 浏览: 102
这段代码没有划分训练集和测试集进行训练再做预测,而是直接将所有数据合并为一个DataFrame进行训练和预测。可能是因为数据量比较小,划分训练集和测试集不会影响模型的性能评估,或者是因为数据量比较大,划分训练集和测试集会增加代码的复杂度和运行时间,而合并所有数据后训练和预测可以简化代码,并且可以利用更多的数据训练模型,提高模型的泛化能力。
然而,在实际应用中,为了有效评估模型的性能,一般还是需要将数据集划分为训练集和测试集进行训练和预测,并采用交叉验证等方法来进一步验证模型的鲁棒性和泛化能力。
相关问题
k邻近算法如何就一个文件夹的所有csv文件的第2到第5列作为自变量1、2、3、4,以另一个文件夹的所有csv文件的第2列作为自变量5,再以第三个文件夹的所有csv文件的第3列和第7列作为因变量,如何进行回归分析和预测,并输出模型准确率
以下是将k邻近算法应用于这个问题的示例代码:
```
import os
import pandas as pd
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import r2_score
# 读取第一个文件夹中的所有csv文件
folder1_path = "/path/to/folder1"
files1 = os.listdir(folder1_path)
dfs1 = []
for file1 in files1:
if file1.endswith(".csv"):
file1_path = os.path.join(folder1_path, file1)
df1 = pd.read_csv(file1_path, usecols=[1,2,3,4])
dfs1.append(df1)
# 将第一个文件夹中的所有数据合并为一个DataFrame
df_X = pd.concat(dfs1, ignore_index=True)
# 读取第二个文件夹中的所有csv文件
folder2_path = "/path/to/folder2"
files2 = os.listdir(folder2_path)
dfs2 = []
for file2 in files2:
if file2.endswith(".csv"):
file2_path = os.path.join(folder2_path, file2)
df2 = pd.read_csv(file2_path, usecols=[1])
dfs2.append(df2)
# 将第二个文件夹中的所有数据合并为一个DataFrame
df_X["X5"] = pd.concat(dfs2, ignore_index=True)
# 读取第三个文件夹中的所有csv文件
folder3_path = "/path/to/folder3"
files3 = os.listdir(folder3_path)
dfs3 = []
for file3 in files3:
if file3.endswith(".csv"):
file3_path = os.path.join(folder3_path, file3)
df3 = pd.read_csv(file3_path, usecols=[2,6])
dfs3.append(df3)
# 将第三个文件夹中的所有数据合并为一个DataFrame
df_y = pd.concat(dfs3, ignore_index=True)
# 训练k邻近回归模型
k = 5
model = KNeighborsRegressor(n_neighbors=k)
model.fit(df_X, df_y)
# 读取测试数据
test_folder_path = "/path/to/test/folder"
test_files = os.listdir(test_folder_path)
test_dfs = []
for test_file in test_files:
if test_file.endswith(".csv"):
test_file_path = os.path.join(test_folder_path, test_file)
test_df = pd.read_csv(test_file_path, usecols=[1,2,3,4])
test_dfs.append(test_df)
# 将测试数据合并为一个DataFrame
test_X = pd.concat(test_dfs, ignore_index=True)
# 对测试数据进行预测
test_y_pred = model.predict(test_X)
# 计算模型准确率
test_y_true = pd.read_csv(test_file_path, usecols=[2,6]).values
r2 = r2_score(test_y_true, test_y_pred)
print("模型准确率:", r2)
```
在这个示例代码中,我们首先读取了三个文件夹中的数据,并将它们分别合并为三个DataFrame。然后,我们使用k邻近回归模型对自变量(第2到第5列和第2列)与因变量(第3列和第7列)进行回归分析。对于测试数据,我们读取了另一个文件夹中的所有csv文件,并将它们合并为一个DataFrame。我们使用训练好的模型对测试数据进行预测,并计算模型的准确率,最后输出模型准确率。
阅读全文
相关推荐












