python - TypeError: 'float' 对象不可下标 --Python

标签 python python-3.x list for-loop typeerror

我正在尝试创建一个程序来从两个 CSV 中获取数据,然后计算距离并对特征进行分类。我的代码目前仅在第一次迭代中正常工作,整个代码工作正常,直到我更改了 weight_based_approach() 函数的返回值,之前 weight_based_approach() 返回 2 float值,现在它只返回一个值,这里是更改的部分 代码:

    return (eucl_weight_prediction_count / len(test_data) * 100), (
            manhattan_metric_prediction_count / len(test_data) * 100)

当前完整代码:

import numpy as np
import matplotlib.pyplot as plt


class Implementation:
    def __init__(self):
        pass

    def Distancess(self, training_sub_data, query_instance):

        query_params = query_instance[:10]

        eucl = np.sqrt(np.sum((training_sub_data - query_params) ** 2, axis=-1))
        return eucl, np.argsort(eucl)

    def weight(self, training_data, distances, sorted_indices, k):
        i = 1
        samples_class = training_data[sorted_indices[:k]][:, -1]
        nearest_distances = distances[sorted_indices[:k]]
        nearest_weights = np.divide(1, np.square(nearest_distances))
        class_0_weights_sum = np.sum(nearest_weights[samples_class == 0])
        class_1_weights_sum = np.sum(nearest_weights[samples_class == 1])
        class_2_weights_sum = np.sum(nearest_weights[samples_class == 2])

        if class_0_weights_sum > class_1_weights_sum and class_0_weights_sum > class_2_weights_sum:
            return 0
        elif class_1_weights_sum > class_0_weights_sum and class_1_weights_sum > class_2_weights_sum:
            return 1
        else:
            return 2

def weight_based_approach(training_data, test_data, kn_k_value):
    training_data_10_columns = training_data[:, :10]

    kn = Implementation()

    eucl_weight_prediction_count = 0
    for query_instance in test_data:
        distances, euclidean_indices = kn.Distancess(training_data_10_columns, query_instance)

        weight_based_average = kn.weight(training_data, distances, euclidean_indices, kn_k_value)

        if query_instance[-1] == weight_based_average:
            eucl_weight_prediction_count += 1

    return eucl_weight_prediction_count / len(test_data) * 100


def main():
    global accuracies
    euclidean_accuracies = []
    k_samples = []
    k_samples.extend(list(range(1, 4, 1)))

    print("Range" + str(k_samples))

    for k in k_samples:
        training_file = "classification/trainingData.csv"
        test_file = "classification/testData.csv"
        kn_k_value = k

        training_data = np.genfromtxt(training_file, delimiter=",")
        test_data = np.genfromtxt(test_file, delimiter=",")

        accuracies = weight_based_approach(training_data, test_data, kn_k_value)
        euclidean_accuracies.append(accuracies[0])

    print("distance: " + str(euclidean_accuracies))

    plt.plot(k_samples, euclidean_accuracies, 'r')
    plt.xlabel('K{Number of Nearest Neighbour(s)}')
    plt.ylabel('Accuracy %')
    plt.title('K vs Accuracy graph')
    plt.grid(True)
    plt.show()


if __name__ == '__main__':
    main()


错误:

Traceback (most recent call last):
  File "A:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-e142ccdf3a2c>", line 1, in <module>
    runfile('A:/Workspace/PML/R00182527/Part2a.py', wdir='A:/Workspace/PML/R00182527')
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "A:/Workspace/P7/Part.py", line 143, in <module>
    main()
  File "A:/Workspace/P7/Part.py", line 126, in main
    euclidean_accuracies.append(accuracies[0])
TypeError: 'float' object is not subscriptable

期望准确率%作为输出

最佳答案

你应该更加努力地构建一个 Minimal, Reproducible Example 。在最小示例程序中重现错误后,您还应该发布错误的完整回溯,以便我们可以看到错误发生在哪一行。

但是,此错误意味着您无法为 float 添加下标,因为它不是容器并且未实现 __getitem__() (如列表、元组等)。也就是说,这不起作用:

>>> x = 3.2
>>> x[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not subscriptable

在您的示例中,变量 accuracies 不是列表/元组,而是 float 。因此,您可能不小心创建了一个错误,导致您返回了 float 而不是列表/元组。

关于python - TypeError: 'float' 对象不可下标 --Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584845/

相关文章:

python - 如何让 setuptools 安装不在 PyPI 上的包?

android - 我可以基于 Python 代码为 Android 和/或 iOS 创建可执行代码吗?

python - 通过 Python 节点将字典从 LabVIEW 传递到 Python 脚本

python - 手动安装pyodbc

jQuery 切换子列表项

r - lapply 在两个列表向量上

c# - 将 IList 转换为列表

python - 如何根据它们的值组合两个字典?

python - Panda 的 DataFrame 双转置将数字类型更改为对象

python - 检查元素是否存在,如果不存在则执行 ..