为啥下面的代码不能得到 n fold = 1 的值?

Posted

技术标签:

【中文标题】为啥下面的代码不能得到 n fold = 1 的值?【英文标题】:Why does the following code cant get value of n_fold = 1?为什么下面的代码不能得到 n fold = 1 的值? 【发布时间】:2018-03-22 01:21:39 【问题描述】:

使用以下代码时,n_folds 必须为 2 或更多。如何更改它以使其与 n_folds = 1 一起使用? 将其更改为 1 时,但是对于 n_folds = 2 及更多,它可以工作。有些东西似乎在函数中不起作用。 对于n_folds = 1,有如下错误:

Traceback (most recent call last):
  File "GX.py", line 266, in <module>
    scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)
  File "GX.py", line 92, in evaluate_algorithm
    predicted = algorithm(train_set, test_set, *args)
  File "GX.py", line 223, in random_forest
    tree = build_tree(sample, max_depth, min_size, n_features)
  File "GX.py", line 183, in build_tree
    root = get_split(train, n_features)
  File "GX.py", line 137, in get_split
    index = randrange(len(dataset[0]) - 1)
IndexError: list index out of range

代码如下:

# Select the best split point for a dataset
def get_split(dataset, n_features):
    class_values = list(set(row[-1] for row in dataset))
    b_index, b_value, b_score, b_groups = 999, 999, 999, None
    features = list()
    while len(features) < n_features:
        index = randrange(len(dataset[0])-1)
        if index not in features:
            features.append(index)
    for index in features:
        for row in dataset:
            groups = test_split(index, row[index], dataset)
            gini = gini_index(groups, class_values)
            if gini < b_score:
                b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    return 'index':b_index, 'value':b_value, 'groups':b_groups


# Random Forest Algorithm on Sonar Dataset
from random import seed
from random import randrange
from csv import reader
from math import sqrt


# Load a CSV file
def load_csv(filename):
    dataset = list()
    with open(filename, 'r') as file:
        csv_reader = reader(file)
        for row in csv_reader:
            if not row:
                continue
            dataset.append(row)
    return dataset


# Convert string column to float
def str_column_to_float(dataset, column):
    for row in dataset:
        row[column] = float(row[column].strip())


# Convert string column to integer
def str_column_to_int(dataset, column):
    class_values = [row[column] for row in dataset]
    unique = set(class_values)
    lookup = dict()
    for i, value in enumerate(unique):
        lookup[value] = i
    for row in dataset:
        row[column] = lookup[row[column]]
    return lookup


# Split a dataset into k folds
def cross_validation_split(dataset, n_folds):
    dataset_split = list()
    dataset_copy = list(dataset)
    fold_size = int(len(dataset) / n_folds)
    for i in range(n_folds):
        fold = list()
        while len(fold) < fold_size:
            index = randrange(len(dataset_copy))
            fold.append(dataset_copy.pop(index))
        dataset_split.append(fold)
    return dataset_split


# Calculate accuracy percentage
def accuracy_metric(actual, predicted):
    correct = 0
    for i in range(len(actual)):
        if actual[i] == predicted[i]:
            correct += 1
    return correct / float(len(actual)) * 100.0


# Evaluate an algorithm using a cross validation split
def evaluate_algorithm(dataset, algorithm, n_folds, *args):
    folds = cross_validation_split(dataset, n_folds)
    scores = list()
    for fold in folds:
        train_set = list(folds)
        train_set.remove(fold)
        train_set = sum(train_set, [])
        test_set = list()
        for row in fold:
            row_copy = list(row)
            test_set.append(row_copy)
            row_copy[-1] = None
        predicted = algorithm(train_set, test_set, *args)
        actual = [row[-1] for row in fold]
        accuracy = accuracy_metric(actual, predicted)
        scores.append(accuracy)
    return scores


# Split a dataset based on an attribute and an attribute value
def test_split(index, value, dataset):
    left, right = list(), list()
    for row in dataset:
        if row[index] < value:
            left.append(row)
        else:
            right.append(row)
    return left, right


# Calculate the Gini index for a split dataset
def gini_index(groups, classes):
    # count all samples at split point
    n_instances = float(sum([len(group) for group in groups]))
    # sum weighted Gini index for each group
    gini = 0.0
    for group in groups:
        size = float(len(group))
        # avoid divide by zero
        if size == 0:
            continue
        score = 0.0
        # score the group based on the score for each class
        for class_val in classes:
            p = [row[-1] for row in group].count(class_val) / size
            score += p * p
        # weight the group score by its relative size
        gini += (1.0 - score) * (size / n_instances)
    return gini


# Select the best split point for a dataset
def get_split(dataset, n_features):
    class_values = list(set(row[-1] for row in dataset))
    b_index, b_value, b_score, b_groups = 999, 999, 999, None
    features = list()
    while len(features) < n_features:
        index = randrange(len(dataset[0]) - 1)
        if index not in features:
            features.append(index)
    for index in features:
        for row in dataset:
            groups = test_split(index, row[index], dataset)
            gini = gini_index(groups, class_values)
            if gini < b_score:
                b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    return 'index': b_index, 'value': b_value, 'groups': b_groups


