IndexError: Too many indices for array?












0












$begingroup$


When I run this code , I get this error:



IndexError: Too many indices for array


I tried some soltions found on net, but it doesn't work.



 # returns a dictionary of n-grams frequency for any list
def ngrams_freq(listname, n):
counts = dict()
# make n-grams as string iteratively
grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
for gram in grams:
if gram not in counts:
counts[gram] = 1
else:
counts[gram] += 1
return counts

# returns the values of features for any list
def feature_freq(listname,n):
counts = dict()
# make n-grams as string iteratively
grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
for gram in grams:
counts[gram] = 0
for gram in grams:
if gram in features:
counts[gram] += 1
return counts

# values of n for finding n-grams
n_values = [3]

# Base address for attack data files
add = "ADFA-LD/ADFA-LD/Attack_Data_Master/"

# initializing dictionary for n-grams from all files
traindict = {}

print(" Training data from Normal")
Normal_list =
in_address = "ADFA-LD/ADFA-LD/Training_Data_Master/"
k = 1
read_files = glob.glob(in_address+"/*.txt")
for f in read_files:
with open(f, "r") as infile:
globals()['Normal%s_list_array' % str(k)] = infile.read().split()
Normal_list.extend(globals()['Normal%s_list_array' % str(k)])
k += 1


#print(Normal_list)
# number of lists for distinct files
Normal_list_size = k-1
# combined list of all files
listname = Normal_list

# finding n-grams and extracting top 30%
for n in n_values:
dictname = ngrams_freq(listname,n)

# Creating feature list
features =
#features.append('Label')
for k,v in dictname.items():
features.append(k)


#print (features) #this contains sequences only

# Writing training data to file, this file contains sequences of 3-grams

with open('train1.csv','w') as csvfile:
# writing features as header
writer = csv.DictWriter(csvfile, fieldnames = features,
extrasaction='ignore')
writer.writeheader();

features_a = np.asarray(features)
X, y = features_a[:, 0], features_a[:, 1]


if any one can help me, I'll be thankful.
Thank you.










share|improve this question









