预测苹果库存

时间:2019-04-05 05:57:31

标签: python pandas machine-learning scikit-learn non-linear-regression

我正在尝试创建一种机器学习模型来预测苹果股票,这是第一次尝试,并且实际上基于YouTube视频。但是我不太了解错误的原因。 我已经尝试过分离函数,但是要求重塑数组并使用numpy数组。

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

dta = pd.read_csv('aapl.csv')

dta.Date = dta.Date.apply(pd.to_datetime)
dates = np.array(dta.Date)
prices = np.array(dta.Open)

def predicted_prices(dates,prices,x):
    dates = np.reshape,(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.ylabel('Price')
    plt.title('Support Vector Regression')
    plt.legend()
    plt.show()

    return svr_rbf.predict(x)[0], svr_lin.predict(x)[0],svr_rbf.predict(x)[0]
predicted_prices(dates,prices,29)

我希望得到预测值的图形。

错误日志:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
 <ipython-input-1-6870461ae560> in <module>()
 33     return svr_rbf.predict(x)[0], svr_lin.predict(x)

[0],svr_rbf.predict(x)[0]
     34 
---> 35 predicted_prices(dates,prices,29)

<ipython-input-1-6870461ae560> in predicted_prices(dates, prices, x)
     17     svr_poly = SVR(kernel='poly', C = 1e3, degree = 2)
     18     svr_rbf =SVR(kernel = 'rbf', C=1e3, gamma=0.1)
---> 19     svr_lin.fit(dates, prices)
     20     svr_poly.fit(dates,prices)
     21     svr_rbf.fit(dates,prices)

C:\Users\chris\Anaconda2\lib\site-packages\sklearn\svm\base.pyc in fit(self, X, y, sample_weight)
    147         X, y = check_X_y(X, y, dtype=np.float64,
    148                          order='C', accept_sparse='csr',
--> 149                          accept_large_sparse=False)
    150         y = self._validate_targets(y)
    151 

C:\Users\chris\Anaconda2\lib\site-packages\sklearn\utils\validation.pyc in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    754                     ensure_min_features=ensure_min_features,
    755                     warn_on_dtype=warn_on_dtype,
--> 756                     estimator=estimator)
    757     if multi_output:
    758         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

C:\Users\chris\Anaconda2\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    525             try:
    526                 warnings.simplefilter('error', ComplexWarning)
--> 527                 array = np.asarray(array, dtype=dtype, order=order)
    528             except ComplexWarning:
    529                 raise ValueError("Complex data not supported\n"

C:\Users\chris\Anaconda2\lib\site-packages\numpy\core\numeric.pyc in asarray(a, dtype, order)
    499 
    500     """
--> 501     return array(a, dtype, copy=False, order=order)
    502 
    503 

TypeError: float() argument must be a string or a number

1 个答案:

答案 0 :(得分:0)

我发现的错误(请提供错误日志):

这显然是一个错误

dates = np.reshape,(len(dates),1)

应该类似于:

dates = np.reshape(dates, (len(dates),1))

此外,当您安装SVR时,您会在X上输入日期,在Y上输入价格。这使算法学会了预测使用日期。您确定这是预测价格的最佳功能吗?也许您应该使用其他股票或业务指标。

希望这会有所帮助。

P.D:请报告错误日志,我们将竭诚为您服务。