TypeError:列表索引必须是整数而不是list

时间:2019-04-05 18:46:35

标签: python

我得到的实际错误

  

回溯(最近通话最近):     在第83行的文件“ train_2.py”中       nb.train(train_listX,train_listY)     在火车上的第116行中的文件“ /home/charul/Desktop/Android_malware/MalwareDetector-master/gauss_nb.py”       组= self.group_by_class(train_list,目标)     group_by_class中的文件“ /home/charul/Desktop/Android_malware/MalwareDetector-master/gauss_nb.py”,第57行       x =功能[目标]   TypeError:列表索引必须是整数,而不是列表

train_2.py:

import pickle
import datetime
import sys
import random
import csv
from sklearn import svm
from sklearn import tree
from sklearn import linear_model
from sklearn.naive_bayes import GaussianNB
from pymongo import MongoClient
from constants import DB_NAME, ALG_TYPES
from core import create_vector_multiple
from gauss_nb import GaussNB

def load_data():
    feature_vector = []
    # Data format
# Column 0: Class label (1: Malware, 0: Benign)
# Column 1-19: Features
with open('data.csv','r') as fp:
    for i,line in enumerate(fp):
        if i == 0:
            pass
        else:
            feature_vector.append([int(x.strip()) for x in line.split(',')])
return feature_vector

if __name__ == '__main__':
    if len(sys.argv) > 1:
        alg = sys.argv[1]
        if alg not in ALG_TYPES:
            print ('[!] Algorithm type should be svm, dt, gauss or log.')
        else:
            client = MongoClient()
        db = client[DB_NAME]
        good_apks = list(db.apk.find({"data_type": "goodware"})[:1000])
        bad_apks = list(db.apk.find({"data_type": "malware"})[:500])
        apks = good_apks + bad_apks
        f, t = create_vector_multiple(apks)
        train_result = {'timestamp': datetime.datetime.now(),
                        'alg_type': alg}
        # Load the data
        data = load_data()
        # Shuffle the data
        random.shuffle(data)
        # Divide the data into training and testing in 60:40
        trainLength = int(0.6*len(data))
        # Training Data
        trainX = [x[:-1] for x in data[:trainLength]]
        trainY = [y[-1] for y in data[:trainLength]]
        # Testing Data
        testX = [x[:-1] for x in data[trainLength:]]
        testY = [y[-1] for y in data[trainLength:]]

        if alg == 'log':
            clf = linear_model.LogisticRegression()
            clf.fit(trainX, trainY)
            print 'Accuracy: {:.3f}%'.format(clf.score(testX, testY)*100)
            s_object = pickle.dumps(clf)
            s_object = s_object.encode('base64')
            train_result['train_data'] = s_object
        elif alg == 'svm':
            C = 1.0
            clf = svm.SVC()
            clf.set_params(kernel='rbf').fit(trainX, trainY)
            print 'Accuracy: {:.3f}%'.format(clf.score(testX, testY)*100)
            s_object = pickle.dumps(clf)
            s_object = s_object.encode('base64')
            train_result['train_data'] = s_object
        elif alg == 'dt':
            clf = tree.DecisionTreeClassifier(random_state=0)
            clf.fit(trainX, trainY)
            print 'Accuracy: {:.3f}%'.format(clf.score(testX, testY)*100)
            s_object = pickle.dumps(clf)
            s_object = s_object.encode('base64')
            train_result['train_data'] = s_object
        elif alg == 'gauss':
            nb = GaussNB()
            train_listX =f[:trainLength]
            train_listY =t[:trainLength]
            test_listX =f[trainLength:]
            test_listY =t[trainLength:]
            nb.train(train_listX,train_listY)
            predicted = nb.predict(test_listX)
            accuracy = nb.accuracy(test_listX, predicted)
            print 'Accuracy: %.3f' % accuracy

        db['train'].insert_one(train_result)
        print ('[+] Data trained and added to database')
else:
    print ('[+] Usage: python {} <alg_type>'.format(__file__))

