No module named ‘utilities‘如何解决?

在同文件目录下创建utilities.py文件

之后写入import utilities时既不会报错

个人思考是utilities.py是原作者自己定义的一个计算包,python官方中没有这个库的,pip是下载不了的,使用jupyter notebook时仍然会报错

utilities.py文件代码如下

import numpy as np
import matplotlib.pyplot as plt
from sklearn import model_selection

def load_data(input_file):
  X = []
  y = []
  with open(input_file, 'r') as f:
      for line in f.readlines():
          data = [float(x) for x in line.split(',')]
          X.append(data[:-1])
          y.append(data[-1])

  X = np.array(X)
  y = np.array(y)

  return X, y

def plot_classifier(classifier, X, y, title='Classifier boundaries', annotate=False):
  # define ranges to plot the figure
  x_min, x_max = min(X[:, 0]) - 1.0, max(X[:, 0]) + 1.0
  y_min, y_max = min(X[:, 1]) - 1.0, max(X[:, 1]) + 1.0

  # denotes the step size that will be used in the mesh grid
  step_size = 0.01

  # define the mesh grid
  x_values, y_values = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))

  # compute the classifier output
  mesh_output = classifier.predict(np.c_[x_values.ravel(), y_values.ravel()])

  # reshape the array
  mesh_output = mesh_output.reshape(x_values.shape)

  # Plot the output using a colored plot
  plt.figure()

  # Set the title
  plt.title(title)

  # choose a color scheme you can find all the options
  # here: http://matplotlib.org/examples/color/colormaps_reference.html
  plt.pcolormesh(x_values, y_values, mesh_output, cmap=plt.cm.GnBu)

  # Overlay the training points on the plot
  plt.scatter(X[:, 0], X[:, 1], c=y, s=80, edgecolors='black', linewidth=1, cmap=plt.cm.Paired)

  # specify the boundaries of the figure
  plt.xlim(x_values.min(), x_values.max())
  plt.ylim(y_values.min(), y_values.max())

  # specify the ticks on the X and Y axes
  plt.xticks(())
  plt.yticks(())

  if annotate:
      for x, y in zip(X[:, 0], X[:, 1]):
          # Full documentation of the function available here:
          # http://matplotlib.org/api/text_api.html#matplotlib.text.Annotation
          plt.annotate(
              '(' + str(round(x, 1)) + ',' + str(round(y, 1)) + ')',
              xy = (x, y), xytext = (-15, 15),
              textcoords = 'offset points',
              horizontalalignment = 'right',
              verticalalignment = 'bottom',
              bbox = dict(boxstyle = 'round,pad=0.6', fc = 'white', alpha = 0.8),
              arrowprops = dict(arrowstyle = '-', connectionstyle = 'arc3,rad=0'))

# Print performance metrics输出性能指标
def print_accuracy_report(classifier, X, y, num_validations=5):
  accuracy = model_selection.cross_val_score(classifier,
          X, y, scoring='accuracy', cv=num_validations)
  print ("Accuracy: " + str(round(100*accuracy.mean(), 2)) + "%")

  f1 = model_selection.cross_val_score(classifier,
          X, y, scoring='f1_weighted', cv=num_validations)
  print ("F1: " + str(round(100*f1.mean(), 2)) + "%")

  precision = model_selection.cross_val_score(classifier,
          X, y, scoring='precision_weighted', cv=num_validations)
  print ("Precision: " + str(round(100*precision.mean(), 2)) + "%")

  recall = model_selection.cross_val_score(classifier,
          X, y, scoring='recall_weighted', cv=num_validations)
  print ("Recall: " + str(round(100*recall.mean(), 2)) + "%")
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值