Last active
December 17, 2017 12:11
-
-
Save sidhusmart/6d83ec0c07aea91b8bd841cf89e548db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from pyspark import SparkConf\n", | |
| "from pyspark.sql import SparkSession\n", | |
| "from pyspark.ml.feature import StringIndexer\n", | |
| "from pyspark.ml.feature import OneHotEncoder\n", | |
| "from pyspark.ml import Pipeline\n", | |
| "from pyspark.sql.functions import when\n", | |
| "\n", | |
| "sparkConf = SparkConf(loadDefaults=True).setAppName(\"Encoding\")\n", | |
| "spark = SparkSession.builder.master(\"local\").config(conf=sparkConf).getOrCreate()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Load the data as a Spark Dataframe now and select the same columns for One Hot Encoding" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 23, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "+--------------------+--------------+--------------------+\n", | |
| "| GenderSelect| Country| EmploymentStatus|\n", | |
| "+--------------------+--------------+--------------------+\n", | |
| "|Non-binary, gende...| null| Employed full-time|\n", | |
| "| Female| United States|Not employed, but...|\n", | |
| "| Male| Canada|Not employed, but...|\n", | |
| "| Male| United States|Independent contr...|\n", | |
| "| Male| Taiwan| Employed full-time|\n", | |
| "| Male| Brazil| Employed full-time|\n", | |
| "| Male| United States| Employed full-time|\n", | |
| "| Female| India| Employed full-time|\n", | |
| "| Female| Australia| Employed full-time|\n", | |
| "| Male| Russia| Employed full-time|\n", | |
| "| Female| Russia|Not employed, and...|\n", | |
| "| Male| India| Employed full-time|\n", | |
| "| Male| Brazil| Employed full-time|\n", | |
| "| Male| Netherlands| Employed full-time|\n", | |
| "| Male| Taiwan| Employed full-time|\n", | |
| "| Male| United States|Independent contr...|\n", | |
| "| Male| Italy| Employed full-time|\n", | |
| "| Male|United Kingdom| Employed full-time|\n", | |
| "| Male| United States|Not employed, but...|\n", | |
| "| Male| Brazil|Not employed, but...|\n", | |
| "+--------------------+--------------+--------------------+\n", | |
| "only showing top 20 rows\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "sparkDF = spark.read.csv('multipleChoiceResponses.csv',header=True)\n", | |
| "sparkDF = sparkDF.limit(100)\n", | |
| "catColumns = ['GenderSelect','Country','EmploymentStatus']\n", | |
| "sparkDF.select(catColumns).show()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Need to reset empty cell values to __NA__ otherwise subsequent operation of StringIndexer fails" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "for col in catColumns:\n", | |
| " sparkDF = sparkDF.withColumn(col, when(sparkDF[col].isNull(),\"__NA__\").otherwise(sparkDF[col]))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Create a pipeline that first converts categorical values to numerical using StringIndexer and then applies OneHotEncoding" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 25, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "indexers = [StringIndexer(inputCol=col, outputCol=col+\"_encoded\").fit(sparkDF) for col in catColumns]\n", | |
| "encoders = [OneHotEncoder(inputCol=col+\"_encoded\", outputCol=col+\"_onehot\") for col in catColumns]\n", | |
| "p1 = Pipeline(stages=indexers)\n", | |
| "indexedDF = p1.fit(sparkDF).transform(sparkDF)\n", | |
| "p2 = Pipeline(stages=encoders)\n", | |
| "encodedDF = p2.fit(indexedDF).transform(indexedDF)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#### Show the dense vector representation of the one hot encoded vectors" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 27, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "+-------------------+---------------+-----------------------+\n", | |
| "|GenderSelect_onehot| Country_onehot|EmploymentStatus_onehot|\n", | |
| "+-------------------+---------------+-----------------------+\n", | |
| "| (2,[],[])|(31,[14],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[1],[1.0])| (31,[0],[1.0])| (5,[1],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[3],[1.0])| (5,[1],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[0],[1.0])| (5,[3],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[8],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[7],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[0],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[1],[1.0])| (31,[1],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[1],[1.0])|(31,[27],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[2],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[1],[1.0])| (31,[2],[1.0])| (5,[2],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[1],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[7],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])|(31,[17],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[8],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[0],[1.0])| (5,[3],[1.0])|\n", | |
| "| (2,[0],[1.0])|(31,[28],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[6],[1.0])| (5,[0],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[0],[1.0])| (5,[1],[1.0])|\n", | |
| "| (2,[0],[1.0])| (31,[7],[1.0])| (5,[1],[1.0])|\n", | |
| "+-------------------+---------------+-----------------------+\n", | |
| "only showing top 20 rows\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "encodedDF.select('GenderSelect_onehot','Country_onehot','EmploymentStatus_onehot').show()" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.13" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment