【问题标题】:Spyder charts in the code are not working. What is w?代码中的 Spyder 图表不起作用。 w是什么?
【发布时间】:2020-07-19 00:00:38
【问题描述】:

我是 Spyder 的新手,正在处理 KDD1999 数据。我正在尝试根据数据集创建图表,例如 srv_error 率的总量。但是,当我尝试创建这些图表时会弹出错误,并且有一些我无法解决。我已经评论了代码。有人知道代码有什么问题吗?

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

注意:(另外,如果有人对它的含义有一些明确的解释,那也是有益的,请尽可能链接来源?)

【问题讨论】:

    标签: python pandas dataframe dataset spyder


    【解决方案1】:

    你在这个 sn-p 中的第一个错误:

    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_attempted':[1]}) 开始,df 现在是一个带有一个键的字典 - 'lsu_attempted' - 它的值是一个带有一个元素的列表。

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

    我不知道您要达到什么目的,但我强烈猜测您不打算用单键字典替换您的数据框。

    您的第二个错误很简单 - 您错误地调用 plt.plot - x 不是关键字参数 - 请参阅 matplotlib.pyplot.plot - Matplotlib 3.2.1 documentation - x 和 y 是位置参数。

    您的第三条错误消息来自上面的代码 sn-p - 您将 df 设为字典 - 您无法调用字典。

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2021-07-01
      • 1970-01-01
      • 2014-01-23
      • 2013-08-06
      相关资源
      最近更新 更多