0

I have been given a problem in Jupiter notebooks to code using python. This problem is about linear regression. It's as follows:

1: Linear Regression In this notebook we will generate data from a linear function: =+ and then solve for ̂ using OLS (ordinary least squares) and gradient descent.

Question 1.1 : Generate data: =+ Here we assume ≈(,)=+ where is linear in with additive noise Your function should have the following properties:

output y as an np.array with shape (M,1) generate_linear_y should work for any arbitrary x, b, and eps, as long as they are the appropriate dimensions do not use for-loops to calculate each y[i] separately, as this will be very slow for large M and N. Instead, you should leverage numpy linear algebra.


They expect us to write code as follows:

def generate_linear_y(X,b):
""" Write a function that generates m data points from inputs X and b

Parameters
----------
X :   numpy.ndarray
      x.shape must be (M,N)
      Each row of `X` is a single data point of dimension N
      Therefore `X` represents M data points

b :   numpy.ndarray
      b.shape must be (N,1)
      Each element of `b` is a value of beta such that b=[[b1][b2]...[bN]]


Returns
-------
y :   numpy.ndarray
      y.shape = (M,1)
      y[i] = X[i]b
"""

Can someone please assist me because I am thoroughly confused! I didn't even realize the things I am doing required array coding in python, which I always struggle with! Please help!

  • If I were you, SO is not my place to go. I will start reading Linear Model with Numpy and from there this problem becomes small see https://www.cs.toronto.edu/~frossard/post/linear_regression/ among other tutorials on LM with numpy – Prayson W. Daniel Apr 29 '20 at 05:09
  • @Prayson W. Daniel Thank you, I will check it out. SO was my last resort seeing as even my teachers weren't able to help me. – ah123412312 Apr 29 '20 at 05:14
  • 1
    That was a really helpful link, I really appreciate the help! – ah123412312 Apr 29 '20 at 05:24

1 Answers1

0

This looks like a direct matrix multiplication to me. In NumPy, this is implemented using the matrix multiplication operator @ (aka np.matmul).

To generate random noise, you can use the functions from numpy.random, most likely random_sample or standard_normal. If you want to do it the most-correct way, you can create a random number generator with default_rng, then use, for instance, rng.standard_normal.

jirassimok
  • 3,850
  • 2
  • 14
  • 23
  • can you please show me an example of the code, because I have never even heard of those functions. – ah123412312 Apr 29 '20 at 04:56
  • omg I know this is TMI but I'm about to cry, I feel so lost and confused and I've never done anything like this. I desperately need help, so I turned to stack overflow which im new to. – ah123412312 Apr 29 '20 at 05:00
  • @ah123412312 It sounds like you want to return either `X @ b` or that plus some noise. To get standard-normally-distributed noise of the same shape as your output, you could save that in a variable (`y1`), and use `np.random.standard_normal(y1.shape)`. – jirassimok Apr 29 '20 at 05:05
  • Thank you so much, it's working out! I really appreciate the help! – ah123412312 Apr 29 '20 at 05:21
  • @ah123412312 If my solution worked, please accept my answer. – jirassimok Apr 29 '20 at 05:32
  • sorry, I didn't realize I was supposed to do that, I did it though and thanks! – ah123412312 Apr 29 '20 at 16:24