python-3.x - 已安装但 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)
解决方案
您应该使用 Anaconda 而不是 Miniconda 在开始阶段默认安装所有依赖项。此外,如果你想安装所有依赖项并在所有编辑器中使用,最好从 PyPy 和 goto 文件夹下载包并运行命令:
python setup.py install
有时通过 pip 安装包不会出现在 Jupyter Notebook 中,所以我更喜欢这种方式。
此外,您的代码有错误,因为 svr_rbf 和 svr_lin 在 plt.plot 中无法访问。
推荐阅读
- sql - 从两个表中获取数据不起作用 SQL
- r - 使用 Shiny Server 和 Docker 将应用程序日志记录到标准输出
- oracle - 错误(9,1):PLS-00428:在创建存储过程时,此 SELECT 语句中需要一个 INTO 子句
- windows - 即使被告知不要,Visual Studio 也会下载并安装更新
- typeorm - TypeORM: Migrate THEN 生成新的迁移?
- listview - Xamarin Forms - 标题之间的分组 CollectionView 空间
- javascript - 更新 VSCode 扩展时显示消息
- javascript - XHR 工作时获取 API 文件上传失败
- javascript - 我无法将 Mock 数据中的图片传递给组件 ReactJS
- lua - 通过字符串化引用而不是直接通过引用传递 Lua 表