蟒蛇错误。ImportError:没有名为 feature_format 的模块

我正在从 Udacity 课程中学习回归,"Intro to Machine Learning"并且正在完成其中的迷你项目。


我复制了项目的代码,编写了回归部分,作为代码的一部分,并尝试在 Python 2 中的 Jupyter Notebook 上运行它。不幸的是,我收到了以下错误。


    ImportError                               Traceback (most recent call last)

    <ipython-input-8-433cee6c5e25> in <module>()

         15 import pickle

         16 sys.path.append("../tools/")

    ---> 17 from feature_format import featureFormat, targetFeatureSplit

         18 dictionary = pickle.load( open("../final_project/final_project_dataset_modified.pkl", "r") )

         19 


    ImportError: No module named feature_format

我正在处理的代码如下:加载/格式化数据集的修改版本(为什么要修改?我们已经删除了一些麻烦点,你会发现自己在异常值迷你项目中)。绘制一个小散点图训练/测试数据n您在指示处填写回归代码:


    #!/usr/bin/python

    import sys

    import pickle

    sys.path.append("../tools/")

    from feature_format import featureFormat, targetFeatureSplit

    dictionary = pickle.load( open("../final_project/final_project_dataset_modified.pkl", "r") )


    ### list the features you want to look at--first item in the 

    ### list will be the "target" feature

    features_list = ["bonus", "salary"]

    data = featureFormat( dictionary, features_list, remove_any_zeroes=True)

    target, features = targetFeatureSplit( data )


    ### training-testing split needed in regression, just like classification

    from sklearn.cross_validation import train_test_split

    feature_train, feature_test, target_train, target_test = train_test_split(features, target, test_size=0.5, random_state=42)

    train_color = "b"

    test_color = "r"



    ### Your regression goes here!

    ### Please name it reg, so that the plotting code below picks it up and 

    ### plots it correctly. Don't forget to change the test_color above from "b" to

    ### "r" to differentiate training points from test points.

    from sklearn import linear_model

    reg = linear_model.LinearRegression()

    reg.fit(feature_train,target_train)



一只斗牛犬
浏览 105回答 2
2回答

交互式爱情

原因ImportError: No module named feature_format表示没有名为 的模块feature_format。该模块已在您正在使用的存储库中定义ud120-projects/tools/feature_format.py。解决方案 1模块的路径feature_format在第 3 行中声明k_means_cluster.py。作为传递给的参数sys.path.append。将路径从相对路径更改为绝对路径,例如sys.path.append("ud120-projects/tools/")。解决方案 2一切都好做这个简单的把戏。克隆 GitHub 存储库git&nbsp;clone&nbsp;https://github.com/udacity/ud120-projects.git转到ud120-projects/k_means/目录cd&nbsp;ud120-projects/k_means/然后是 python2 的 python 脚本python&nbsp;k_means_cluster.py

POPMUISE

尝试添加:import sys!{sys.executable} -m pip install feature_format在导入 feature_format 之前。这个“应该”安装它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python