Ich habe einen Pandas-Datenrahmen, in dem die Werte in einer Spalte gruppiert werden, um Untermodelle zu erstellen.Untermodelle mit pandas groupby erstellen und jedes Modell mit Testdaten lokalisieren
import pandas as pd
from sklearn.linear_model import Ridge
data = pd.DataFrame({"Name": ["A", "A", "A", "B", "B", "B"], "Score": [90, 80, 90, 92, 87, 80], "Age": [10, 12, 14, 9, 11, 12], "Training": [0, 1, 2, 0, 1, 2]})
"Name"
wird als Grundlage verwendet Submodell für jeden einzelnen zu schaffen. Ich möchte die Variablen "Age"
und "Training"
verwenden, um "Score"
einer einzelnen "Name"
(d. H. "A"
und "B"
in diesem Fall) vorherzusagen. Das heißt, wenn ich "A"
habe und die "Age"
und "Training"
von "A"
kenne, würde ich gerne "A"
, "Age"
, "Training"
verwenden, um "Score"
vorherzusagen. Jedoch sollte "A"
verwendet werden, um auf das Modell zuzugreifen, dass "A"
zu anderem als anderem Modell gehört.
grouped_df = data.groupby(['Name'])
for key, item in grouped_df:
Score = grouped_df['Score']
Y = grouped_df['Age', 'Training']
Score_item = Score.get_group(key)
Y_item = Y.get_group(key)
model = Ridge(alpha = 1.2)
modelfit = model.fit(Y_item, Score_item)
modelpred = model.predict(Y_item)
modelscore = model.score(Y_item, Score_item)
print modelscore
Bis hierher habe ich einfach Ridge Modelle Untergruppen A
und B
gebaut.
Meine Frage ist, mit Testdaten wie folgt:
test_data = [u"A, 13, 0", u"B, 12, 1", u"A 10, 0"] ##each element, respectively, represents `Name`, `Age` and `Training`
Wie die Daten an die Vorhersagemodelle zu füttern? Ich habe
line = test_data
Name = [line[i].split()[0] for i in range(len(line))]
Age = [line[i].split()[1] for i in range(len(line))]
Training = [line[i].split()[2] for i in range(len(line))]
Y = pd.DataFrame({"Name": Name, "Age": Age, "Training": Training})
Das gibt mir die Pandas Datenrahmen der Testdaten. Ich bin mir jedoch nicht sicher, wie ich weiter vorgehen soll, um die Testdaten dem Modell zuzuführen. Ich schätze Ihre Hilfe sehr. Vielen Dank!!
UPDATE
Nachdem ich den Code von Parfait angenommen, sieht der Code jetzt besser. Hier habe ich jedoch keinen anderen Pandas-Datenrahmen der Testdaten erstellt (da ich mir nicht sicher bin, wie ich mit der Zeile umgehen soll). Stattdessen füge ich die Testwerte ein, indem ich die Strings aufspalte. Ich habe einen Fehler wie unten angegeben erhalten. Ich suchte und fand hier einen Beitrag Preprocessing in scikit learn - single sample - Depreciation warning, der verwandt ist. Allerdings habe ich versucht, die Testdaten umzuformen, aber es ist auf dem Listenformular, so dass es nicht das Attribut der Umformung hat. Ich glaube, ich verstehe das falsch. Ich schätze es sehr, wenn Sie mir mitteilen können, wie Sie diesen Fehler beheben können. Vielen Dank.
import pandas as pd
from sklearn.linear_model import Ridge
import numpy as np
data = pd.DataFrame({"Name": ["A", "A", "A", "B", "B", "B"], "Score": [90, 80, 90, 92, 87, 80], "Age": [10, 12, 14, 9, 11, 12], "Training": [0, 1, 2, 0,$
modeldict = {} # INITIALIZE DICT
grouped_df = data.groupby(['Name'])
for key, item in grouped_df:
Score = grouped_df['Score']
Y = grouped_df['Age', 'Training']
Score_item = Score.get_group(key)
Y_item = Y.get_group(key)
model = Ridge(alpha = 1.2)
modelfit = model.fit(Y_item, Score_item)
modelpred = model.predict(Y_item)
modelscore = model.score(Y_item, Score_item)
modeldict[key] = modelfit # SAVE EACH FITTED MODEL TO DICT
line = [u"A, 13, 0", u"B, 12, 1", u"A, 10, 0"]
Name = [line[i].split(",")[0] for i in range(len(line))]
Age = [line[i].split(",")[1] for i in range(len(line))]
Training = [line[i].split(",")[2] for i in range(len(line))]
for i in range(len(line)):
Name = line[i].split(",")[0]
Age = line[i].split(",")[1]
Training = line[i].split(",")[2]
model = modeldict[Name]
ip = [float(Age), float(Training)]
score = model.predict(ip)
print score
ERROR
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
86.6666666667
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.DeprecationWarning)
83.5320600273
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.DeprecationWarning)
86.6666666667
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.DeprecationWarning)
[ 86.66666667]
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.DeprecationWarning)
[ 83.53206003]
/opt/conda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
[ 86.66666667]