$endgroup$

















    0












    $begingroup$


    When I run this code , I get this error:



    IndexError: Too many indices for array


    I tried some soltions found on net, but it doesn't work.



     # returns a dictionary of n-grams frequency for any list
    def ngrams_freq(listname, n):
    counts = dict()
    # make n-grams as string iteratively
    grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
    for gram in grams:
    if gram not in counts:
    counts[gram] = 1
    else:
    counts[gram] += 1
    return counts

    # returns the values of features for any list
    def feature_freq(listname,n):
    counts = dict()
    # make n-grams as string iteratively
    grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
    for gram in grams:
    counts[gram] = 0
    for gram in grams:
    if gram in features:
    counts[gram] += 1
    return counts

    # values of n for finding n-grams
    n_values = [3]

    # Base address for attack data files
    add = "ADFA-LD/ADFA-LD/Attack_Data_Master/"

    # initializing dictionary for n-grams from all files
    traindict = {}

    print(" Training data from Normal")
    Normal_list =
    in_address = "ADFA-LD/ADFA-LD/Training_Data_Master/"
    k = 1
    read_files = glob.glob(in_address+"/*.txt")
    for f in read_files:
    with open(f, "r") as infile:
    globals()['Normal%s_list_array' % str(k)] = infile.read().split()
    Normal_list.extend(globals()['Normal%s_list_array' % str(k)])
    k += 1


    #print(Normal_list)
    # number of lists for distinct files
    Normal_list_size = k-1
    # combined list of all files
    listname = Normal_list

    # finding n-grams and extracting top 30%
    for n in n_values:
    dictname = ngrams_freq(listname,n)

    # Creating feature list
    features =
    #features.append('Label')
    for k,v in dictname.items():
    features.append(k)


    #print (features) #this contains sequences only

    # Writing training data to file, this file contains sequences of 3-grams

    with open('train1.csv','w') as csvfile:
    # writing features as header
    writer = csv.DictWriter(csvfile, fieldnames = features,
    extrasaction='ignore')
    writer.writeheader();

    features_a = np.asarray(features)
    X, y = features_a[:, 0], features_a[:, 1]


    if any one can help me, I'll be thankful.
    Thank you.










    share|improve this question









    $endgroup$















      0












      0








      0





      $begingroup$


      When I run this code , I get this error:



      IndexError: Too many indices for array


      I tried some soltions found on net, but it doesn't work.



       # returns a dictionary of n-grams frequency for any list
      def ngrams_freq(listname, n):
      counts = dict()
      # make n-grams as string iteratively
      grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
      for gram in grams:
      if gram not in counts:
      counts[gram] = 1
      else:
      counts[gram] += 1
      return counts

      # returns the values of features for any list
      def feature_freq(listname,n):
      counts = dict()
      # make n-grams as string iteratively
      grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
      for gram in grams:
      counts[gram] = 0
      for gram in grams:
      if gram in features:
      counts[gram] += 1
      return counts

      # values of n for finding n-grams
      n_values = [3]

      # Base address for attack data files
      add = "ADFA-LD/ADFA-LD/Attack_Data_Master/"

      # initializing dictionary for n-grams from all files
      traindict = {}

      print(" Training data from Normal")
      Normal_list =
      in_address = "ADFA-LD/ADFA-LD/Training_Data_Master/"
      k = 1
      read_files = glob.glob(in_address+"/*.txt")
      for f in read_files:
      with open(f, "r") as infile:
      globals()['Normal%s_list_array' % str(k)] = infile.read().split()
      Normal_list.extend(globals()['Normal%s_list_array' % str(k)])
      k += 1


      #print(Normal_list)
      # number of lists for distinct files
      Normal_list_size = k-1
      # combined list of all files
      listname = Normal_list

      # finding n-grams and extracting top 30%
      for n in n_values:
      dictname = ngrams_freq(listname,n)

      # Creating feature list
      features =
      #features.append('Label')
      for k,v in dictname.items():
      features.append(k)


      #print (features) #this contains sequences only

      # Writing training data to file, this file contains sequences of 3-grams

      with open('train1.csv','w') as csvfile:
      # writing features as header
      writer = csv.DictWriter(csvfile, fieldnames = features,
      extrasaction='ignore')
      writer.writeheader();

      features_a = np.asarray(features)
      X, y = features_a[:, 0], features_a[:, 1]


      if any one can help me, I'll be thankful.
      Thank you.










      share|improve this question









      $endgroup$




      When I run this code , I get this error:



      IndexError: Too many indices for array


      I tried some soltions found on net, but it doesn't work.



       # returns a dictionary of n-grams frequency for any list
      def ngrams_freq(listname, n):
      counts = dict()
      # make n-grams as string iteratively
      grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
      for gram in grams:
      if gram not in counts:
      counts[gram] = 1
      else:
      counts[gram] += 1
      return counts

      # returns the values of features for any list
      def feature_freq(listname,n):
      counts = dict()
      # make n-grams as string iteratively
      grams = [' '.join(listname[i:i+n]) for i in range(len(listname)-n)]
      for gram in grams:
      counts[gram] = 0
      for gram in grams:
      if gram in features:
      counts[gram] += 1
      return counts

      # values of n for finding n-grams
      n_values = [3]

      # Base address for attack data files
      add = "ADFA-LD/ADFA-LD/Attack_Data_Master/"

      # initializing dictionary for n-grams from all files
      traindict = {}

      print(" Training data from Normal")
      Normal_list =
      in_address = "ADFA-LD/ADFA-LD/Training_Data_Master/"
      k = 1
      read_files = glob.glob(in_address+"/*.txt")
      for f in read_files:
      with open(f, "r") as infile:
      globals()['Normal%s_list_array' % str(k)] = infile.read().split()
      Normal_list.extend(globals()['Normal%s_list_array' % str(k)])
      k += 1


      #print(Normal_list)
      # number of lists for distinct files
      Normal_list_size = k-1
      # combined list of all files
      listname = Normal_list

      # finding n-grams and extracting top 30%
      for n in n_values:
      dictname = ngrams_freq(listname,n)

      # Creating feature list
      features =
      #features.append('Label')
      for k,v in dictname.items():
      features.append(k)


      #print (features) #this contains sequences only

      # Writing training data to file, this file contains sequences of 3-grams

      with open('train1.csv','w') as csvfile:
      # writing features as header
      writer = csv.DictWriter(csvfile, fieldnames = features,
      extrasaction='ignore')
      writer.writeheader();

      features_a = np.asarray(features)
      X, y = features_a[:, 0], features_a[:, 1]


      if any one can help me, I'll be thankful.
      Thank you.







      python keras






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 13 mins ago









      KikioKikio

      264




      264






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46113%2findexerror-too-many-indices-for-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46113%2findexerror-too-many-indices-for-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár