<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amine Benatmane on Medium]]></title>
        <description><![CDATA[Stories by Amine Benatmane on Medium]]></description>
        <link>https://medium.com/@am.benatmane?source=rss-75652b89fc38------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*H8n2QZRJ7jpNdRBLikdjHQ@2x.jpeg</url>
            <title>Stories by Amine Benatmane on Medium</title>
            <link>https://medium.com/@am.benatmane?source=rss-75652b89fc38------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 09:54:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@am.benatmane/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Keras Hyperparameter Tuning using Sklearn Pipelines & Grid Search with Cross Validation]]></title>
            <link>https://medium.com/@am.benatmane/keras-hyperparameter-tuning-using-sklearn-pipelines-grid-search-with-cross-validation-ccfc74b0ce9f?source=rss-75652b89fc38------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccfc74b0ce9f</guid>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[gridsearchcv]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <dc:creator><![CDATA[Amine Benatmane]]></dc:creator>
            <pubDate>Fri, 16 Aug 2019 13:54:36 GMT</pubDate>
            <atom:updated>2019-08-17T17:35:15.959Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_qY7cbt2fWyhjZdbJMi-8Q.png" /></figure><p>Training a Deep Neural Network that can <em>generalize</em> well to new data is a very challenging problem. Furthermore, Deep learning models are full of <em>hyper-parameters </em>and finding the optimal ones can be a tedious process !</p><p>Fortunately, Sklearn Grid Search is here to save us!</p><h3>Keras Wrappers for the Scikit-Learn API</h3><p>To perform Grid Search with <strong>Sequential Keras models</strong> (single-input only), you must turn these models into <strong>sklearn-compatible estimators</strong> by using <strong>Keras Wrappers</strong> for the <strong>Scikit-Learn API</strong>: [refer to <a href="http://keras.io/scikit-learn-api">docs</a> for more details]</p><p>A sklearn estimator is a class object with fit(X,y) , predict(x)and scoremethods. (and optionnaly predict_proba method)</p><p>No need to do that from scratch, you can use Sequential Keras models as part of your Scikit-Learn workflow by implementing one of two wrappers from keras.wrappers.scikit_learnpackage:</p><ul><li>KerasClassifier(build_fn=None, **sk_params): which implements the Scikit-Learn classifier interface.</li><li>KerasRegressor(build_fn=None, **sk_params): which implements the Scikit-Learn regressor interface.</li></ul><p><strong>Arguments</strong></p><ul><li><strong>build_fn</strong>: callable function or class instance the should construct, compile and return a Keras model, which will then be used to fit/predict.</li><li><strong>sk_params</strong>: model parameters &amp; fitting parameters.</li></ul><p>Note that like all other estimators in scikit-learn, build_fn should provide default values for its arguments, so that you could create the estimator without passing any values to sk_params.</p><h3>Example: Text classification with IMDB movie reviews dataset</h3><p>In this example, we show how to combine <strong>Sklearn Pipeline</strong>, <strong>GridSearch</strong> and these <strong>Keras Wrappers</strong> to fine-tune some of the <strong>hyperparameters</strong> of <strong>TfidfVectorizer</strong> and a basic<strong> Sequential keras model</strong> on the <strong>IMDB movie reviews dataset </strong>from <a href="http://kaggle.com/">Kaggle</a>.</p><p><strong>Link:</strong> <a href="https://www.kaggle.com/lakshmi25npathi/imdb-dataset-of-50k-movie-reviews">https://www.kaggle.com/lakshmi25npathi/imdb-dataset-of-50k-movie-reviews</a></p><p>You can create a <em>Kaggle Kernel </em>directly from this dataset<em> :)</em></p><p>Or fork my kernel on Kaggle<em>:</em><strong><em> </em></strong><a href="https://www.kaggle.com/med92amine/keras-hyperparameter-tuning"><strong>https://www.kaggle.com/med92amine/keras-hyperparameter-tuning</strong></a></p><h4>Import some usefull libraries</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b90b697705ff449dd9ba82ca04bcfb97/href">https://medium.com/media/b90b697705ff449dd9ba82ca04bcfb97/href</a></iframe><h4>Loading Data using pandas</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d24cbce50cb23989c78988fbcb096439/href">https://medium.com/media/d24cbce50cb23989c78988fbcb096439/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*4jOyz0Tbq2UP8_4JfBLJKg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zd-PRU__phuOKM1ZdHQdAQ.png" /><figcaption>Dataset Shape and class distribution</figcaption></figure><p>Perfect! This Dataset is <strong>“class Balanced”</strong> !</p><h4>Do Some Text Preprocessing</h4><p>Remove punctuations, numbers, whitespaces, urls, … You know, cleaning thing !</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8eb66f2c9afb978e41f5c2eb49325963/href">https://medium.com/media/8eb66f2c9afb978e41f5c2eb49325963/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48a34a2b40e892fcbcd40cba578ba7b4/href">https://medium.com/media/48a34a2b40e892fcbcd40cba578ba7b4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b48cd5f36728e0995811e6af6832925/href">https://medium.com/media/3b48cd5f36728e0995811e6af6832925/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25f82fd0debaa15563060bbe6ac1403a/href">https://medium.com/media/25f82fd0debaa15563060bbe6ac1403a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b70e667c5d6353e4bb2d73696a095479/href">https://medium.com/media/b70e667c5d6353e4bb2d73696a095479/href</a></iframe><h4>Implement Keras Model creator function</h4><p>We want to fine-tune these hyperparameters: <strong>optimizer</strong>, <strong>dropout_rate</strong>, <strong>kernel_init method </strong>and <strong>dense_layer_size.</strong></p><p>These parameters must be defined in the <strong><em>signature</em></strong> of <strong>create_model()</strong> function with <strong>default parameters</strong>. You can add other hyperparameters if you want such as <strong>learning_rate</strong>, ...</p><p><strong>binary_crossentropy</strong> is perfect for <strong><em>Two</em>-<em>class classification problem.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b516b247856a16cea4829feca4029e15/href">https://medium.com/media/b516b247856a16cea4829feca4029e15/href</a></iframe><h4>Create sklearn-like estimator</h4><p>It’s a <strong>classification problem</strong> so we are using <strong>KerasClassifier wrapper</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b39e21da9de4db8db9d32e4b65b7239b/href">https://medium.com/media/b39e21da9de4db8db9d32e4b65b7239b/href</a></iframe><h4>Tuning some TF-IDF Hyperparameters</h4><p>We need to convert the text into <strong><em>numerical feature vectors </em></strong>to perform text classification. We will be using <strong>Sklearn TfidfVectorizer</strong> in this example.</p><p>We could use <strong><em>Keras Text Preprocessing Tokenizer </em></strong>for that, but we want to do some <strong>TF-IDF Hyperparameters fine-tuning.</strong> So, we create a <strong>Sklearn Pipeline</strong> for that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa84640058e281ff4f23e86246d2e121/href">https://medium.com/media/aa84640058e281ff4f23e86246d2e121/href</a></iframe><h4>Defining Hyperparamers Space</h4><p>We define here our <strong>hyperparameters space</strong> including keras fit hyperparameters: <strong>epochs</strong> and <strong>batch_size</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f769c9da487334d1ec1f2633e75263f2/href">https://medium.com/media/f769c9da487334d1ec1f2633e75263f2/href</a></iframe><h4>And Finally Performing Grid Search with KFold Cross Validation</h4><p>It’s same as grid search with sklearn; it’s no big deal!</p><p>Remember, For <strong>K-fold cross validation</strong>, <strong>K</strong> is not a <strong>hyperparameter</strong>. The purpose of cross-validation is not to come up with a final <strong>“performant model”</strong> but to see how well our model is able to <strong>generalize</strong> well to unseen data against a relevant performance metric.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dd4fb1fb22c5115cae9d265b557f743/href">https://medium.com/media/4dd4fb1fb22c5115cae9d265b557f743/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a9d13c824029b05a1514c7e45689d0c/href">https://medium.com/media/5a9d13c824029b05a1514c7e45689d0c/href</a></iframe><p><strong>And Voila !</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k3mBy-GLTuU1E-sokqApBQ.png" /><figcaption>Perfroming Grid Search</figcaption></figure><h3>Bonus: Tune the number of dense layers and their sizes</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea515d6750106b0083c54debb0dd54f0/href">https://medium.com/media/ea515d6750106b0083c54debb0dd54f0/href</a></iframe><p>You can find an excellent example with mnist dataset on keras github repo: <a href="https://github.com/keras-team/keras/blob/master/examples/mnist_sklearn_wrapper.py">https://github.com/keras-team/keras/blob/master/examples/mnist_sklearn_wrapper.py</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccfc74b0ce9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a Spark Environment with Jupyter Notebook and Apache Zeppelin on Ubuntu]]></title>
            <link>https://medium.com/@am.benatmane/setting-up-a-spark-environment-with-jupyter-notebook-and-apache-zeppelin-on-ubuntu-e12116d6539e?source=rss-75652b89fc38------2</link>
            <guid isPermaLink="false">https://medium.com/p/e12116d6539e</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[apache-spark]]></category>
            <category><![CDATA[apache-zeppelin]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <dc:creator><![CDATA[Amine Benatmane]]></dc:creator>
            <pubDate>Sun, 11 Nov 2018 14:37:23 GMT</pubDate>
            <atom:updated>2019-03-22T19:57:42.487Z</atom:updated>
            <content:encoded><![CDATA[<p>The Ultimate Guide to setup a Spark Interactive Environment of choice for Data Analysis and more!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y7eRzyGZVUkK2x58qgkDYA.png" /></figure><p><a href="https://spark.apache.org/"><strong>Apache Spark™</strong></a> is a unified analytics engine for large-scale data processing. It is a general-purpose framework for <strong>cluster computing</strong>, so it is used for a diverse range of applications such as <strong>Data Science</strong> and <strong>Analytics</strong>.</p><p>You can write <strong>Spark Applications</strong> in several programming languages such as <strong>Scala</strong>, <strong>Python </strong>and <strong>Java. </strong>Refer to <a href="https://spark.apache.org/docs/2.1.1/programming-guide.html">https://spark.apache.org/docs/2.1.1/programming-guide.html</a> for more details.</p><p><strong>Exploratory Data Analysis</strong> <strong><em>EDA </em></strong>with Spark, can be performed directly in <strong>Spark-Shell</strong>, <strong>PySpark</strong> Shell or any <strong>IDE</strong> of choice, but <strong>Interactive Tools</strong> are ideal for that task because they let you collaborate on many projects and showcase the results to peers and open-source community.</p><p><strong>Interactive Notebooks</strong> provide a great interface for <strong>iteractively running code</strong>, <strong>exploring</strong> and <strong>visualizing</strong> data. They allow you to mix <strong>narrative</strong>, <strong>rich media</strong> and <strong>data</strong> in a single <strong>Story</strong> !</p><h4>Jupyter | Zeppelin Notebooks</h4><p><a href="http://jupyter.org/"><strong>Jupyter Notebook</strong></a> and <a href="https://zeppelin.apache.org/"><strong>Apache Zeppelin</strong></a> are <strong>Web-based interactive notebooks </strong>that enable <strong>data-driven</strong>, <strong>interactive</strong> <strong>data analytics</strong> and <strong>collaborative</strong> documents with <strong>Python</strong>, <strong>SQL</strong>, <strong>Scala</strong> and more. They allow you to modify and re-execute parts of your code in a very flexible way. <strong>That’s why they are great tools for EDA and suitable to test and prototype data products.</strong></p><p><strong>Let’s start by installing our Toolbox :)</strong></p><h3>1. Installing the Toolbox</h3><h4><strong>1.1. Installing Linuxbrew</strong></h4><p><a href="http://linuxbrew.sh/">Linuxbrew</a> is a fork of <strong>Homebrew</strong> (<em>macOS package manager</em>) for <strong>Linux</strong> ! You can use it to install up-to-date versions of open softwares (such as Apache-Spark, Hadoop, HDFS, …) not packaged by the native distribution (eventually old distributions) to a home directory (it does not require <strong>root access</strong>) !</p><p><strong>Dependencies:</strong></p><ul><li><strong>GCC</strong> 4.4 or newer</li><li><strong>Linux</strong> 2.6.16 or newer</li><li><strong>64-bit x86</strong> or <strong>32-bit ARM</strong> (Raspberry Pi)</li></ul><p>On a <strong>Debian</strong> or <strong>Ubuntu</strong>, paste to a Terminal prompt:</p><pre>sudo apt update &amp; sudo apt install build-essential curl file git -y</pre><p>git cloneLinuxbrew wherever you want; to a home directory $HOME/.linuxbrew (or/home/linuxbrew/.linuxbrew if possible):</p><pre>git clone https://github.com/Linuxbrew/brew.git ~/.linuxbrew</pre><p>Add linuxbrew to your PATH and to your bash shell profile script ~/.profile (or directly to ~/.bachrc, ~/.zshrc ):</p><pre>echo &quot;export PATH=&#39;$(brew --prefix)/bin:$(brew --prefix)/sbin&#39;&quot;:&#39;&quot;$PATH&quot;&#39; &gt;&gt;~/.profile</pre><p>source ~/.profile so changes take effect …</p><p>Et Voila! You’re done!</p><p>Test the installation by installing a package:</p><pre>brew install hello</pre><h4>1.2. Installing Apache Spark:</h4><p>Apache Spark is one of the most popular framework for big data analysis. Though Spark has API’s for <strong>Scala</strong>, <strong>Python</strong>, <strong>Java</strong> and <strong>R</strong> but the popularly used languages are the former two.</p><h4>1.2.1. Installing Java and Scala</h4><p>Scala runs primarily on the <strong>Java Virtual Machine <em>JVM</em></strong>. Sometimes new JVM versions require us to update Scala to remain compatible. For more details refer to: <a href="https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html#jdk-9--up-compatibility-notes">https://docs.scala-lang.org/overviews/jdk-compatibility/overview.html#jdk-9--up-compatibility-notes</a></p><p>You can install JDK and Scala using brew:</p><pre>brew install jdk scala</pre><p>or using apt:</p><pre>sudo apt install scala default-jdk</pre><p>Check Scala version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*AXM2LuVGb0Fp8UfVh3EO1Q.png" /><figcaption>Checking where default Scala is installed and it’s version</figcaption></figure><p>Check Java version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*IKr0T9LXUFNx_eASv6xYUA.png" /><figcaption>Checking where default Java is installed and it’s version</figcaption></figure><p>You can downgrade Java to version 8, using brew as follow:</p><pre>brew uninstall jdk <br>brew install java@8</pre><p>You may also add java, in case you downgraded it, to your PATH and to your bash shell profile script ~/.profile (or directly to ~/.bachrc, ~/.zshrc). Source it so changes take effect !</p><h4>1.2.2. Installing Python: Anaconda3</h4><p><strong>Anaconda</strong> is a free and open source distribution of Python. It comes with plenty packages for Data Science, Data Analysis and Machine Learning applications.</p><p>Download the latest version of anaconda3 (python3) from <a href="https://www.anaconda.com/download/#linux">https://www.anaconda.com/download/#linux</a> (choose 64-bit (x86) Installer):</p><pre>wget <a href="https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh">https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh</a></pre><p>Then run the Anaconda Script using bash command:</p><pre><a href="https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh">bash Anaconda3-5.3.0-Linux-x86_64.sh</a></pre><p>You may Add Anaconda to your PATH and to your bash shell profile script ~/.profile (or directly to ~/.bachrc, ~/.zshrc). You will be prompted to do it while installing anaconda, in my case i installed it to $HOME/.opt/anaconda3 :</p><pre>export PATH=&quot;$HOME/.opt/anaconda3/bin:$PATH&quot;</pre><p>source ~/.profile so changes take effect …</p><p>Check the installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*PEQiAvGvu2WAlugEeNwVLw.png" /><figcaption>Checking default Python</figcaption></figure><p>Testing Python Shell:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*ZE-i3iuqkq3okYU5vV-NUw.png" /><figcaption>Python Shell</figcaption></figure><h4>1.2.3. Installing Apache Spark</h4><p>You can easily install <strong>Apache Spark</strong> using brewlinux as follow:</p><pre>brew install apache-spark</pre><p><strong>Check the installation</strong></p><p>To check the installation let’s launch the Spark Shell as follow:</p><pre>spark-shell</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*0rNP6yrMVpfk8UUhvOux7A.png" /><figcaption>Spark-shell</figcaption></figure><p>Testing with an example (You can paste a code snippet directly via :paste command):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*J-UFIK3PYVtwNx2ePTIk2Q.png" /><figcaption>Using Spark-Shell</figcaption></figure><p>And PySpark:</p><pre>pyspark</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*EVKRXI7X_2Fw5pWRcB349A.png" /><figcaption>PySpark Shell</figcaption></figure><p>Testing with spark-submit(calculating Pi using Scala example):</p><pre>spark-submit --class org.apache.spark.examples.SparkPi --driver-memory 512m --executor-memory 512m --executor-cores 1 /home/linuxbrew/.linuxbrew/Cellar/apache-spark/2.3.2/libexec/examples/jars/spark-examples*.jar 10</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*2M0sUm4e1uRIg6Oo5mWWkg.png" /><figcaption>Using Spark-Submit with scala jar</figcaption></figure><p>Testing with spark-submit (calculating Pi using Python example):</p><pre>spark-submit --class org.apache.spark.examples.SparkPi --driver-memory 512m --executor-memory 512m --executor-cores 1 /home/linuxbrew/.linuxbrew/Cellar/apache-spark/2.3.2/libexec/examples/src/main/python/pi.py 10</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*RZA3MWV69ssDnCfpsQu1kg.png" /><figcaption>Using Spark-Submit with python source</figcaption></figure><h4>1.3. Installing Apache Zeppelin:</h4><p>You can easily install <strong>Apache Zeppelin</strong> using brewlinux as follow:</p><pre>brew install apache-zeppelin</pre><p>Running <strong>Zeppelin</strong> Instance:</p><pre>zeppelin.sh</pre><p>Go to localhost:8080 using your favorite Web navigator:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ss6JMQfokpLYd7XBpNdwkQ.png" /><figcaption>Running Zeppelin Server</figcaption></figure><p><strong>Done!</strong> <br>Let’s move to serious things …</p><h3>2. Spark with Jupyter Notebook</h3><h4>2.1. Run Pyspark in Jupyter Notebook:</h4><p>There are two ways to run PySpark in a Jupyter Notebook:</p><ul><li>Configure PySpark driver to use Jupyter Notebook.</li><li>Load a regular Jupyter Notebook and load PySpark using findSpark package</li></ul><p>You may install jupyter notebook package if you are using system’s python:</p><pre>pip install jupyter</pre><p><strong>First Method: Configure PySpark Driver</strong></p><p>Add these lines (PySpark driver environment variables) to your bash profile so pyspark can run in jupyter notebook:</p><pre>export PYSPARK_DRIVER_PYTHON=jupyter<br>export PYSPARK_DRIVER_PYTHON_OPTS=&#39;notebook&#39;<br>export PYSPARK_PYTHON=python3</pre><p>Source it so changes take effect, then run pyspark ….</p><pre>pyspark</pre><p>Et Voila!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1it1fGzw9u5pO8ThGr7AHg.png" /><figcaption>Running PySpark in Jupyter Notebook</figcaption></figure><p>Copy/Paste the provided URL into your favorite web browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0oPtyPCknIb_33MaGPMlfw.png" /><figcaption>Jupyter UI</figcaption></figure><p><strong>Second Method: Using FindSpark package</strong></p><p>Install FindSpark package as follow:</p><pre>pip install findspark</pre><p>Add SPARK_HOME env variable to your bash profile so findspark can recognize Spark:</p><pre>export SPARK_HOME=&quot;/home/linuxbrew/.linuxbrew/Cellar/apache-spark/2.3.2/libexec&quot;</pre><p>Source it so changes take effect … Then run a regular Jupyter Notebook:</p><pre>jupyter-notebook</pre><p>To initialize PySpark, just call:</p><pre>import findspark<br>findspark.init()</pre><pre>import pyspark<br>sc = pyspark.SparkContext(appName=&quot;myAppName&quot;)</pre><p>You can pass directly Spark Home to init() method by the way.</p><h4>2.2. Run Spark (with Scala) in Jupyter Notebook using a Scala Kernel:</h4><p>You can find many kernels for that. <a href="https://github.com/Valassis-Digital-Media/spylon-kernel">spylon-kernel</a> is a Scala <a href="http://jupyter.readthedocs.io/en/latest/projects/kernels.html">Jupyter kernel</a> that uses <a href="https://github.com/Calysto/metakernel">metakernel</a> in combination with <a href="https://www.py4j.org/">py4j</a>.</p><p><strong>Prerequisites</strong></p><ul><li>Apache Spark 2.1.1 compiled for Scala 2.11</li><li>Jupyter Notebook</li><li>Python 3.5+</li></ul><p>You can install the spylon-kernel package using pip or conda.</p><pre>pip install spylon-kernel<br># or<br>conda install -c conda-forge spylon-kernel</pre><p>You can use <strong>spylon-kernel</strong> as Scala kernel for Jupyter Notebook. Do this when you want to work with Spark in Scala with a bit of Python code mixed in.</p><p>Create a kernel spec for Jupyter notebook by running the following command:</p><pre>python -m spylon_kernel install --user</pre><p>Launch a regular jupyter notebook and you should see a spylon-kernel as an option in the <em>New</em> dropdown menu:</p><pre>jupyter-notebook</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gSG3nAEiOuA6pyUEIvrA2A.png" /><figcaption>Jupyter UI</figcaption></figure><h3>3. Spark with Apache Zeppelin</h3><p>You can run Zeppelin Notebook as follow:</p><pre>zeppelin.sh</pre><p>Add new <strong>Spark Note</strong> to start working with Spark in Scala with a bit of Python code in same Notebook! <strong>“La Classe !”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCSlz-mjeGMLdLMFrUmV4w.png" /><figcaption>Creating New Spark Note using Zeppelin</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K5KzHdIXryZFrSftehKfew.png" /><figcaption>Spark Note in Zeppelin</figcaption></figure><p><strong>Done!</strong> <br><strong>You are now able to run Spark (with Scala), PySpark in a Jupyter or Zeppelin Notebooks like a Boss :)</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e12116d6539e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>