地块上的点未在直线上一一连接
Points on plot are not connectedd one by one in the line
我真的不知道为什么 matplotlib 以随机方式连接图上的点:
看起来不错,只有当我用 scatter()
函数绘制日期时:
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
np.random.seed(0)
n = 100
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure()
colors = ['teal', 'yellowgreen', 'gold', 'red']
lw = 2
plt.scatter(X_train, y_train, color='navy', s=30, marker='o', label="training points")
for count, degree in enumerate([1, 3, 6, 9]):
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(X_train[:, np.newaxis], y_train)
y_plot = model.predict(X_test[:, np.newaxis])
plt.plot(X_test[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
label="degree %d" % degree)
plt.legend(loc='lower right')
plt.show()
它们以随机顺序连接,因为它们是 给出 的随机顺序。您正在生成随机点,它们会在允许的范围内来回跳跃。如果你想让它们按升序排列,你需要先对它们进行排序。
没有问题的代码:
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
np.random.seed(0)
n = 100
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure()
colors = ['teal', 'yellowgreen', 'gold', 'red']
lw = 2
plt.scatter(train_data[0].values, train_data[1].values, color='navy', s=30, marker='o', label="training points")
# sorting values
train_data = pd.DataFrame(data = [X_train, y_train]).T.sort_values(0)
test_data = pd.DataFrame(data = [X_test, y_test]).T.sort_values(0)
for count, degree in enumerate([1, 3, 6, 9]):
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(train_data[0].values[:, np.newaxis], train_data[1].values)
y_plot = model.predict(test_data[0].values[:, np.newaxis])
plt.plot(test_data[0].values[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
label="degree %d" % degree)
plt.legend(loc='lower right')
plt.show()
结果:
我真的不知道为什么 matplotlib 以随机方式连接图上的点:
scatter()
函数绘制日期时:
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
np.random.seed(0)
n = 100
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure()
colors = ['teal', 'yellowgreen', 'gold', 'red']
lw = 2
plt.scatter(X_train, y_train, color='navy', s=30, marker='o', label="training points")
for count, degree in enumerate([1, 3, 6, 9]):
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(X_train[:, np.newaxis], y_train)
y_plot = model.predict(X_test[:, np.newaxis])
plt.plot(X_test[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
label="degree %d" % degree)
plt.legend(loc='lower right')
plt.show()
它们以随机顺序连接,因为它们是 给出 的随机顺序。您正在生成随机点,它们会在允许的范围内来回跳跃。如果你想让它们按升序排列,你需要先对它们进行排序。
没有问题的代码:
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
np.random.seed(0)
n = 100
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure()
colors = ['teal', 'yellowgreen', 'gold', 'red']
lw = 2
plt.scatter(train_data[0].values, train_data[1].values, color='navy', s=30, marker='o', label="training points")
# sorting values
train_data = pd.DataFrame(data = [X_train, y_train]).T.sort_values(0)
test_data = pd.DataFrame(data = [X_test, y_test]).T.sort_values(0)
for count, degree in enumerate([1, 3, 6, 9]):
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(train_data[0].values[:, np.newaxis], train_data[1].values)
y_plot = model.predict(test_data[0].values[:, np.newaxis])
plt.plot(test_data[0].values[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
label="degree %d" % degree)
plt.legend(loc='lower right')
plt.show()
结果: