Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The easy way to handle error - indexerror: invalid index to scalar variable.
Next
Answered
Mila Bousquet
  • 12
Mila Bousquet
Asked: May 12, 20222022-05-12T07:38:15+00:00 2022-05-12T07:38:15+00:00In: Programs

The easy way to handle error – indexerror: invalid index to scalar variable.

  • 12

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

import pandas as pd
 import numpy as np
 from sklearn import ensemble
 from sklearn import cross_validation
 
 def ToWeight(y):
  w = np.zeros(y.shape, dtype=float)
  ind = y != 0
  w[ind] = 1./(y[ind]**2)
  return w
 
 def RMSPE(y, yhat):
  w = ToWeight(y)
  rmspe = np.sqrt(np.mean( w * (y - yhat)**2 ))
  return rmspe
 
 forest = ensemble.RandomForestRegressor(n_estimators=10, min_samples_split=2, n_jobs=-1)
 
 print ("Cross validations")
 cv = cross_validation.KFold(len(train), n_folds=5)
 
 results = []
 for traincv, testcv in cv:
  y_test = np.expm1(forest.fit(X_train[traincv], y_train[traincv]).predict(X_train[testcv]))
  results.append(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test]))
[False False False ..., True True True]

but in my program, I am getting the warning :

IndexError: invalid index to scalar variable.

Can someone explain to me why the issue of the “ indexerror: invalid index to scalar variable.” happened? Where have I gone wrong? Thank you!

invalid index to scalar variable.
  • 2 2 Answers
  • 145 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-30T00:40:57+00:00Added an answer on May 30, 2022 at 12:40 am

    The cause: You have an iteration, then for each element of that loop which could be a scalar, has no index. If each element is an empty array, single variable, or scalar and is not a list or array you can’t use indices.

    Solution:

    You want to index the value into a non-iterable scalar.

    [y[1] for y in y_test]
    #  ^ this is the problem

    You call [y for y in test] to iterate over the values, so you only get one value in y. Your code will work the same way as if you tried to do the following:

    y_test = [1, 2, 3]
    y = y_test[0] # y = 1
    print(y[0]) # this line will fail

    Although I don’t know what you are trying to do with your results array, you should get rid [y[1] for y in y_test]. To append each y to y_test, you will need to increase your list comprehension to something like the following:

    [results.append(..., y) for y in y_test]

    You can also use a for loop

    for y in y_test:
        results.append(..., y)
    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Inès Delahaye
    2022-05-25T19:32:12+00:00Added an answer on May 25, 2022 at 7:32 pm


    YOLO Object Detection

    layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

    You don’t have to index i in layer_names[i[0]- 1] . It’s easy to remove it and do layer_names[i-1]

    layer_names = net.getLayerNames() output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]


    It Works For Me

    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.