python - Graphlab 和 numpy 问题

标签 python numpy graphlab

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

类(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)

关于python - Graphlab 和 numpy 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40439636/

相关文章:

python - 使用正则表达式从大型 SFrame 或数据帧中提取信息,而不使用循环

algorithm - 用于分类的不平衡数据

python - 如何在 Python 中排序 xml 元素属性?

python - 是否可以从operator.methodcaller获取函数名称?

python - 使用 optparse 接受参数的大多数 pythonic 方式

python - 附加维度 numpy 数组

python - 在不安装 graphlab 的情况下在 SFrame 中分组

python - 无法使用openCV从图像中提取每个文本

python - 数据框查找行以返回索引

python - 对二维 numpy 数组中的每个 NXN 子数组执行计算的最快方法