Untitled

 avatar
unknown
plain_text
5 months ago
3.5 kB
1
Indexable

Tôi sẽ kiểm tra và sửa các lỗi tiềm ẩn trong tệp .arff. Dưới đây là kế hoạch:

So khớp các giá trị trong dữ liệu với các thuộc tính đã khai báo.
Sửa những dòng dữ liệu không khớp với định nghĩa danh mục (nominal).
Bây giờ tôi sẽ viết mã để tìm và sửa lỗi nếu có.


Phân tích bị lỗi

python
Luôn hiển thị chi tiết

Sao chép mã
# Let's inspect the attributes and validate the data against the definitions.
import re

# Extracting the attribute definitions and data section
attribute_section = []
data_section = []
is_data_section = False

# Splitting the file into attributes and data sections
for line in arff_content:
    if "@DATA" in line:
        is_data_section = True
    elif is_data_section:
        data_section.append(line.strip())
    else:
        attribute_section.append(line.strip())

# Parsing the attributes to identify nominal ones and their allowed values
nominal_attributes = {}
attribute_names = []

attribute_pattern = re.compile(r"@ATTRIBUTE\s+(\w+)\s+\{([^}]+)\}")

for line in attribute_section:
    match = attribute_pattern.match(line)
    if match:
        attr_name = match.group(1)
        allowed_values = [val.strip() for val in match.group(2).split(",")]
        nominal_attributes[attr_name] = allowed_values
    elif "@ATTRIBUTE" in line:
        # Keep track of attribute names in order of appearance
        attr_name = line.split()[1]
        attribute_names.append(attr_name)

# Converting data section to list of lists for validation and correction
data_rows = [line.split(",") for line in data_section if line]

# Correcting invalid nominal values if found
corrections = 0
for row in data_rows:
    for idx, value in enumerate(row):
        attribute_name = attribute_names[idx]

        # If the attribute is nominal, check its validity
        if attribute_name in nominal_attributes:
            allowed_values = nominal_attributes[attribute_name]
            if value not in allowed_values:
                # Try to correct by picking the closest valid value
                row[idx] = allowed_values[0]  # Default to first valid value
                corrections += 1

# Reconstruct the ARFF content with corrections
corrected_data = "\n".join([",".join(row) for row in data_rows])
corrected_arff_content = "\n".join(attribute_section) + "\n@DATA\n" + corrected_data

# Saving the corrected ARFF file
corrected_file_path = "/mnt/data/corrected_dfGop.arff"
with open(corrected_file_path, "w") as file:
    file.write(corrected_arff_content)

corrected_file_path, corrections  # Return the path to the corrected file and number of corrections made
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[2], line 42
     40 for row in data_rows:
     41     for idx, value in enumerate(row):
---> 42         attribute_name = attribute_names[idx]
     44         # If the attribute is nominal, check its validity
     45         if attribute_name in nominal_attributes:

IndexError: list index out of range
Lỗi "list index out of range" cho thấy có sự không đồng bộ giữa số cột trong phần dữ liệu và số thuộc tính đã khai báo. Tôi sẽ điều chỉnh mã để xác nhận lại cấu trúc dữ liệu và kiểm tra xem có dòng nào thiếu hoặc thừa dữ liệu không.

Hãy để tôi sửa lại và kiểm tra từng phần. ​
Editor is loading...
Leave a Comment