{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Assignment 2: Python for Analytics, Winter”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“* covers lectures 4-6\n”,
“* due: Feb 25th by 9am.\n”,
“* Points will be deducted if:\n”,
” * Problems are not completed.\n”,
” * Portions of problems are not completed.\n”,
” * Third party modules where used when the question specified not to do so.\n”,
” * The problem was solved in a very inefficient manner. For instance, copying and pasting the same block of code 10 times instead of using a for loop or using a for loop when a comprehension would work.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 1 (15 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Using the Iris data, sum the 4 numeric features and find out how many rows have a sum greater than 10. Do this in two ways. First using Numpy, then using Pandas.\n”,
“\n”,
“Print the shape for both the Pandas and Numpy solution.”
]
},
{
“cell_type”: “code”,
“execution_count”: 5,
“metadata”: {},
“outputs”: [],
“source”: [
“import numpy as np\n”,
“import pandas as pd\n”,
“from sklearn.datasets import load_iris\n”,
“\n”,
“iris = load_iris()\n”,
“iris_df = pd.DataFrame(data= np.c_[iris[‘data’], iris[‘target’]],\n”,
” columns= iris[‘feature_names’] + [‘target’])\n”,
“iris_df[‘species’] = pd.Categorical.from_codes(iris.target, iris.target_names)\n”,
“iris_df = iris_df.drop(‘target’, 1)”
]
},
{
“cell_type”: “code”,
“execution_count”: 6,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/html”: [
“
\n”,
“
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
\n”,
“
”
],
“text/plain”: [
” sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n”,
“0 5.1 3.5 1.4 0.2 \n”,
“\n”,
” species \n”,
“0 setosa ”
]
},
“execution_count”: 6,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“iris_df.head(1)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 2 (10 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Consider the below two arrays. The first will be actual values and the second predicted values. Calculate the below:\n”,
“\n”,
“* MAE: Mean Absolute Error, defined as the average absolute error.\n”,
“* MSE: Mean Squared Error, defined as taking the difference between the two arrays, squaring the errors, summing and finding the mean.\n”,
“* MAPE: Mean Absolute Percentage Error, defined as the mean percentage difference between the two arrays.\n”,
“\n”,
“Solve each using one line of code, making use of Numpy array elementwise operations.\n”,
“\n”,
“Print out each metric.”
]
},
{
“cell_type”: “code”,
“execution_count”: 36,
“metadata”: {},
“outputs”: [],
“source”: [
“a = np.array([1,4,5,2,4,6,1])\n”,
“b = np.array([5,2,3,4,5,6,1])”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“metadata”: {},
“outputs”: [],
“source”: [
“# mape = abs(y – yhat)/y”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 3 (10 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Using the describe method and loc, find the standard deviation and mean for Sepal Length and Petal Length. Create a new DataFrame that is subetted to include only rows where the Sepal Length and Petal Length are greater than one standard deviation from the mean. Find the pairwise correlations for each feature for the subsetted DataFrame and the number of rows left after subsetting. Do the same process but switch the and to an or when subsetting the DataFrame.\n”,
“\n”,
“Print the row count and correlation matrix for both the and subsetting and or subsetting.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 4 (15 points)”
]
},
{
“cell_type”: “code”,
“execution_count”: 7,
“metadata”: {},
“outputs”: [],
“source”: [
“from sklearn.datasets import load_boston”
]
},
{
“cell_type”: “code”,
“execution_count”: 8,
“metadata”: {},
“outputs”: [],
“source”: [
“boston_data = load_boston()”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Load Boston Housing dataset from Scikit-Learn. Put the data into a Pandas DataFrame using the data and feature_names attributes from the boston_data object. Find the IQR (interquartile range) for AGE, which is defined as the 75th quartile – the 25th quartile. Remove observations with an AGE that are not within 1.5 IQR of the median. Find the strongest correlated feature with AGE, not including itself, and plot the two features as a scatter plot. Note strongest correlated could mean positive or negative.\n”,
“\n”,
“Print the IQR, the highest correlating feature, the correlation itself and the scatter plot.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 5 (10 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“For rating_df, do the folling 3 data transformations to each column:\n”,
“\n”,
“* min_max: 0-1 scale, defined as (x – min(x))/(max(x) – min(x))\n”,
“* mean_centered: x – mean(x)\n”,
“* z_score: (x – mean(x))/std(x)\n”,
“\n”,
“This means, for instance, each column for min max should be scaled to where the max is 1 and the min is 0.\n”,
“\n”,
“Hint, this should be done using 1 line, making use of broadcasting and rows and columnwise mean, min, max and standard deviation calculations.\n”,
“\n”,
“Print out the 3 scaled dataframes.”
]
},
{
“cell_type”: “code”,
“execution_count”: 3,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/html”: [
“
\n”,
“
star_wars | harry_potter | avengers | |
---|---|---|---|
user_1 | 4 | 2 | 5 |
user_2 | 1 | 5 | 4 |
user_3 | 2 | 4 | 2 |
user_4 | 3 | 5 | 4 |
\n”,
“
”
],
“text/plain”: [
” star_wars harry_potter avengers\n”,
“user_1 4 2 5\n”,
“user_2 1 5 4\n”,
“user_3 2 4 2\n”,
“user_4 3 5 4”
]
},
“execution_count”: 3,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“import numpy as np \n”,
“import pandas as pd \n”,
“\n”,
“user_1 = np.array([4,2,5])\n”,
“user_2 = np.array([1,5,4])\n”,
“user_3 = np.array([2,4,2])\n”,
“user_4 = np.array([3,5,4])\n”,
“\n”,
“rating_matrix = np.array([user_1, user_2, user_3, user_4])\n”,
“\n”,
“columns = [\”star_wars\”, \”harry_potter\”, \”avengers\”]\n”,
“index = [\”user_1\”, \”user_2\”, \”user_3\”, \”user_4\”]\n”,
“\n”,
“rating_df = pd.DataFrame(rating_matrix, columns = columns, index = index)\n”,
“\n”,
“rating_df”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Quesiton 6 (15 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Add a column to rating_df called \”most_similar_user\” that has the user_id of the most similar user for that given observation. Define similarity using the euclidean distance between two rating vectors. Note, when making a distance matrix, the min distance is going to be the distance between each user and themselves. Make sure the most_similar_user is not the user themself.\n”,
“\n”,
“For instance, for user_1, the most similar user, not including themself, is user_4.\n”,
“\n”,
“Print out the dataframe with the new column.”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“metadata”: {},
“outputs”: [],
“source”: [
“import pandas as pd\n”,
“import numpy as np\n”,
“from scipy.spatial.distance import cdist”
]
},
{
“cell_type”: “code”,
“execution_count”: 3,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/html”: [
“
\n”,
“
star_wars | harry_potter | avengers | |
---|---|---|---|
user_1 | 4 | 2 | 5 |
user_2 | 1 | 5 | 4 |
user_3 | 2 | 4 | 2 |
user_4 | 3 | 5 | 4 |
\n”,
“
”
],
“text/plain”: [
” star_wars harry_potter avengers\n”,
“user_1 4 2 5\n”,
“user_2 1 5 4\n”,
“user_3 2 4 2\n”,
“user_4 3 5 4”
]
},
“execution_count”: 3,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“user_1 = np.array([4,2,5])\n”,
“user_2 = np.array([1,5,4])\n”,
“user_3 = np.array([2,4,2])\n”,
“user_4 = np.array([3,5,4])\n”,
“\n”,
“rating_matrix = np.array([user_1, user_2, user_3, user_4])\n”,
“\n”,
“columns = [\”star_wars\”, \”harry_potter\”, \”avengers\”]\n”,
“index = [\”user_1\”, \”user_2\”, \”user_3\”, \”user_4\”]\n”,
“\n”,
“rating_df = pd.DataFrame(rating_matrix, columns = columns, index = index)\n”,
“\n”,
“rating_df”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 7 (10 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Use a for loop to make a 2,3 and 4 period rolling mean column for each user. Making sure to add each column to the dataframe.\n”,
“\n”,
“Print the dataframe out.”
]
},
{
“cell_type”: “code”,
“execution_count”: 4,
“metadata”: {},
“outputs”: [],
“source”: [
“import numpy as np\n”,
“import pandas as pd”
]
},
{
“cell_type”: “code”,
“execution_count”: 5,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/html”: [
“
\n”,
“
id | metric | |
---|---|---|
0 | a | 5 |
1 | a | 3 |
2 | a | 2 |
3 | a | 4 |
4 | a | 5 |
5 | b | 1 |
6 | b | 4 |
7 | b | 1 |
8 | b | 4 |
9 | b | 2 |
10 | c | 5 |
11 | c | 3 |
12 | c | 1 |
13 | c | 2 |
14 | c | 3 |
\n”,
“
”
],
“text/plain”: [
” id metric\n”,
“0 a 5\n”,
“1 a 3\n”,
“2 a 2\n”,
“3 a 4\n”,
“4 a 5\n”,
“5 b 1\n”,
“6 b 4\n”,
“7 b 1\n”,
“8 b 4\n”,
“9 b 2\n”,
“10 c 5\n”,
“11 c 3\n”,
“12 c 1\n”,
“13 c 2\n”,
“14 c 3”
]
},
“execution_count”: 5,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“metric = np.array([5,3,2,4,5,1,4,1,4,2,5,3,1,2,3])\n”,
“ids = np.array([\”a\”,\”a\”,\”a\”,\”a\”,\”a\”,\”b\”,\”b\”,\”b\”,\”b\”,\”b\”,\”c\”,\”c\”,\”c\”,\”c\”,\”c\”])\n”,
“\n”,
“df = pd.DataFrame({\n”,
” \”id\”:ids,\n”,
” \”metric\”:metric\n”,
“})\n”,
“df”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“#### Question 8 (15 points)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“Find pairwise correlations for each ids period of data, meaning for each id a,b,c, treat metric for periods 1-5 as a vector, and find the correlations. The result should be a 3 x 3 correlation matrix.\n”,
“\n”,
“Find the highest correlating users (negative or positive) using the correlation matrix. Create a dataframe with two columns, the first being an id (a,b,c) and the second column the highest correlating id.\n”,
“\n”,
“Print the 3 by 3 correlation matrix and the two column dataframe with the most similar ids.”
]
},
{
“cell_type”: “code”,
“execution_count”: 21,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/html”: [
“
\n”,
“
id | metric | period | |
---|---|---|---|
0 | a | 5 | 1 |
1 | a | 3 | 2 |
2 | a | 2 | 3 |
3 | a | 4 | 4 |
4 | a | 5 | 5 |
\n”,
“
”
],
“text/plain”: [
” id metric period\n”,
“0 a 5 1\n”,
“1 a 3 2\n”,
“2 a 2 3\n”,
“3 a 4 4\n”,
“4 a 5 5”
]
},
“execution_count”: 21,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“metric = np.array([5,3,2,4,5,1,4,1,4,2,5,3,1,2,3])\n”,
“periods = [1,2,3,4,5] * 3\n”,
“ids = np.array([\”a\”,\”a\”,\”a\”,\”a\”,\”a\”,\”b\”,\”b\”,\”b\”,\”b\”,\”b\”,\”c\”,\”c\”,\”c\”,\”c\”,\”c\”])\n”,
“\n”,
“df = pd.DataFrame({\n”,
” \”id\”:ids,\n”,
” \”metric\”:metric,\n”,
” \”period\”: periods\n”,
“})\n”,
“\n”,
“df.head()”
]
}
],
“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.6”
}
},
“nbformat”: 4,
“nbformat_minor”: 4
}
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.