import matplotlib.pyplot as plt
plt.plot(x_test,y_pred)
print(x_test)
print()
print(y_pred)
print()
print(y_test)
plt.plot(x_test,y_test)
plt.show()
我正在嘗試使用以上 3 個陣列繪制折線圖,??所有陣列的形狀都相同,但由于某種原因,折線圖變得混亂。我還是個菜鳥,誰能告訴我這個錯誤?
輸出:
[31.588117 53.66093226 46.68222865 43.10782022 70.34607562 44.49285588
57.5045333 36.93007661 55.80573336 38.95476907 56.9012147 56.86890066
34.3331247 59.04974121 57.78822399 54.28232871 51.0887199 50.28283635
44.21174175 38.00548801 32.94047994 53.69163957 68.76573427 46.2309665
68.31936082 50.03017434 49.23976534 50.03957594 48.14985889 25.12848465]
[ 48.07285783 77.96480957 68.51395026 63.67333329 100.56054527
65.5490079 83.16997871 55.30716832 80.86939107 58.04909356
82.35293882 82.30917777 51.79026483 85.26256544 83.55416479
78.80633125 74.48140943 73.39004741 65.1683111 56.76353649
49.90428565 78.00639472 98.42037934 67.90283173 97.8158813
73.0478817 71.97747598 73.06061375 70.50147799 39.32494706]
[ 50.39267014 63.64239878 72.24725107 57.81251298 104.25710159
86.64202032 91.486778 55.23166089 79.55043668 44.84712424
80.20752314 83.14274979 55.72348926 77.63418251 99.05141484
79.12064627 69.58889785 69.51050331 73.68756432 61.36690454
67.17065577 85.66820315 114.85387123 90.12357207 97.91982104
81.53699078 72.11183247 85.23200734 66.22495789 53.45439421]

uj5u.com熱心網友回復:
由于您的 x 軸未排序,因此該行會很亂。它準確地繪制了您告訴它繪制的內容。如果您需要折線圖,則需要先對資料進行排序(同時確保對其他向量進行相應排序)。例如,您可以使用 Pandas 輕松做到這一點。
import pandas as pd
df = pd.DataFrame({'x_test': x_test,
'y_test': y_test,
'y_pred': y_pred})
df.sort_values('x_test', inplace=True)
plt.plot(df.x_test, df.y_pred)
plt.plot(df.x_test, df.y_test)
plt.show()
線圖結果
或者,如前所述,您可能更喜歡散點圖:
plt.figure()
plt.plot(df.x_test, df.y_pred)
plt.scatter(df.x_test, df.y_test)
plt.show()
散點圖結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/394131.html
標籤:Python 麻木的 matplotlib 线性回归
上一篇:二叉樹每一片葉子的路徑
