SVM模型预测不变

时间:2019-10-31 18:00:16

标签: python machine-learning scikit-learn svm

当我改变分类因素的水平时,SVM模型的预测不会改变。

假设因子A的水平为A1和A2,因子B的水平为B1和B2。 现在,当我将A的电平更改为A1时,会得到输出O1,但是当我将电平更改为A2时,我仍会得到相同的结果。 同样将B的电平更改为B1,得到输出P1,但是当我将电平更改为B2时,我继续获取P1。

可能是什么原因

我的模特基本上 19个参数由于一次编码而膨胀到193个。 大约有1600个数据点

   #Pre Procesing
        df = pd.read_csv("01.METADATA.csv")
    cat_prams = ["LGTCOND", "PRECREVbin", "FIRSTCRA", "TRAFFLDT", "PREVEH", "CRITPRE", "AVOIDMAN", "GAD1", "MANUSE", 
                 "SEATPOS", "PARTNERCLASSe"]
    cont_prams = ['OAL',"OAW", 'DIRDAMW', 'MAX', 'VC_1ST', 'DV_1ST', "LANEOPP"]
    HIS = ["HISPID"]
    all_param = HIS + cont_prams + cat_prams
    df = df[all_param]
    df["HISPID"] = df["HISPID"].astype("category")
    for cat_pram in cat_prams:
        df[cat_pram] = df[cat_pram].astype("category")

    df= df.dropna()

    # ONE HOT ENCODING
    cat_columns = cat_prams
    df_onehot = pd.get_dummies(df, prefix_sep = "__", columns = cat_prams)
    cat_dummies = [col for col in df_onehot if "__" in col and col.split("__")[0] in cat_columns]
    processed_columns = list(df_onehot.columns[:])

    df_onehot.to_csv("02_SVM_model_Input.csv")

    #Train Test split
    y = df_onehot["HISPID"]
    col = df_onehot.columns[1:]
    X = df_onehot[col]
    order = X.columns
    print(order)

    # Normalizing the factors
    from sklearn.preprocessing import StandardScaler
    scaler = StandardScaler()
    X = scaler.fit_transform(X)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=101)

    linearmodel = SVC(kernel= "linear", probability= True).fit(X_train, y_train)
    lin_predictions = linearmodel.predict(X_test)
    print(classification_report(y_test,lin_predictions))
    error_shift(lin_predictions, y_test)

#Creating an impulse in the actual data and predicting the outcome
print("\nShape of trainign: ", X_train.shape)
print("No of parameters used in training: ", X_train.shape[1], "\n")

dft = pd.read_csv("01.METADATA.csv")
dft = dft[all_param]

print(dft.iloc[:, 6].head())
dft.iloc[:, 6] = dft["DV_1ST"]*2
print(dft.iloc[:, 6].head())


for cat_pram in cat_prams:
    dft[cat_pram] = dft[cat_pram].astype("category")
dft= dft.dropna()
print("dft Shape (after dropping rows with null):" , dft.shape)

dft_onehot = pd.get_dummies(dft, prefix_sep = "__", columns = cat_prams)
print("dft_onehot Shape (after creating dummies):" , dft_onehot.shape)
cat_dummies = [col for col in dft_onehot if "__" in col and col.split("__")[0] in cat_columns]


# Remove additional columns
print("---------------------")
for col in dft_onehot.columns:
    if ("__" in col) and (col.split("__")[0] in cat_columns) and col not in processed_columns:
        print("Removing feature {}".format(col))
        dft_onehot.drop(col, axis=1, inplace=True)
print("---------------------")
print("dft_onehot Shape (after removing extra param):" , dft_onehot.shape)


    #Add missing columns:
    print("---------------------")
    for col in processed_columns:
        if col not in dft_onehot.columns:
            print("Adding feature {}".format(col))
            dft_onehot[col] = 0
    print("---------------------")
    print("dft_onehot Shape (after adding missing param):" , dft_onehot.shape)


    yt = dft_onehot["HISPID"]
    col = dft_onehot.columns[1:]
    Xt = dft_onehot[col]
    print("y Shape:" , yt.shape)
    Xt = dft_onehot[dft_onehot.columns[1:]]
    print("X Shape:" , Xt.shape)
    Xt = Xt[order]
    Xt.to_csv("Xt.csv")
    Xt.head()
    print(Xt.columns)


    Xt = scaler.fit_transform(Xt)
    new_predict = linearmodel.predict(Xt)
    impulse_result = pd.DataFrame({'Actual': yt, 'Altered': new_predict })

0 个答案:

没有答案