_ml.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:pydatalab 作者: googledatalab 项目源码 文件源码
def _predict(args, cell):
  schema, features = _local_predict.get_model_schema_and_features(args['model'])
  headers = [x['name'] for x in schema]
  img_cols = []
  for k, v in six.iteritems(features):
    if v['transform'] in ['image_to_vec']:
      img_cols.append(v['source_column'])

  data = args['data']
  df = _local_predict.get_prediction_results(
      args['model'], data, headers, img_cols=img_cols, cloud=False,
      show_image=not args['no_show_image'])

  def _show_img(img_bytes):
    return '<img src="data:image/png;base64,' + img_bytes + '" />'

  def _truncate_text(text):
    return (text[:37] + '...') if isinstance(text, six.string_types) and len(text) > 40 else text

  # Truncate text explicitly here because we will set display.max_colwidth to -1.
  # This applies to images to but images will be overriden with "_show_img()" later.
  formatters = {x: _truncate_text for x in df.columns if df[x].dtype == np.object}
  if not args['no_show_image'] and img_cols:
    formatters.update({x + '_image': _show_img for x in img_cols})

  # Set display.max_colwidth to -1 so we can display images.
  old_width = pd.get_option('display.max_colwidth')
  pd.set_option('display.max_colwidth', -1)
  try:
    IPython.display.display(IPython.display.HTML(
        df.to_html(formatters=formatters, escape=False, index=False)))
  finally:
    pd.set_option('display.max_colwidth', old_width)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号