美文网首页
Python机器学习TensorFlow的例子1(一)

Python机器学习TensorFlow的例子1(一)

作者: 玩er2017 | 来源:发表于2018-06-25 09:47 被阅读0次

我把蓝线屏蔽了。

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



dates = []
prices = []


# csv文件导入数据,用“/”隔开日期
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)
    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]
get_data('aapl.csv')

# print "Dates/ ", dates
# print "Prices/  ", prices

predicted_price = predict_price(dates, prices, 29)
print(predicted_price)

相关文章

网友评论

      本文标题:Python机器学习TensorFlow的例子1(一)

      本文链接:https://www.haomeiwen.com/subject/rpyeyftx.html