gpt4 book ai didi

python - Jupyter notebook 挂着 sklearn 返回?

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:22 27 4
gpt4 key购买 nike

https://github.com/llSourcell/predicting_stock_prices/blob/master/demo.py 复制代码当我在 jupyter notebook 中运行它时,它挂起并卡在最后一行。我在笔记本和下载文件夹中有 .csv...不确定是否是错误

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


#plt.switch_backend('newbackend')

dates = []
prices = []

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

def predict_price(dates, prices, x):
dates = np.reshape(dates,(len(dates), 1)) # converting to matrix of n X 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) # defining the support vector regression models
svr_rbf.fit(dates, prices) # fitting the data points in the models
svr_lin.fit(dates, prices)
svr_poly.fit(dates, prices)

plt.scatter(dates, prices, color= 'black', label= 'Data') # plotting the initial datapoints
plt.plot(dates, svr_rbf.predict(dates), color= 'red', label= 'RBF model') # plotting the line made by the RBF kernel
plt.plot(dates,svr_lin.predict(dates), color= 'green', label= 'Linear model') # plotting the line made by linear kernel
plt.plot(dates,svr_poly.predict(dates), color= 'blue', label= 'Polynomial model') # plotting the line made by polynomial kernel
plt.xlabel('Date')
plt.ylabel('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('table.csv') # calling get_data method by passing the csv file to it

predicted_price = predict_price(dates, prices, 29)

我已将代码划分为 jupyter 中的单元格和 <br/>
predicted_price
似乎挂起 In [*]:

最佳答案

代码很好。 SVR 需要时间来计算。了解更多 here .您可以使用线性回归尝试以下代码。

导入

from sklearn import linear_model

# defining the linear regression model
linear_mod = linear_model.LinearRegression()

# fitting the data points in the model
linear_mod.fit(dates, prices)

plt.scatter(dates, prices, color='black', label='Data')
# plotting the initial datapoints
plt.plot(dates, linear_mod.predict(dates), color='red',
label='Linear model')

关于python - Jupyter notebook 挂着 sklearn 返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768213/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com