gauss_nb.py `

class GaussNB:
    def __init__(self):
    pass

def load_csv(self, data, header=False):
    """
    :param data: raw comma seperated file
    :param header: remove header if it exists
    :return:
    Load and convert each string of data into a float
    """
    lines = csv.reader(data.splitlines())
    dataset = list(lines)
    if header:
        # remove header
        dataset = dataset[1:]
    for i in range(len(dataset)):
        dataset[i] = [float(x) if re.search('\d', x) else x for x in dataset[i]]
    return dataset

def split_data(self, data, weight):
    """
    :param data:
    :param weight: indicates the percentage of rows that'll be used for training
    :return:
    Randomly selects rows for training according to the weight and uses the rest of the rows for testing.
    """
    train_size = int(len(data) * weight)
    train_set = []
    for i in range(train_size):
        index = random.randrange(len(data))
        train_set.append(data[index])
        data.pop(index)
    return [train_set, data]

def group_by_class(self, data, target):
    """
    :param data: Training set. Lists of events (rows) in a list
    :param target: Index for the target column. Usually the last index in the list
    :return:
    Mapping each target to a list of it's features
    """
    target_map = defaultdict(list)
    for index in range(len(data)):
        features = data[index]
        if not features:
            continue
        x = features[target]
        target_map[x].append(features[:-1])  # designating the last column as the class column
    return dict(target_map)

def mean(self, numbers):
    """
    :param numbers: list of numbers
    :return:
    """
    result = sum(numbers) / float(len(numbers))
    return result

def stdev(self, numbers):
    """
    :param numbers: list of numbers
    :return:
    Calculate the standard deviation for a list of numbers.
    """
    avg = self.mean(numbers)
    squared_diff_list = []
    for num in numbers:
        squared_diff = (num - avg) ** 2
        squared_diff_list.append(squared_diff)
    squared_diff_sum = sum(squared_diff_list)
    sample_n = float(len(numbers) - 1)
    var = squared_diff_sum / sample_n
    return var ** .5

def summarize(self, test_set):
    """
    :param test_set: lists of features
    :return:
    Use zip to line up each feature into a single column across multiple lists.
    yield the mean and the stdev for each feature.
    """
    for feature in zip(*test_set):
        yield {
            'stdev': self.stdev(feature),
            'mean': self.mean(feature)
        }

def prior_prob(self, group, target, data):
    """
    :return:
    The probability of each target class
    """
    total = float(len(data))
    result = len(group[target]) / total
    return result

def train(self, train_list, target):
    """
    :param data:
    :param target: target class
    :return:
    For each target:
        1. yield prior_prob: the probability of each class. P(class) eg P(Iris-virginica)
        2. yield summary: list of {'mean': 0.0, 'stdev': 0.0}
    """
    group = self.group_by_class(train_list, target)
    self.summaries = {}
    for target, features in group.iteritems():
        self.summaries[target] = {
            'prior_prob': self.prior_prob(group, target, train_list),
            'summary': [i for i in self.summarize(features)],
        }
    return self.summaries

def normal_pdf(self, x, mean, stdev):
    """
    :param x: a variable
    :param mean: µ - the expected value or average from M samples
    :param stdev: σ - standard deviation
    :return: Gaussian (Normal) Density function.
    N(x; µ, σ) = (1 / 2πσ) * (e ^ (x–µ)^2/-2σ^2
    """
    variance = stdev ** 2
    exp_squared_diff = (x - mean) ** 2
    exp_power = -exp_squared_diff / (2 * variance)
    exponent = e ** exp_power
    denominator = ((2 * pi) ** .5) * stdev
    normal_prob = exponent / denominator
    return normal_prob

def marginal_pdf(self, joint_probabilities):
    """
    :param joint_probabilities: list of joint probabilities for each feature
    :return:
    Marginal Probability Density Function (Predictor Prior Probability)
    Joint Probability = prior * likelihood
    Marginal Probability is the sum of all joint probabilities for all classes.

    marginal_pdf =
      [P(setosa) * P(sepal length | setosa) * P(sepal width | setosa) * P(petal length | setosa) * P(petal width | setosa)]
    + [P(versicolour) * P(sepal length | versicolour) * P(sepal width | versicolour) * P(petal length | versicolour) * P(petal width | versicolour)]
    + [P(virginica) * P(sepal length | verginica) * P(sepal width | verginica) * P(petal length | verginica) * P(petal width | verginica)]

    """
    marginal_prob = sum(joint_probabilities.values())
    return marginal_prob

def joint_probabilities(self, test_row):
    """
    :param test_row: single list of features to test; new data
    :return:
    Use the normal_pdf(self, x, mean, stdev) to calculate the Normal Probability for each feature
    Take the product of all Normal Probabilities and the Prior Probability.
    """
    joint_probs = {}
    for target, features in self.summaries.iteritems():
        total_features = len(features['summary'])
        likelihood = 1
        for index in range(total_features):
            feature = test_row[index]
            mean = features['summary'][index]['mean']
            stdev = features['summary'][index]['stdev']
            normal_prob = self.normal_pdf(feature, mean, stdev)
            likelihood *= normal_prob
        prior_prob = features['prior_prob']
        joint_probs[target] = prior_prob * likelihood
    return joint_probs

def posterior_probabilities(self, test_row):
    """
    :param test_row: single list of features to test; new data
    :return:
    For each feature (x) in the test_row:
        1. Calculate Predictor Prior Probability using the Normal PDF N(x; µ, σ). eg = P(feature | class)
        2. Calculate Likelihood by getting the product of the prior and the Normal PDFs
        3. Multiply Likelihood by the prior to calculate the Joint Probability.

    E.g.
    prior_prob: P(setosa)
    likelihood: P(sepal length | setosa) * P(sepal width | setosa) * P(petal length | setosa) * P(petal width | setosa)
    joint_prob: prior_prob * likelihood
    marginal_prob: predictor prior probability
    posterior_prob = joint_prob/ marginal_prob

    returning a dictionary mapping of class to it's posterior probability
    """
    posterior_probs = {}
    joint_probabilities = self.joint_probabilities(test_row)
    marginal_prob = self.marginal_pdf(joint_probabilities)
    for target, joint_prob in joint_probabilities.iteritems():
        posterior_probs[target] = joint_prob / marginal_prob
    return posterior_probs

def get_map(self, test_row):
    """
    :param test_row: single list of features to test; new data
    :return:
    Return the target class with the largest/best posterior probability
    """
    posterior_probs = self.posterior_probabilities(test_row)
    map_prob = max(posterior_probs, key=posterior_probs.get)
    return map_prob

def predict(self, test_set):
    """
    :param test_set: list of features to test on
    :return:
    Predict the likeliest target for each row of the test_set.
    Return a list of predicted targets.
    """
    map_probs = []
    for row in test_set:
        map_prob = self.get_map(row)
        map_probs.append(map_prob)
    return map_probs

def accuracy(self, test_set, predicted):
    """
    :param test_set: list of test_data
    :param predicted: list of predicted classes
    :return:
    Calculate the the average performance of the classifier.
    """
    correct = 0
    actual = [item[-1] for item in test_set]
    for x, y in zip(actual, predicted):
        if x == y:
            correct += 1
    return correct / float(len(test_set))

` 如果有人可以帮助我,那就太好了!

2 个答案:

答案 0 :(得分:0)

您的相关方法while($row = mysqli_fetch_assoc($result)) { $values[$row['FieldName']] = $row['FieldValue']; } echo $values['Header.Title']; train(self, train_list, target)

group_by_class(self, data, target)

第二个参数应该是整数而不是列表。传递nb.train(train_listX,train_listY)作为第二个参数,然后调用train_listY,这里的目标是features[target]

train_listY

答案 1 :(得分:0)

通常进行调试:在该行之前打印目标值。可能是列表,而不是整数(如错误消息所示)。然后回溯到谁用列表调用它。

但是,仅通过查看,我发现您在target中重用变量名train()并覆盖了函数的arg感到非常可疑。