已经安装,但是ModuleNotFoundError:没有名为“ sklearn”的模块

时间:2018-12-08 12:59:54

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

我很确定我已经安装了scikit learning,因为我已经在终端中尝试了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)

1 个答案:

答案 0 :(得分:-1)

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

python setup.py install 

有时候,通过pip安装软件包不会显示在Jupyter Notebook中,所以我更喜欢这种方式。

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