首页 > 解决方案 > 已安装但 ModuleNotFoundError: No module named 'sklearn'

问题描述

我很确定我已经安装了 scikit learn 因为我已经在终端中尝试过 pip install 和 Conda 并且我收到了消息"# All requested packages already installed."但是当我在 Python 3.7.1 中运行我的代码时我不断收到错误"ModuleNotFoundError: No module named 'sklearn'"

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)

标签: python-3.xmatplotlibscikit-learnlinear-regressionsklearn-pandas

解决方案


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

python setup.py install 

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

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


推荐阅读