- 3rd Jul 2024
- 17:44 pm
In this assignment, we will guide you through importing, splitting, and standardizing your data to prepare for neural network construction. You'll learn how to plot training data, build and train a neural network, and visualize model predictions. Finally, we'll cover how to compute and assess the generalization error to evaluate your model's performance.
- Import, Split, and Standardize Data - Were you able to successfully import, split, and normalize the data into a training and test set? Did you normalize your data after the split instead of before?
- Plotting Data - Were you able to properly plot the training data?
- Neural Network Construction - Were you able to successfully build and train a neural network on the training data? Does your model seem to fit the data well based on your plotted model predictions?
- Plot Model Predictions for Training Set - Were you able to successfully visualize the model’s predictions for the training data? Does your prediction curve seem to fit your data well?
- Compute Generalization Error - Were you able to successfully compute the generalization error for the testing data? You should be able to tell if your generalization error might be too high based on the fit of your prediction curve.
Applied Machine Learning - DTSC680 - Get Assignment Solution
Please note that this is a sample Applied Machine Learning - DTSC680 assignment solved by our Python Programmers. These solutions are intended to be used for research and reference purposes only.
- Option 1 - To download the complete solution along with Code, Report and screenshots - Please visit our Python Assignment Sample Solution page
- Option 2 - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- Option 3 - You can check the partial solution for this assignment in this blog below
Free Assignment Solution - Applied Machine Learning - DTSC680
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 7: Exploring 3D Sinusoidal Data using Artificial Neural Networks\n",
"## DTSC 680: Applied Machine Learning\n",
"\n",
"## Name: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Directions and Overview\n",
"\n",
"Here just just a few of the hyperparameters you can play around with:\n",
"\n",
"- number of nodes per layer\n",
"- number of layers\n",
"- activation functions\n",
"- normalization method (should be negligible)\n",
"- number of epochs\n",
"- learning rate\n",
"- loss function\n",
"Lastly in this assignment, you will compute the generalization error on the test set."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preliminaries\n",
"\n",
"Let's import some common packages:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Common imports\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl\n",
"from matplotlib import cm\n",
"from mpl_toolkits import mplot3d\n",
"import numpy as np\n",
"import pandas as pd\n",
"%matplotlib inline\n",
"mpl.rc('axes', labelsize=14)\n",
"mpl.rc('xtick', labelsize=12)\n",
"mpl.rc('ytick', labelsize=12)\n",
"import os\n",
"\n",
"# Where to save the figures\n",
"PROJECT_ROOT_DIR = \".\"\n",
"FOLDER = \"figures\"\n",
"IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, FOLDER)\n",
"os.makedirs(IMAGES_PATH, exist_ok=True)\n",
"\n",
"def save_fig(fig_id, tight_layout=True, fig_extension=\"png\", resolution=300):\n",
" path = os.path.join(IMAGES_PATH, fig_id + \".\" + fig_extension)\n",
" print(\"Saving figure\", fig_id)\n",
" if tight_layout:\n",
" plt.tight_layout()\n",
" plt.savefig(path, format=fig_extension, dpi=resolution)\n",
" \n",
"def plot3Ddata(X,Y,Z):\n",
" A =[-140,0,90,30]\n",
" for i in A:\n",
" fig = plt.figure(figsize = (10, 7))\n",
" ax = plt.axes(projection =\"3d\")\n",
" ax.scatter3D(X, Y, Z, color = \"green\")\n",
" plt.title(\"simple 3D scatter plot\")\n",
" # show plot\n",
" ax.view_init(i, 60)\n",
" \n",
"\n",
" \n",
"def plotscatter3Ddata(fit_x, fit_y, fit_z, scat_x, scat_y, scat_z):\n",
" A =[-140,0,90,30]\n",
" for i in A:\n",
" fig = plt.figure(figsize = (10, 7))\n",
" ax = plt.axes(projection =\"3d\")\n",
" ax.scatter3D(fit_x, fit_y, fit_z, color = \"green\")\n",
" ax.scatter3D(scat_x, scat_y, scat_z, color = \"blue\")\n",
" plt.title(\"simple 3D scatter plot\")\n",
" # show plot\n",
" ax.view_init(i, 60)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing the dataset"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data = pd.read_csv('1649983201_3DSinusoidalANN.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(560, 3)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" x y z\n",
"0 5.003425 -0.097041 0.136004\n",
"1 4.914072 -0.049873 -1.726903\n",
"2 5.236610 0.257471 -1.838183\n",
"3 5.217523 0.212911 -0.669068\n",
"4 5.114359 0.808719 0.302012"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data Preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(448, 2)\n",
"(448, 1)\n",
"(112, 2)\n",
"(112, 1)\n"
]
}
],
"source": [
"### Separate Target Variable and Predictor Variables\n",
"X = data[['x','z']].values\n",
"Y = data[\"y\"].values\n",
"\n",
"Y= Y.reshape(-1,1)\n",
"\n",
"\n",
"### Sandardization of data \n",
"from sklearn.preprocessing import StandardScaler\n",
"scaler = StandardScaler()\n",
"X = scaler.fit_transform(X)\n",
"Y = scaler.fit_transform(Y)\n",
"\n",
"\n",
"\n",
"### Splitting the dataset into the Training set and Test set\n",
"from sklearn.model_selection import train_test_split\n",
"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)\n",
"\n",
"\n",
"\n",
"### Quick sanity check with the shapes of Training and testing datasets\n",
"print(X_train.shape)\n",
"print(Y_train.shape)\n",
"print(X_test.shape)\n",
"print(Y_test.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plot Data\n",
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"X_train1= pd.DataFrame(X_train)\n",
"X_test1= pd.DataFrame(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot Model Predictions for Training Set\n",
"\n",
"Use the model's `predict()` method to make a prediction for `y` using the `x` and `z` training data. Use the `plotscatter3Ddata(fit_x, fit_y, fit_z, scat_x, scat_y, scat_z)` function to plot the data and the prediction curve."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"y_pred1 = model.predict(X_train)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"
"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize =(15,5))\n",
"plt.plot(Y_train, color = 'red', label = 'Real data')\n",
"plt.plot(y_pred1, color = 'blue', label = 'Predicted data')\n",
"plt.title('Prediction on Train Data')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compute Generalization Error\n",
"\n",
"Compute the generalization error and use MSE as the generalization error metric. Round your answers to four significant digits. Print the generalization error for the model."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"y_pred2 = model.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.figure(figsize =(15,5))\n",
"plt.plot(Y_test, color = 'red', label = 'Real data')\n",
"plt.plot(y_pred2, color = 'blue', label = 'Predicted data')\n",
"plt.title('Prediction on Test Data')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.04813132280815494"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calculation of Mean Squared Error (MSE)\n",
"from sklearn.metrics import mean_squared_error\n",
"mean_squared_error(Y_test,y_pred2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Get the best Applied Machine Learning - DTSC680 assignment help and tutoring services from our experts now!
About The Author - Jane Doe
Jane Doe is an experienced data scientist with a strong background in machine learning and neural network development. With a master's degree in computer science, Jane has worked on numerous projects involving data preprocessing, model building, and performance evaluation. She is passionate about teaching and helping students understand the complexities of neural networks and data science through hands-on assignments and practical examples.