[英]No line of regression appear when I use regplot and lmplot for seaborn using imported CSV data
我想為東京多年的租金創建一個線性回歸模型。 到目前為止,我已經設法用 seaborn 繪制散點圖。 但是,當我嘗試進行線性回歸時,錯誤 UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('< U32') 彈出,沒有回歸線(但散點圖似乎工作正常)。
我的代碼
df = pd.read_csv('Tokyo rent (One Bedroom apartment in the city centre).csv', thousands=',')
g = sns.lmplot(x='Date 2.0', y='Rent(Local Currency)', data=df)
g.figure.autofmt_xdate()
我的 CSV 文件
Year Rent(USD) Date Rent(Local Currency) Date 2.0
2017 1223.4 17/03/17 129,594.59 2017-03-17
2017 1070.24 28/10/17 121,656.25 2017-10-28
2018 1104.51 23/01/18 121,689.66 2018-01-23
2018 1030.61 22/10/18 116,270.83 2018-10-22
2019 1124.33 14/06/19 122,062.50 2019-06-14
2019 1129.6 20/06/19 121,255.32 2019-06-20
2019 1129.9 21/06/19 121,255.32 2019-06-21
2020 1198.53 23/03/20 128,701.75 2020-03-23
2020 1183.66 01/07/20 127,195.65 2020-07-01
2020 1213.38 17/09/20 127,466.67 2020-09-17
2020 1168.37 05/10/20 123,578.95 2020-10-05
2020 1192.5 11/11/20 125,525.00 2020-11-11
2020 1228.34 02/12/20 128,312.50 2020-12-02
2021 1220 06/03/21 132,200.00 2021-03-06
2021 1342.84 29/08/21 147,524.40 2021-08-29
2021 1284.65 14/10/21 145,696.54 2021-10-14
我的散點圖結果(抱歉日期被壓縮)
在這種情況下,x 軸是一個時間序列,因此一旦我們將其轉換為 matplotlib 的日期數字格式,我們就可以顯示回歸線。 然后,您可以將 x 軸顯示更改為時間序列。
df['Date 2.0'] = pd.to_datetime(df['Date 2.0'])
df['Date 2.0'] = mdates.date2num(df['Date 2.0'])
import seaborn as sns
g = sns.lmplot(x='Date 2.0', y='Rent(Local Currency)', data=df)
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
g.ax.xaxis.set_major_locator(locator)
g.ax.xaxis.set_major_formatter(formatter)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.