Skip to main content
 首页 » 编程设计

python之Graphlab 和 numpy 问题

2024年10月17日8xiaohuochai

我目前正在学习华盛顿大学提供的 Coursera(机器学习)类(class),我在使用 numpy 和 graphlab 时遇到了一些小问题

类(class)要求使用1.7以上的graphlab版本 我的更高,如下所示,但是,当我运行下面的脚本时,出现如下错误:

  [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 

上面的脚本是类(class)作者写的,所以我相信我做错了什么

任何帮助将不胜感激。

请您参考如下方法:

您应该在运行函数 get_numpy_data 之前完成它,这就是您收到错误的原因。按照原始函数中的说明进行操作,实际上是:

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)