【发布时间】:2017-07-13 01:22:44
【问题描述】:
我现在正在使用回归并尝试将多项式模型拟合到具有 3 个不同度数的数据中,并且它仅绘制最低度数。我不知道我哪里出错了。这是我的代码和数据点:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
import time
def main():
inputfile = "statistics.txt"
X= np.loadtxt(inputfile, delimiter=",",dtype=np.str, usecols=[0])
X= [dt.datetime.strptime(date, '%d.%m.%Y') for date in X]
X = mdates.date2num(X)
Y= np.loadtxt(inputfile, delimiter=",", usecols=[1])
num_training = int(0.9*len(X))
num_test = len(X) - num_training
X_train, Y_train = X[:num_training], Y[:num_training]
X_test, Y_test = X[num_training:], Y[num_training:]
plt.scatter(X_train, Y_train, color="blue",s=10, marker='o')
plt.title("Euro Swedish Krona Exchange rate")
plt.xlabel("Time in months from April to June in 2017")
plt.ylabel("Exhange rate")
colors = ['teal', 'yellowgreen', 'gold']
for count, degree in enumerate([2, 3, 4]):
coeffs = np.polyfit(X_train, Y_train, degree)
f = np.poly1d(coeffs)
x_line = np.linspace(X[0], X[-1], 50)
x_line_plot = mdates.num2date(x_line)
y_line = f(x_line)
plt.plot(x_line_plot, y_line, color=colors[count], linewidth=2, label="degree {}".format(degree))
print(coeffs)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.grid()
plt.legend(loc='upper left')
plt.show()
if __name__ == '__main__':
main()
-----statistics.txt-----
11.04.2017,9.6059
12.04.2017,9.5741
13.04.2017,9.5976
14.04.2017,9.5892
17.04.2017,9.5763
18.04.2017,9.6101
19.04.2017,9.6107
20.04.2017,9.6309
21.04.2017,9.6611
24.04.2017,9.6266
25.04.2017,9.5858
26.04.2017,9.5551
27.04.2017,9.6070
28.04.2017,9.6474
01.05.2017,9.6438
02.05.2017,9.6220
03.05.2017,9.6326
04.05.2017,9.7007
05.05.2017,9.6669
08.05.2017,9.6616
09.05.2017,9.6649
10.05.2017,9.6974
11.05.2017,9.6489
12.05.2017,9.6480
15.05.2017,9.6903
16.05.2017,9.7402
17.05.2017,9.7432
18.05.2017,9.7797
19.05.2017,9.7800
22.05.2017,9.7683
23.05.2017,9.7363
24.05.2017,9.7255
25.05.2017,9.7378
26.05.2017,9.7233
29.05.2017,9.7138
30.05.2017,9.7580
31.05.2017,9.7684
01.06.2017,9.7402
02.06.2017,9.7256
05.06.2017,9.7388
06.06.2017,9.7707
07.06.2017,9.7833
08.06.2017,9.7685
09.06.2017,9.7579
12.06.2017,9.7980
13.06.2017,9.7460
14.06.2017,9.7634
15.06.2017,9.7540
16.06.2017,9.7510
19.06.2017,9.7475
20.06.2017,9.7789
21.06.2017,9.7676
22.06.2017,9.7581
23.06.2017,9.7629
26.06.2017,9.7537
27.06.2017,9.7647
28.06.2017,9.7213
29.06.2017,9.6806
30.06.2017,9.6309
03.07.2017,9.6479
04.07.2017,9.6740
05.07.2017,9.6332
06.07.2017,9.6457
07.07.2017,9.6084
10.07.2017,9.6101
11.07.2017,9.6299
我认为它与日期有关,因为我的情节没有日期。我的代码中也可能有太多东西。也很奇怪,通过改变度数我有时会得到 3 条曲线,有时只有 1 条。
【问题讨论】:
-
您可以向我们展示您的 .txt 文件,并解释您遇到的错误。
-
如果你放大你的图,你会看到它确实绘制了所有三个,但是它们非常接近。
标签: python numpy matplotlib