{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6\n", "\n", "## SVM & Regularization\n", "\n", "\n", "For this homework we consider a set of observations on a number of red and white wine varieties involving their chemical properties and ranking by tasters. Wine industry shows a recent growth spurt as social drinking is on the rise. The price of wine depends on a rather abstract concept of wine appreciation by wine tasters, opinion among whom may have a high degree of variability. Pricing of wine depends on such a volatile factor to some extent. Another key factor in wine certification and quality assessment is physicochemical tests which are laboratory-based and takes into account factors like acidity, pH level, presence of sugar and other chemical properties. For the wine market, it would be of interest if human quality of tasting can be related to the chemical properties of wine so that certification and quality assessment and assurance process is more controlled.\n", "\n", "Two datasets are available of which one dataset is on red wine and have 1599 different varieties and the other is on white wine and have 4898 varieties. All wines are produced in a particular area of Portugal. Data are collected on 12 different properties of the wines one of which is Quality, based on sensory data, and the rest are on chemical properties of the wines including density, acidity, alcohol content etc. All chemical properties of wines are continuous variables. Quality is an ordinal variable with possible ranking from 1 (worst) to 10 (best). Each variety of wine is tasted by three independent tasters and the final rank assigned is the median rank given by the tasters.\n", "\n", "A predictive model developed on this data is expected to provide guidance to vineyards regarding quality and price expected on their produce without heavy reliance on volatility of wine tasters." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "data_r = pd.read_csv('https://github.com/albahnsen/PracticalMachineLearningClass/raw/master/datasets/Wine_data_red.csv')\n", "data_w = pd.read_csv('https://github.com/albahnsen/PracticalMachineLearningClass/raw/master/datasets/Wine_data_white.csv')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
fixed acidityvolatile aciditycitric acidresidual sugarchloridesfree sulfur dioxidetotal sulfur dioxidedensitypHsulphatesalcoholqualitytype
35946.80.190.327.60.04937.0107.00.993323.120.4410.77white
24126.30.380.178.80.08050.0212.00.998033.470.669.44white
537810.60.280.3915.50.0696.023.01.002603.120.669.25red
29057.60.310.261.70.07340.0157.00.993803.100.469.85white
64326.60.560.142.40.06413.029.00.993973.420.6211.77red
\n", "
" ], "text/plain": [ " fixed acidity volatile acidity citric acid residual sugar chlorides \\\n", "3594 6.8 0.19 0.32 7.6 0.049 \n", "2412 6.3 0.38 0.17 8.8 0.080 \n", "5378 10.6 0.28 0.39 15.5 0.069 \n", "2905 7.6 0.31 0.26 1.7 0.073 \n", "6432 6.6 0.56 0.14 2.4 0.064 \n", "\n", " free sulfur dioxide total sulfur dioxide density pH sulphates \\\n", "3594 37.0 107.0 0.99332 3.12 0.44 \n", "2412 50.0 212.0 0.99803 3.47 0.66 \n", "5378 6.0 23.0 1.00260 3.12 0.66 \n", "2905 40.0 157.0 0.99380 3.10 0.46 \n", "6432 13.0 29.0 0.99397 3.42 0.62 \n", "\n", " alcohol quality type \n", "3594 10.7 7 white \n", "2412 9.4 4 white \n", "5378 9.2 5 red \n", "2905 9.8 5 white \n", "6432 11.7 7 red " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = data_w.assign(type = 'white')\n", "\n", "data = data.append(data_r.assign(type = 'red'), ignore_index=True)\n", "data.sample(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.1\n", "\n", "Show the frecuency table of the quality by type of wine" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# SVM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.2\n", "\n", "* Standarized the features (not the quality)\n", "* Create a binary target for each type of wine\n", "* Create two Linear SVM's for the white and red wines, repectively.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.3\n", "\n", "Test the two SVM's using the different kernels (‘poly’, ‘rbf’, ‘sigmoid’)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.4\n", "Using the best SVM find the parameters that gives the best performance\n", "\n", "'C': [0.1, 1, 10, 100, 1000], 'gamma': [0.01, 0.001, 0.0001]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.5\n", "\n", "Compare the results with other methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Regularization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.6\n", "\n", "\n", "* Train a linear regression to predict wine quality (Continous)\n", "\n", "* Analyze the coefficients\n", "\n", "* Evaluate the RMSE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.7\n", "\n", "* Estimate a ridge regression with alpha equals 0.1 and 1.\n", "* Compare the coefficients with the linear regression\n", "* Evaluate the RMSE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.8\n", "\n", "* Estimate a lasso regression with alpha equals 0.01, 0.1 and 1.\n", "* Compare the coefficients with the linear regression\n", "* Evaluate the RMSE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.9\n", "\n", "* Create a binary target\n", "\n", "* Train a logistic regression to predict wine quality (binary)\n", "\n", "* Analyze the coefficients\n", "\n", "* Evaluate the f1score" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6.10\n", "\n", "* Estimate a regularized logistic regression using:\n", "* C = 0.01, 0.1 & 1.0\n", "* penalty = ['l1, 'l2']\n", "* Compare the coefficients and the f1score" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }