Почему я не могу предсказать напрямую, используя Матрицу возможностей? - PullRequest
0 голосов
/ 30 августа 2018

[решено] В приведенном ниже процессе я обрабатываю свои новые данные и пытаюсь предсказать, но не могу использовать данные и мою обученную модель.

Сначала я импортирую,

import pandas as pd
from sklearn import preprocessing
import sklearn.model_selection as ms
from sklearn import linear_model
import sklearn.metrics as sklm
import numpy as np
import numpy.random as nr
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as ss
import math

%matplotlib inline

Импорт данных и обработка данных

##test
##prepare test_data
x_test_data = pd.read_csv('AW_test.csv')
x_test_data.loc[:,x_test_data.dtypes==object].isnull().sum()

##dropnan
cols_of_interest = ['Title','MiddleName','Suffix','AddressLine2']
x_test_data.drop(cols_of_interest,axis=1,inplace=True)

##dropduplicate
x_test_data.drop_duplicates(subset = 'CustomerID', keep = 'first', 
inplace=True)
print(x_test_data.shape)

Затем я преобразовываю свои свойства категориальных переменных в матрицы с горячим кодированием

##change categorical variables to numeric variables
def encode_string(cat_features):
    enc = preprocessing.LabelEncoder()
    enc.fit(cat_features)
    enc_cat_features = enc.transform(cat_features)
    ohe = preprocessing.OneHotEncoder()
    encoded = ohe.fit(enc_cat_features.reshape(-1,1))
    return encoded.transform(enc_cat_features.reshape(-1,1)).toarray()

categorical_columns = 
['CountryRegionName','Education','Occupation','Gender','MaritalStatus']
Features = encode_string(x_test_data['CountryRegionName'])
for col in categorical_columns:
    temp = encode_string(x_test_data[col])
    Features = np.concatenate([Features, temp],axis=1)
print(Features)

Затем я добавляю остальные числовые элементы в матрицы

##add numeric variables
Features = np.concatenate([Features, 
np.array(x_test_data[['HomeOwnerFlag','NumberCarsOwned',
'TotalChildren','YearlyIncome']])], axis=1)

Далее я масштабирую матрицы элементов

##scale numeric variables
with open('./lin_reg_scaler.pickle', 'rb') as file:
scaler =pickle.load(file)
Features[:,-5:] = scaler.transform(Features[:,-5:])

Я загружаю модель линейной регрессии, которую я обучил, в другом файле (при необходимости я могу опубликовать ее)

# Loading the saved linear regression model pickle
import pickle
loaded_model = pickle.load(open('./lin_reg_mod.pickle', 'rb'))

Я поместил свои матрицы функций прямо в

#predict
loaded_model.predict(Features)

Тем не менее, это то, что я получил

