【问题标题】:already installed but ModuleNotFoundError: No module named 'sklearn'已安装但 ModuleNotFoundError: No module named 'sklearn'
【发布时间】:2019-05-10 00:03:31
【问题描述】:

我很确定我已经安装了 scikit learn,因为我在终端中尝试过 pip install 和 Conda,我收到消息 "# All requested packages already installed." 但是当我在 Python 3.7.1 中运行我的代码时,我一直收到错误 @987654322 @

import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt

dates = []
prices = []

def get_data(filename):
    with open(filename, 'r') as csvfile:
        csvFileReader = csv.reader(csvfile)
        next(csvFileReader)
        for row in csvFileReader:
            dates.append(float(row[0].split('-')[0]))
            prices.append(float(row[1]))
    return

def predict_prices(dates, prices, x):
    dates = np.reshape(dates,(len(dates), 1))
    svr_lin = SVR(kernel = 'linear', C = 1e3)
    svr_poly = SVR(kernel = 'poly', C = 1e3, degree = 2)
    svr_rbf = SVR(kernel = 'rbf', C = 1e3, gamma = 0.1)
    svr_lin.fit(dates, prices)
    svr_poly.fit(dates, prices)
    svr_rbf.fit(dates, Prices)

plt.scatter(dates, prices, color = 'black', label = 'Data')
plt.plot(dates, svr_rbf.predict(dates), color = 'red', label = 'RBF model')
plt.plot(dates, svr_lin.predict(dates), color = 'green', label = 'Linear model')
plt.plot(dates, svr_poly.predict(dates), color = 'blue', label = 'Polynomial model')
plt.xlabel('Date')
plt.title('Price')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]

get_data('EURUSD4h.csv')

predictedPrice = predict_prices(dates, prices, 29)
print(predictedPrice)

【问题讨论】:

  • 试试sudo pip3 install scikit-learnpip3 install scikit-learn
  • 已满足要求:.​​/miniconda3/lib/python3.7/site-packages (0.20.1) 中的 scikit-learn 已满足要求:.​​/miniconda3/lib 中的 numpy>=1.8.2 /python3.7/site-packages(来自 scikit-learn)(1.15.4)要求已经满足:./miniconda3/lib/python3.7/site-packages 中的 scipy>=0.13.3(来自 scikit-learn)( 1.1.0)
  • 您使用的是哪个编辑器?
  • 只使用python 3.7.1 IDLE

标签: python-3.x matplotlib scikit-learn linear-regression sklearn-pandas


【解决方案1】:

您应该使用 Anaconda 而不是 Miniconda 在开始阶段默认安装所有依赖项。 另外,如果你想安装所有依赖项并在所有编辑器中使用,最好从 PyPy 和 goto 文件夹下载包并运行命令:

python setup.py install 

有时通过 pip 安装包不会出现在 Jupyter Notebook 中,所以我更喜欢这种方式。

另外,您的代码有错误,因为 svr_rbf 和 svr_lin 在 plt.plot 中无法访问。

【讨论】:

    猜你喜欢
    • 2018-12-17
    • 2019-02-07
    • 2021-02-18
    • 2022-01-24
    • 2021-02-04
    • 2018-06-12
    • 2022-06-30
    • 2020-03-17
    • 2018-11-23
    相关资源
    最近更新 更多