{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport json \nfrom pandas.io.json import json_normalize #нормализуем json\nfrom sklearn import preprocessing\nimport warnings\nwarnings.simplefilter('ignore', FutureWarning)\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def cut_for_speed(obj):\n    msk = np.random.rand(len(obj)) <0.1 #10% строк для ускорения\n    nobj = obj[msk]\n    return nobj","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def json_convert(df,json_columns): #преобразование json\n    for column in  json_columns:\n        column_as_df = json_normalize(df[column])\n        column_as_df.columns = [f'{column}.{subcolumn}' for subcolumn in column_as_df.columns]\n        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\nimport csv\ndata = pd.read_csv(\"../input/ga-customer-revenue-prediction/train.csv\", sep = \",\", dtype = {'fullVisitorId': 'str'},\n                   converters={column: json.loads for column in JSON_COLUMNS}, \n                nrows = None).sample(frac=0.3)\ndata = json_convert(data, JSON_COLUMNS) #конвертирование json\n#отчистка мусора\ndelete = data.ne ('not available in demo dataset')\ndata = data.loc[delete.any(1), delete.any()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prepare_data(df):\n\n    df = df.replace({'socialEngagementType': {'Engaged': True, 'Not Socially Engaged': False}})\n    float_cols = ['totals.pageviews','totals.bounces','totals.transactionRevenue']\n    for col in float_cols: \n        df[col].fillna(0, inplace=True)\n        df[col] = df[col].astype(float)\n    df['totals.transactionRevenue'] = np.log1p(df['totals.transactionRevenue'].values)\n    str_cols = ['device.browser','geoNetwork.subContinent','geoNetwork.region',\n            'geoNetwork.networkDomain','trafficSource.medium'] #считаем логорифм\n    for col in str_cols:\n        df = df.replace({col: {'not available in demo dataset': '(not set)'}})\n\n        le = preprocessing.LabelEncoder()\n        le.fit(df[col].unique())\n        df[col] = le.transform(list(df[col].values.astype('str')))\n        df[col] = df[col].astype(float)\n\n    for columns in df.columns:\n        if df[columns].dtype == 'object' or df[columns].dtype == 'int64':\n            df = df.drop([columns], axis = 1)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = prepare_data(data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.svm import SVR\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\n\ny = data['totals.transactionRevenue'] #отделяем от выборки значения зависимой переменной\nX = data.drop(columns=['totals.transactionRevenue'], axis=1) \nXtrn, Xtest, Ytrn, Ytest = train_test_split(X, y, test_size = 0.2) #разбиваем выборку на тестовую и обуч.\nmodels = [GradientBoostingRegressor(), #градиентный бустинг\nRandomForestRegressor(), #случайный лес\nSVR(kernel='linear'), #эпсилон-опорная векторная регрессия \nLinearRegression()] #линейная регрессия\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Result = pd.DataFrame() \ntmp = pd.DataFrame() \n\nfor model in models: \n    m = str(model) \n    model_name = m[:m.index('(')] \n    scores = np.array([]) #кросс валидация\n    fold = KFold(n_splits=15, shuffle=True) \n    scores_on_this_split = cross_val_score( \n        estimator = model, X=X, y=y, \n        cv=fold, scoring='r2') \n    scores = np.append(scores, scores_on_this_split) \n    print('Отработала', model_name) \n    tmp['Оценка качества'] = pd.Series(scores) \n    tmp['Модель'] = model_name \n    Result = Result.append([tmp]) #записывам данные","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#ящик с усами\nResult = Result.replace({'Модель': {'GradientBoostingRegressor': 'Бустинг', 'RandomForestRegressor': 'Лес', 'LinearRegression': 'Линейная'}}) \nsns.boxplot(y = 'Оценка качества', x = 'Модель', data = Result)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}