array([-5.71697209e+12, -4.64634881e+12, -4.64634881e+12, -4.64634881e+12,
   -4.64634881e+12, -4.64634881e+12, -5.71697209e+12, -4.64634881e+12,
   -5.71697209e+12, -4.64634881e+12, -5.71697209e+12, -4.64634881e+12,
   -4.64634881e+12, -4.64634881e+12, -5.71697209e+12, -4.64634881e+12,
   -4.64634881e+12, -5.71697209e+12, -5.71697209e+12, -5.71697209e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -4.64634881e+12,
   -4.64634881e+12, -5.71697209e+12, -4.64634881e+12, -5.71697209e+12,
   -5.71697209e+12, -4.64634881e+12, -5.71697209e+12, -5.71697209e+12,
   -4.64634881e+12, -5.71697209e+12, -4.64634881e+12, -5.71697209e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -4.64634881e+12,
   -5.71697209e+12, -5.71697209e+12, -4.64634881e+12, -4.64634881e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -5.71697209e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -4.64634881e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -4.64634881e+12,
   -4.64634881e+12, -5.71697209e+12, -4.64634881e+12, -5.71697209e+12,
   -4.64634881e+12, -4.64634881e+12, -4.64634881e+12, -5.71697209e+12,
   -5.71697209e+12, -5.71697209e+12, -5.71697209e+12, -4.64634881e+12,............

В моем другом файле я успешно обучил свою модель и протестировал ее с моими тестовыми данными.

Это то, что я получил при вводе x_test в мою модель в этом файле (результат, который я хочу получить):

[83.75482221 66.31820493 47.22211384 ... 69.65032224 88.45908874
  58.45193545]

Понятия не имею, что происходит, может кто-нибудь помочь плз

[ОБНОВЛЕНИЕ] Ниже приведен мой код для обучения модели

custs = pd.read_csv('combined_custs.csv')
custs.dtypes

##avemonthspend data
ams = pd.read_csv('AW_AveMonthSpend.csv')
ams.drop_duplicates(subset='CustomerID', keep='first', inplace=True)
##merge
combined_custs=custs.merge(ams)
combined_custs.to_csv('./ams_combined_custs.csv')
combined_custs.head(20)
##change categorical variables to numeric variables
def encode_string(cat_features):
enc = preprocessing.LabelEncoder()
enc.fit(cat_features)
enc_cat_features = enc.transform(cat_features)
ohe = preprocessing.OneHotEncoder()
encoded = ohe.fit(enc_cat_features.reshape(-1,1))
return encoded.transform(enc_cat_features.reshape(-1,1)).toarray()

categorical_columns = 
['CountryRegionName','Education','Occupation','Gender','MaritalStatus']
Features = encode_string(combined_custs['CountryRegionName'])
for col in categorical_columns:
    temp = encode_string(combined_custs[col])
    Features = np.concatenate([Features, temp],axis=1)
print(Features.shape)
print(Features[:2,:])

##add numeric variables
Features = np.concatenate([Features, 


np.array(combined_custs[['HomeOwnerFlag',
'NumberCarsOwned','TotalChildren','YearlyIncome']])], axis=1)

print(Features.shape)
print(Features)

##train_test_split
nr.seed(9988)
labels = np.array(combined_custs['AveMonthSpend'])
indx = range(Features.shape[0])
indx = ms.train_test_split(indx, test_size = 300)
x_train = Features[indx[0],:]
y_train = np.ravel(labels[indx[0]])
x_test = Features[indx[1],:]
y_test = np.ravel(labels[indx[1]])
print(x_test.shape)

##scale numeric variables
scaler = preprocessing.StandardScaler().fit(x_train[:,-5:])

x_train[:,-5:] = scaler.transform(x_train[:,-5:])
x_test[:,-5:] = scaler.transform(x_test[:,-5:])
x_train[:2,]

import pickle
file = open('./lin_reg_scaler.pickle', 'wb')
pickle.dump(scaler, file)
file.close()

##define and fit the linear regression model
lin_mod = linear_model.LinearRegression(fit_intercept=False)
lin_mod.fit(x_train,y_train)
print(lin_mod.intercept_)
print(lin_mod.coef_)

import pickle
file = open('./lin_reg_mod.pickle', 'wb')
pickle.dump(lin_mod, file)
file.close()

lin_mod.predict(x_test)

И прогноз для моей тренировочной модели:

array([ 78.20673535,  91.11860042,  75.27284767,  63.69507673,
   102.10758616,  74.64252358,  92.84218321,  77.9675721 ,
   102.18989779,  96.98098962,  87.61415378,  39.37006326,
    85.81839618,  78.41392293,  45.49439829,  48.0944897 ,
    36.06024114,  70.03880373, 128.90267485,  54.63235443,
    52.20289729,  82.61123334,  41.58779815,  57.6456416 ,
    46.64014991,  78.38639454,  77.61072157,  94.5899366 ,.....

1 Ответ

0 голосов
/ 30 августа 2018

Вы используете этот метод как для обучения, так и для тестирования:

def encode_string(cat_features):
    enc = preprocessing.LabelEncoder()
    enc.fit(cat_features)
    enc_cat_features = enc.transform(cat_features)
    ohe = preprocessing.OneHotEncoder()
    encoded = ohe.fit(enc_cat_features.reshape(-1,1))
    return encoded.transform(enc_cat_features.reshape(-1,1)).toarray()

по телефону:

Features = encode_string(combined_custs['CountryRegionName'])
for col in categorical_columns:
    temp = encode_string(combined_custs[col])
    Features = np.concatenate([Features, temp],axis=1)

Но, как я уже сказал в моем комментарии выше, вам нужно применить к тесту ту же предварительную обработку, что и в поезде.

Вот что происходит, во время тестирования, в зависимости от порядка данных в x_test_data, кодировка меняется. Поэтому, возможно, строковое значение, которое получило число 0, во время обучения теперь получает число 1, и порядок элементов в вашем окончательном Features меняется.

Чтобы решить эту проблему, вам нужно сохранить LabelEncoder и OneHotEncoder для каждого столбца отдельно.

Итак, во время тренировки сделайте следующее:

import pickle
def encode_string(cat_features):
    enc = preprocessing.LabelEncoder()
    enc.fit(cat_features)
    enc_cat_features = enc.transform(cat_features)

    # Save the LabelEncoder for this column
    encoder_file = open('./'+cat_features+'_encoder.pickle', 'wb')
    pickle.dump(lin_mod, encoder_file)
    encoder_file.close()

    ohe = preprocessing.OneHotEncoder()
    encoded = ohe.fit(enc_cat_features.reshape(-1,1))

    # Same for OHE
    ohe_file = open('./'+cat_features+'_ohe.pickle', 'wb')
    pickle.dump(lin_mod, ohe_file)
    ohe_file.close()

    return encoded.transform(enc_cat_features.reshape(-1,1)).toarray()

А затем, во время тестирования:

def encode_string(cat_features):
    # Load the previously saved encoder
    with open('./'+cat_features+'_encoder.pickle', 'rb') as file:
        enc = pickle.load(file)

    # No fitting, only transform
    enc_cat_features = enc.transform(cat_features)

    # Same for OHE
    with open('./'+cat_features+'_ohe.pickle', 'rb') as file:
        enc = pickle.load(file)

    return encoded.transform(enc_cat_features.reshape(-1,1)).toarray()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...