{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "r-magic-command.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "EnyONbNhCqSK", "colab_type": "text" }, "source": [ "# **Using R and Python in the Same Notebook**\n", "\n", "Chanin Nantasenamat\n", "\n", "[*'Data Professor' YouTube channel*](http://youtube.com/dataprofessor)\n", "\n", "In this Jupyter notebook, I will show you how to use R and Python in the same notebook.\n", "\n", "---" ] }, { "cell_type": "code", "metadata": { "id": "2h-2I4CviFCR", "colab_type": "code", "colab": {} }, "source": [ "# activate R magic\n", "%load_ext rpy2.ipython" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "FftFvPLNiZME", "colab_type": "text" }, "source": [ "## Python" ] }, { "cell_type": "code", "metadata": { "id": "3hPnRI2piJM3", "colab_type": "code", "colab": {} }, "source": [ "import pandas as pd" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "yNKM70-ZiPcg", "colab_type": "code", "colab": {} }, "source": [ "x <- 42\n", "print(x)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "dtkChhxpiWEd", "colab_type": "text" }, "source": [ "## R" ] }, { "cell_type": "code", "metadata": { "id": "ozqbZ3lviTPj", "colab_type": "code", "colab": {} }, "source": [ "%%R\n", "x <- 42\n", "print(x)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "napTAYyXiU8r", "colab_type": "code", "colab": {} }, "source": [ "%%R\n", "install.packages('caret')\n", "install.packages('mlbench')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "4eB_IbK4kztb", "colab_type": "code", "colab": {} }, "source": [ "%%R\n", "install.packages('mlbench')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Bl0feNEUi-Jk", "colab_type": "code", "colab": {} }, "source": [ "%%R\n", "library(caret)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "zY7WFnrSj4Mr", "colab_type": "code", "colab": {} }, "source": [ "%%R\n", "############################################\n", "# Data Professor #\n", "# http://youtube.com/dataprofessor #\n", "# http://github.com/dataprofessor #\n", "# http://facebook.com/dataprofessor #\n", "# https://www.instagram.com/data.professor #\n", "############################################\n", "\n", "# Importing libraries\n", "library(mlbench) # Contains several benchmark data sets (especially the Boston Housing dataset)\n", "library(caret) # Package for machine learning algorithms / CARET stands for Classification And REgression Training\n", "\n", "# Importing the Boston Housing data set\n", "data(BostonHousing)\n", "\n", "head(BostonHousing)\n", "\n", "# Check to see if there are missing data?\n", "sum(is.na(BostonHousing))\n", "\n", "# To achieve reproducible model; set the random seed number\n", "set.seed(100)\n", "\n", "# Performs stratified random split of the data set\n", "TrainingIndex <- createDataPartition(BostonHousing$medv, p=0.8, list = FALSE)\n", "TrainingSet <- BostonHousing[TrainingIndex,] # Training Set\n", "TestingSet <- BostonHousing[-TrainingIndex,] # Test Set\n", "\n", "\n", "###############################\n", "\n", "# Build Training model\n", "Model <- train(medv ~ ., data = TrainingSet,\n", " method = \"lm\",\n", " na.action = na.omit,\n", " preProcess=c(\"scale\",\"center\"),\n", " trControl= trainControl(method=\"none\")\n", ")\n", "\n", "# Apply model for prediction\n", "Model.training <-predict(Model, TrainingSet) # Apply model to make prediction on Training set\n", "Model.testing <-predict(Model, TestingSet) # Apply model to make prediction on Testing set\n", "\n", "# Model performance (Displays scatter plot and performance metrics)\n", " # Scatter plot of Training set\n", "plot(TrainingSet$medv,Model.training, col = \"blue\" )\n", "plot(TestingSet$medv,Model.testing, col = \"blue\" )" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Q6A7bOvbll8D", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }