代码中的Spyder图表不起作用。w是什么?

2024-09-28 17:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Spyder的新手,正在处理KDD1999数据。我试图根据数据集(如srv_错误率的总量)创建图表。然而,当我试图创建这些图表时,错误会弹出,我有一些问题无法解决。我已经对代码进行了注释。有人知道代码有什么问题吗

#Used to import all packanges annd/or libraries you will be useing
#pd loads and creates the data table or dataframe
import pandas as pd

####Section for loading data
#If the datafile extention has xlsx than the read_excel function should be used. If cvs than read_cvs should be used
#As this is stored in the same area the absoloute path can remain unchanged
df = pd.read_csv('kddcupdata1.csv')

#Pulls specific details
#Pulls first five rows
df.head()
#Pulls first three rows
df.head(3)

#Setting column names
df.columns = ['duration', 'protocol_type', 'service', 'flag', 'src_bytes', 'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot', 'num_failed_logins', 'logged_in', 'lnum_compromised', 'lroot_shell', 'lsu_attempted', 'lnum_root', 'lnum_file_creations', 'lnum_shells', 'lnum_access_files', 'lnum_outbound_cmds', 'is_host_login', 'is_guest_login', 'count', 'srv_count', 'serror_rate', 'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate', 'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count', 'dst_host_srv_count', 'dst_host_same_srv_rate', 'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate', 'dst_host_srv_diff_host_rate', 'dst_host_serror_rate', 'dst_host_srv_serror_rate', 'dst_host_rerror_rate', 'dst_host_srv_rerror_rate', 'label']

#Scatter graph for number of failed logins caused by srv serror rate
df.plot(kind='scatter',x='num_failed_logins',y='srv_serror_rate',color='red')


#This works
#Total num_failed_logins caused by srv_error_rate
# making a dict of list   
info = {'Attack': ['dst_host_same_srv_rate', 'dst_host_srv_rerror_rate'],   
        'Num' : [0, 1]}     
otd = pd.DataFrame(info)     
# sum of all salary stored in 'total'  
otd['total'] = otd['Num'].sum()     
print(otd)  

##################################################################################
#Charts that do not work

import matplotlib.pyplot as plt

#1 ERROR MESSAGE - AttributeError: 'list' object has no attribute 'lsu_attempted'
#Bar chart showing total 1su attempts
df['lsu_attempted'] = df['lsu_attempted'].astype(int)
df = ({'lsu_attempted':[1]})
df['lsu_attempted'].lsu_attempted(sort=0).plot.bar()
ax = df.plot.bar(x='super user attempts', y='Total of super user attempts', rot=0)
df.from_dict('all super user attempts', orient='index')
df.transpose()

#2 ERROR MESSAGE - TypeError: plot got an unexpected keyword argument 'x'
#A simple line plot
plt.plot(kind='bar',x='protocol_type',y='lsu_attempted')


#3 ERROR MESSAGE - TypeError: 'set' object is not subscriptable
df['lsu_attempted'] = df['lsu_attempted'].astype(int)
df = ({'lsu_attempted'})
df['lsu_attempted'].lsu_attempted(sort=0).plot.bar()
ax = df.plot.bar(x='protocol_type', y='lsu_attempted', rot=0)
df.from_dict('all super user attempts', orient='index')
df.transpose()

#5 ERROR MESSAGE - TypeError: 'dict' object is not callable
#Bar chart showing total of chosen protocols used
Data = {'protocol_types': ['tcp','icmp'],
        'number of protocols used': [10,20,30]
       }
bar = df(Data,columns=['protocol_types','number of protocols used'])
bar.plot(x ='protocol_types', y='number of protocols used', kind = 'bar')
df.show()

注意:(如果有人对它的内容有一些清晰的解释,那也会很健康,如果可能的话,请链接来源?)


Tags: ofthehostdfrateplotbarprotocol
1条回答
网友
1楼 · 发布于 2024-09-28 17:22:01

此代码段中的第一个错误:

df['lsu_attempted'] = df['lsu_attempted'].astype(int)
df = ({'lsu_attempted':[1]})
df['lsu_attempted'].lsu_attempted(sort=0).plot.bar()
ax = df.plot.bar(x='super user attempts', y='Total of super user attempts', rot=0)
df.from_dict('all super user attempts', orient='index')
df.transpose()

您得到的错误AttributeError: 'list' object has no attribute 'lsu_attempted'是上面第二行的结果

最初,df是一个pandas数据帧(上面的第1行),但从第2行开始,df现在是一个带有一个键的字典——“lsu_尝试”——它的值是一个带有一个元素的列表

因此,在第3行中,当您执行df['lsu_-attended'](作为该语句的第一部分)时,这等同于单个元素列表,并且列表没有lsu_-attended属性

我不知道您试图实现什么,但我强烈猜测您不打算用单键词典替换数据帧

第二个错误很容易-调用plt.plot不正确-x不是关键字参数-请参见matplotlib.pyplot.plot - Matplotlib 3.2.1 documentation-x和y是位置参数

您的第三条错误消息来自上面的代码片段-您制作了一个字典-并且您不能调用字典

相关问题 更多 >