簡體   English   中英

如何使用舊數據框中的信息預測新Python數據框中的列值

[英]How to predict values of column in new Python data frame using info from the old data frame

假設我有兩個數據幀df1和df2。 在df1中,我有幾個列,例如userid,sexid,location等。在df2中,我有與df1中相同的列,除了sexid,我需要使用一些預測算法來填充。 我只是一個初學者,我嘗試了另一種問題。 因此,歡迎任何可能幫助我破解它的建議或有用的參考資料。

一個最小的例子:

import pandas as pd
from sklearn.linear_model import LogisticRegression

df1 = pd.DataFrame({'sexid': list('MMFFMFFMMF'), 'x1': [0, 12, 2, 3, 4, 2, 0, 12, 12, 12], 'x2': [0, 1, 1, 1, 0, 1, 1, 0, 0, 1]})

df2 = pd.DataFrame({'x1': [0, 12, 2, 3, 4, 2, 0, 12, 12, 12], 'x2': [0, 1, 1, 1, 0, 1, 1, 0, 0, 1]})

X = df1[['x1', 'x2']]
y = df1['sexid']

model = LogisticRegression()

model.fit(X, y)

model.predict(df2)

哪個回報:

array(['F', 'M', 'F', 'F', 'M', 'F', 'F', 'M', 'M', 'M'], dtype=object)

我強烈建議你讀這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM