pythonnumpygraphlab

Graphlab and numpy issue


I'm currently doing a course on Coursera (Machine Leraning) offered by University of Washington and I'm facing little problem with the numpy and graphlab

The course requests to use a version of graphlab higher than 1.7 Mine is higher as you can see below, however, when I run the script below, I got an error as follows:

  [INFO] graphlab.cython.cy_server: GraphLab Create v2.1 started.
  def get_numpy_data(data_sframe, features, output):
      data_sframe['constant'] = 1
      features = ['constant'] + features # this is how you combine two lists
      # the following line will convert the features_SFrame into a numpy matrix:
      feature_matrix = features_sframe.to_numpy()
      # assign the column of data_sframe associated with the output to the SArray output_sarray

      # the following will convert the SArray into a numpy array by first converting it to a list
      output_array = output_sarray.to_numpy()
      return(feature_matrix, output_array)

     (example_features, example_output) = get_numpy_data(sales,['sqft_living'], 'price') # the [] around 'sqft_living' makes it a list
     print example_features[0,:] # this accesses the first row of the data the ':' indicates 'all columns'
     print example_output[0] # and the corresponding output

     ----> 8     feature_matrix = features_sframe.to_numpy()
     NameError: global name 'features_sframe' is not defined

The script above was written by the course authors, so I believe there is something I'm doing wrong

Any help will be highly appreciated.


Solution

  • You are supposed to complete the function get_numpy_data before running it, that's why you are getting an error. Follow the instructions in the original function, which actually are:

    def get_numpy_data(data_sframe, features, output):
        data_sframe['constant'] = 1 # this is how you add a constant column to an SFrame
        # add the column 'constant' to the front of the features list so that we can extract it along with the others:
        features = ['constant'] + features # this is how you combine two lists
        # select the columns of data_SFrame given by the features list into the SFrame features_sframe (now including constant):
    
        # the following line will convert the features_SFrame into a numpy matrix:
        feature_matrix = features_sframe.to_numpy()
        # assign the column of data_sframe associated with the output to the SArray output_sarray
    
        # the following will convert the SArray into a numpy array by first converting it to a list
        output_array = output_sarray.to_numpy()
        return(feature_matrix, output_array)