text_html_output_1.png
from sklearn.linear_model import RidgeClassifier

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

from sklearn.model_selection import GridSearchCV, train_test_split

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import LabelEncoder, PolynomialFeatures

from time import time

import numpy as np

import pandas as pd

import plotly.graph_objs as go

import plotly.offline as pyo

data_df = pd.read_csv('../input/mushrooms.csv')

data_df['y'] = data_df['class'].map({'p': 1, 'e': 0})
feature_columns = [c for c in data_df.columns if not c in ('class', 'y')]
stats_df = []
single_val_c = {}
for i, c in enumerate(feature_columns):
    if data_df[c].nunique() == 1:
        single_val_c[c] = data_df[c].unique()[0]
        continue
    gb = data_df.groupby(c)
    m = gb['y'].mean()
    s = gb.size()
    df = pd.DataFrame(index=range(len(m)))
    df['col'] = c
    df['val'] = m.index.values
    df['positive_percentage'] = m.values
    df['quantity_percentage'] = s.values / s.sum()
    stats_df.append(df)
    trace_prate = go.Bar(x=df['val'], y=df['positive_percentage'] * 100, name='positive percentage')
    trace_cnt = go.Bar(x=df['val'], y=df['quantity_percentage'] * 100, name='quantity percentage')
    layout = go.Layout(xaxis=dict(title=c), yaxis=dict(title='positive and quantity percentage'))
    fig = go.Figure(data=[trace_prate, trace_cnt], layout=layout)
stats_df = pd.concat(stats_df, axis=0)

for c in single_val_c.keys():
    feature_columns.remove(c)

data_all = pd.get_dummies(data=data_df, columns=x_columns, prefix=x_columns).drop(list(single_val_c.keys()) + ['class'], axis=1)

def ridge_model(X_train, y_train):
    r_c = Pipeline([('poly', PolynomialFeatures(interaction_only=True)), ('clf', RidgeClassifier())])
    params_pool = {'poly__degree': [2, 3], 'clf__alpha': np.logspace(-1, 3, num=6)}
    gs_c = GridSearchCV(r_c, param_grid=params_pool, n_jobs=-1, cv=4)
    gs_c.fit(X_train, y_train)
    return gs_c
def do_model_train(model_name, X_train, y_train, X_test, y_test):
    bg = time()
    if 'Ridge' == model_name:
        model = ridge_model(X_train, y_train)
    y_hat = model.predict(X_train)
    y_hat = model.predict(X_test)

X_all, y_all = (data_all[[c for c in data_all.columns if c != 'y']], data_all['y'])
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=0.33, random_state=1)
do_model_train('Ridge', X_train, y_train, X_test, y_test)