gpt4 book ai didi

python - 类型错误 : list indices must be integers not list

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:05 32 4
gpt4 key购买 nike

我得到的实际错误

Traceback (most recent call last): File "train_2.py", line 83, in nb.train(train_listX,train_listY) File "/home/charul/Desktop/Android_malware/MalwareDetector-master/gauss_nb.py", line 116, in train group = self.group_by_class(train_list, target) File "/home/charul/Desktop/Android_malware/MalwareDetector-master/gauss_nb.py", line 57, in group_by_class x = features[target] TypeError: list indices must be integers, not list

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))

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

最佳答案

您的相关方法train(self, train_list, target)group_by_class(self, data, target)

nb.train(train_listX,train_listY)

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

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 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)

关于python - 类型错误 : list indices must be integers not list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541660/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com