API_Net代码的train.txt与val.txt

import os

if __name__=="__main__":
    path = "/home/yjys/datasets/CUB_200_2011"
    train_test_file = "train_test_split.txt"
    image_class_label = "image_class_labels.txt"
    image = "images.txt"
    image_path = "/home/yjys/datasets/CUB_200_2011/images"

    image_name = []
    with open(os.path.join(path,image), 'r') as f:
        for line in f.readlines():
            img_name = os.path.join(image_path, line.strip("\n").split(" ")[1])
            image_name.append(img_name)

    train_test = []
    with open(os.path.join(path, train_test_file)) as f:
        for line in f.readlines():
            t_t = line.strip("\n").split(" ")[1]
            train_test.append(int(t_t))

    img_class_label = []
    with open(os.path.join(path, image_class_label)) as f:
        for line in f.readlines():
            c_l = line.strip("\n").split(" ")[1]
            img_class_label.append(int(c_l))

    with open("train.txt", 'w') as f:
        with open("val.txt", 'w') as fv:
            for index, name in enumerate(image_name):
                if train_test[index]:
                    f.write(name+" "+str(img_class_label[index]-1)+"\n")
                else:
                    fv.write(name+" "+str(img_class_label[index]-1)+"\n")


#IndexError: Target 200 is out of bounds.使用cpu炮程序后,出现的错误,也就是在计算🔠input和targets的损失函数的时候出现的错误
#RuntimeError: cuda runtime error (710) : device-side assert triggered 使用gpu炮程序,出现的错误,看不出来
``` import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn.impute import SimpleImputer from sklearn.pipeline import Pipeline # 读取训练数据 train_df = pd.read_excel(r'C:\Users\tengh\Desktop\3\测试\train.xlsx') # 数据预处理 # 删除无关列 drop_columns = [ '序号', '测量点编号', '测量点名称', '用户/台区编号', '用户/台区名称', '电表资产号', '表地址信息', '数据日期', '相序' ] train_df_clean = train_df.drop(columns=drop_columns) # 处理缺失值(用0填充) train_df_filled = train_df_clean.fillna(0) # 分割特征和目标 X = train_df_filled.drop(columns=['标签', '窃电判断']).values y_type = train_df_filled['标签'].map({'普通居民': 0, '农排灌溉': 1}) y_theft = train_df_filled['窃电判断'] # 划分训练集和验证集 X_train, X_val, y_type_train, y_type_val, y_theft_train, y_theft_val = train_test_split( X, y_type, y_theft, test_size=0.2, random_state=42 ) # 创建预处理和模型管道 pipeline = Pipeline([ ('imputer', SimpleImputer(strategy='constant', fill_value=0)), ('model', RandomForestClassifier(random_state=42)) ]) # 训练用户类型分类模型 model_type = pipeline.clone() model_type.fit(X_train, y_type_train) # 训练窃电判断模型 model_theft = pipeline.clone() model_theft.fit(X_train, y_theft_train) # 验证模型性能 print("用户类型分类报告:") print(classification_report(y_type_val, model_type.predict(X_val))) print("\n窃电判断分类报告:") print(classification_report(y_theft_val, model_theft.predict(X_val))) # 预测测试集 test_df = pd.read_excel(r'C:\Users\tengh\Desktop\3\测试\test.xlsx') test_df_clean = test_df.drop(columns=drop_columns) test_df_filled = test_df_clean.fillna(0) X_test = test_df_filled.values # 生成预测结果 type_predictions = model_type.predict(X_test) theft_predictions = model_theft.predict(X_test) # 保存结果到DataFrame results = pd.DataFrame({ '用户类型预测': ['普通居民' if pred == 0 else '农排灌溉' for pred in type_predictions], '窃电判断预测': theft_predictions }) # 输出结果到Excel results.to_excel('predictions.xlsx', index=False) print("\n预测结果已保存至 predictions.xlsx")```C:\Users\tengh\AppData\Local\Programs\Python\Python310\python.exe C:\Users\tengh\Desktop\3\5.6.py Traceback (most recent call last): File "C:\Users\tengh\Desktop\3\5.6.py", line 39, in <module> model_type = pipeline.clone() AttributeError: 'Pipeline' object has no attribute 'clone' 进程已结束,退出代码为 1
最新发布
03-29
仔细阅读下面【Python】代码,判断是否会运行报错? 如果会报错,请说明原因,并输出解决方法; 如果不会报错,请回答“无错误” 你需要处理的代码为:【 import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split, learning_curve from sklearn.naive_bayes import GaussianNB from sklearn.metrics import accuracy_score digits = load_digits() X, y = digits.data, digits.target print(f"输入特征维度: {X.shape}") print(f"目标变量维度: {y.shape}") X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=123) print("\n--- 数据划分情况 ---") print(f"训练集大小: {X_train.shape0} 条记录") print(f"测试集大小: {X_test.shape0} 条记录") def plot_learning_curve(estimator, title, X, y, ylim=None, cv=5) plt.figure(figsize=(8, 6)) plt.title(title) if ylim is not None: plt.ylim(*ylim) plt.xlabel("Training Examples") plt.ylabel("Score") train_sizes, train_scores, val_scores = learning_curve( estimator, X, y, cv=cv, scoring='accuracy', n_jobs=-1, train_sizes=np.linspace(0.1, 1.0, 10), shuffle=True, random_state=42) train_scores_mean = np.mean(train_scores, axis=1) val_scores_mean = np.mean(val_scores, axis=1) plt.grid() plt.plot(train_sizes, train_scores_mean, 'o-', color="r", label="Training Accuracy") plt.plot(train_sizes, val_scores_mean, 'o-', color="g", label="Cross-validation Accuracy") plt.legend(loc="best") return plt model = GaussianNB() plot_learning_curve(model, "Learning Curve of Gaussian Naive Bayes on Digits Dataset", X_train, y_train, cv=5) plt.show() 】
03-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值