0

I would like to create an array of two columns such that the second column is generated from the first in an array.

This is the best I can do ... it simply stacks x and y, one on top of itself.

import numpy as np
from numpy import array

n= 100
results= np.array([])

for x in range(0, 100):  
    y= x*x
    new_row = [x, y]
    results = np.append(results, new_row)

I got this one to work... eventually!!!!

import numpy as np
from numpy import array

results= np.zeros(shape=(0,2))

for x in range(0, 100): 
    y = x*x
    row = array([[x, y]])
    results = np.concatenate((results, row))
3
  • I really like the code examples except my real "x**2" is actually several lines of code. Commented Mar 23, 2019 at 17:35
  • I've updated my answer - you could just write a function to calculate the other value. This would work the same in @sagarr's answer too as it could change to b = np.column_stack((a, func(a))) Commented Mar 23, 2019 at 18:14
  • thanks guys. I managed to get another solution (eventually!!!). I use R most of the time and am trying to o python.... Commented Mar 23, 2019 at 18:28

2 Answers 2

2

np.column_stack can do the trick:

>>> a = np.array(range(100))
>>> b = np.column_stack((a, a**a))
>>> b
array([[                   0,                    1],
       [                   1,                    1],
       [                   2,                    4],
       [                   3,                   27],
       [                   4,                  256],
       [                   5,                 3125],
       [                   6,                46656],
       [                   7,               823543],
       [                   8,             16777216],
       [                   9,            387420489],
       [                  10,          10000000000],
       [                  11,         285311670611],
       [                  12,        8916100448256],
       [                  13,      302875106592253],
Sign up to request clarification or add additional context in comments.

Comments

1

A list comprehension could do that for you:

import numpy as np
results = np.array([[x, x**2] for x in range(100)])

That gives you an array of two columns:

Out[5]: 
array([[   0,    0],
       [   1,    1],
       [   2,    4],
       [   3,    9],
        ...

You've just commented that the function is more complex than just x**2 - one solution is simply to define it as a function, e.g.

import numpy as np

def func(x): # example functionality
    y = x**2
    y = y*2
    return y

results = np.array([[x, func(x)] for x in range(100)])

Out[13]: 
array([[    0,     0],
       [    1,     2],
       [    2,     8],
       [    3,    18],
       ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.