1

i have a part of code like this

#predicitng values one by one 
regr = linear_model.LinearRegression()
predicted_value = np.array([ 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32])
predicted_value =  predicted_value.reshape(-1,1)


#mt
regr.fit(x, y)
predicted_values = regr.predict(predicted_value)
predict_outcome = regr.predict(predicted_value)
predictions = {'predicted_value': predict_outcome}
mmt = np.mean(predict_outcome)

#ht
regr.fit(x, ht)
predicted_values = regr.predict(predicted_value)
predict_outcome = regr.predict(predicted_value)
predictions = {'predicted_value': predict_outcome}
mht = np.mean(predict_outcome)

here instead of this :

predicted_value = np.array([ 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32])

how can i set a range from 9 to 32(or x to y) so that i can avoid typing all numbers.if it is done using for loop how to apply it in this context

2
  • Hi, you could consider editing the title of your question. Currently it is somewhat misleading because the question is not necessarily about for loops. It is about initializing an array with a sequence of numbers, if I am not mistaken. Commented Mar 6, 2017 at 9:10
  • You could also skip the first code snippet since it is irrelevant to your actual question. Commented Mar 6, 2017 at 18:15

2 Answers 2

8

There is no need to use a loop. You can use numpy.arange([start, ]stop, [step, ]) to generate a range of numbers.

In your case:

predicted_value = np.arange(9, 33)  # Note the 33 if you want 9..32

If you really want to use a loop, there is the option of using a list comprehension:

predicted_value = np.array([i for i in range(9, 33)])

Or an explicit loop, which would be most horrible:

predicted_value = np.empty(33 - 9)
for k, i in enumerate(range(9, 33)):
    predicted_value[k] = i
Sign up to request clarification or add additional context in comments.

Comments

0
predicted_value = np.array([i for i in range(9, 33)])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.