在Python中转换新数据集以进行预测

时间:2016-02-29 16:39:29

标签: python dataset transform

我训练模型(对于充足的 linear_model.LinearRegression )进行一些迭代,例如*pd.get_dummies*

我得到了新的数据结构 现在我采用一个新的数据集&想要预测。我不能使用 predict ,因为结构不同。 *pd.get_dummies*新数据会为我们提供另外数量的列

如何转换此数据集? 通过appendind到先前的数据集并再次使用新数据进行训练? 或者我可以使用“转换”来获取新数据吗?

 import pandas as pd 
import numpy as np
from sklearn import linear_model
df1 = pd.DataFrame({ 'y' : np.array([1., 1., 2., 3., 1.] ,dtype='int32'),
   ....:                      'X' : np.array(["1","1","2","2", "1"])})
y = df1[df1.columns[0]]
X = pd.get_dummies(df1['X'])
lr = linear_model.LinearRegression()
lr = lr.fit(X, y)
lr.predict(X)

现在我有

df2 = pd.DataFrame({ 'y' : 'nan',
   ....:                      'X' : np.array(["3"])})
Xnew = pd.get_dummies(df2['X'])
lr.predict(Xnew)
ValueError: shapes (1,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)

1 个答案:

答案 0 :(得分:0)

我是这样看的

import numpy as np
import pandas as pd
from sklearn import linear_model, metrics, pipeline, preprocessing
df = pd.DataFrame({'a':range(12), 'b':[1,2,3,1,2,3,1,2,3,3,1,2], 'c':['a', 'b', 'c']*4, 'd': ['m', 'f']*6})
y = df.a
num = df[['b']]
cat = df[['c', 'd']]
from sklearn.feature_extraction import DictVectorizer
enc = DictVectorizer(sparse = False)
enc_data = enc.fit_transform(cat .T.to_dict().values())
crat = pd.DataFrame(enc_data,  columns=enc.get_feature_names())
X = pd.concat([crat, num], axis=1)
cat_columns = ['c=a', 'c=b', 'c=c', 'd=f', 'd=m'] 
cat_indices = np.array([(column in cat_columns) for column in X.columns], dtype = bool)
numeric_col = ['b']
num_indices = np.array([(column in numeric_col) for column in X.columns], dtype = bool)
reg = linear_model.SGDRegressor()
estimator = pipeline.Pipeline(steps = [       
    ('feature_processing', pipeline.FeatureUnion(transformer_list = [        
            ('categorical', preprocessing.FunctionTransformer(lambda data: data[:, cat_indices])), 

            #numeric
            ('numeric', pipeline.Pipeline(steps = [
                ('select', preprocessing.FunctionTransformer(lambda data: data[:, num_indices])),
                ('scale', preprocessing.StandardScaler())            
                        ]))
        ])),
    ('model', reg)
    ]
)
estimator.fit(X, y)

现在我们使用新的数据集

test = pd.DataFrame({ 'b':[1], 'c':['a'], 'd': ['f']})
cat = test[['c', 'd']]
num = test[['b']]
enc_data = enc.transform(cat.T.to_dict().values())
crat = pd.DataFrame(enc_data,  columns=enc.get_feature_names())
test = pd.concat([crat, num], axis=1)
estimator.predict(test)