# Create a terminal node value
def to_terminal(group):
    outcomes = [row[-1] for row in group]
    return max(set(outcomes), key=outcomes.count)


# Create child splits for a node or make terminal
def split(node, max_depth, min_size, n_features, depth):
    left, right = node['groups']
    del (node['groups'])
    # check for a no split
    if not left or not right:
        node['left'] = node['right'] = to_terminal(left + right)
        return
    # check for max depth
    if depth >= max_depth:
        node['left'], node['right'] = to_terminal(left), to_terminal(right)
        return
    # process left child
    if len(left) <= min_size:
        node['left'] = to_terminal(left)
    else:
        node['left'] = get_split(left, n_features)
        split(node['left'], max_depth, min_size, n_features, depth + 1)
    # process right child
    if len(right) <= min_size:
        node['right'] = to_terminal(right)
    else:
        node['right'] = get_split(right, n_features)
        split(node['right'], max_depth, min_size, n_features, depth + 1)


# Build a decision tree
def build_tree(train, max_depth, min_size, n_features):
    root = get_split(train, n_features)
    split(root, max_depth, min_size, n_features, 1)
    return root


# Make a prediction with a decision tree
def predict(node, row):
    if row[node['index']] < node['value']:
        if isinstance(node['left'], dict):
            return predict(node['left'], row)
        else:
            return node['left']
    else:
        if isinstance(node['right'], dict):
            return predict(node['right'], row)
        else:
            return node['right']


# Create a random subsample from the dataset with replacement
def subsample(dataset, ratio):
    sample = list()
    n_sample = round(len(dataset) * ratio)
    while len(sample) < n_sample:
        index = randrange(len(dataset))
        sample.append(dataset[index])
    return sample


# Make a prediction with a list of bagged trees
def bagging_predict(trees, row):
    predictions = [predict(tree, row) for tree in trees]
    return max(set(predictions), key=predictions.count)


# Random Forest Algorithm
def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_features):
    trees = list()
    for i in range(n_trees):
        sample = subsample(train, sample_size)
        tree = build_tree(sample, max_depth, min_size, n_features)
        trees.append(tree)
    predictions = [bagging_predict(trees, row) for row in test]
    return (predictions)

seed(1)
import pandas as pd
file_path ='https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data'
dataset2 = pd.read_csv(file_path, header=None, sep=',')

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import train_test_split
from sklearn import preprocessing
dataset1 = pd.DataFrame(dataset2)
dataset1 = dataset1.drop(0, axis=1)

train, test = train_test_split(dataset1, test_size=0.1, random_state = 0) ###<-----
df = dataset1.astype('str')
dataset = df.values.tolist()

train1 = train.astype('str')
train = train1.values.tolist()

test1 = test.astype('str')
test = test1.values.tolist()

target_index = 0 
for i in range(0, len(dataset[0])):
        if i != target_index:
            str_column_to_float(dataset, i)
# convert class column to integers
str_column_to_int(dataset, target_index)

# evaluate algorithm
n_folds = 1 ##<----
max_depth = 10
min_size = 1
sample_size = 1.0
n_features = int(sqrt(len(dataset[0]) - 1))


for n_trees in [5]:
    scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)
    print('Trees: %d' % n_trees)
    print('Scores: %s' % scores)
    print('Mean Accuracy: %.3f%%' % (sum(scores) / float(len(scores))))

【问题讨论】:

哇,如果我是你,我会尽量减少代码并简化问题。这太长了.. 它有什么作用?拥有函数的上下文肯定会有所帮助! n_folds 是什么? How do I ask a good question? 对于 n_fold=1,数据不会按照你的代码分为训练和测试。您定义的单折叠将分配给test_set,并从evaluate_algorithm() 中的train_set 中删除。这是algorithm() 中后续步骤出错的原因,因为您没有任何数据要训练。你想怎么处理? 非常感谢@VivekKumar!我只想将数据拆分为训练和测试(例如 90% 和 10%)并将其发送到评估算法。所以,实际上,我需要得到测试数据的预测(我将它与实际进行比较)。 【参考方案1】:

你了解cross validation的概念吗?在 k 折交叉验证中,您的数据被分成 k 个样本,一个样本用于测试模型,而 k-1 个样本用于训练模型。

如果您设置 k = 1(或示例中的 n = 1),您将无法对数据进行采样并执行交叉验证。

【讨论】:

谢谢@ArthurGouveia,这是个问题:我不想使用 k-folds,这就是为什么我想用 k-folds=1 运行它,我的问题是如何更改代码,所以它适用于这种情况!

以上是关于为啥下面的代码不能得到 n fold = 1 的值?的主要内容,如果未能解决你的问题,请参考以下文章

为啥不能比较两个 int 类型变量的值? [复制]

在 c# 代码中,为啥下面的副本不能作为参考副本? [复制]

关于24c02芯片的程序问题,希望得到帮助解决,下面的程序有啥问题,为啥下载到单片机里面不能运行!

为啥指针不能传递正确的值(C)?

vs2017为啥不能输出我要的结果,而是输出的下面的东西

为啥下面的方法会得到内部链接?