<jupyter_start><jupyter_text>Day 2 Data wrangling with Python - Exercises with answers  Exercise 1  Question 1 Create an array of 25 numbers between 1 and 100 (including 100) and save to `arr1` variable. Pring the type and data type of `arr1`. Answer:<jupyter_code>import numpy as np
arr1 = np.linspace(1, 100, 25)
arr1
print(type(arr1))
print(arr1.dtype)<jupyter_output>float64<jupyter_text>Question 2 Create an array of 20 numbers from 50 to 70 (excluding 70). Use `arange` function and assign the output to `arr2` variable. Answer:<jupyter_code>arr2 = np.arange(50, 70)
arr2<jupyter_output><empty_output><jupyter_text>Question 3 Create an array of all even numbers between 30 and 60 (excluding 60). Create an array of numbers between 0 and 100 (including 100), with step size of 25. Answer:<jupyter_code>np.arange(30, 60, 2)
np.arange(0, 101, 25)<jupyter_output><empty_output><jupyter_text>Question 4 Create an array of 10 numbers between 0 and 20 (including 20) assign to `arr3` variable. Use linspace function. Find the max, min and sum of the array. Answer:<jupyter_code>arr3 = np.linspace(0, 20, 10)
arr3
np.amin(arr3)
np.amax(arr3)
np.sum(arr3)<jupyter_output><empty_output><jupyter_text>Question 5 Create two arrays of size 4 with the numbers 4, 5, 6, 4 in `array1` and 7, 2, 12, 16 in `array2`. Find the `sum`,  `product`, `difference` and `quotient` of `array1` and `array2` and save as `array_sum`, `product`, `difference`, and `quotient` variables respectively. Print the results. Answer:<jupyter_code>array1 = np.array([4, 5, 6, 4])
array2 = np.array([7, 2, 12, 16])
array_sum = array1 + array2
print(array_sum)
product = array1 * array2
print(product)
difference = array1 - array2
print(difference)
quotient = array1 / array2
print(quotient)<jupyter_output>[0.57142857 2.5        0.5        0.25      ]<jupyter_text>Exercise 2 Question 1 Compute the absolute value of each entry in the following array `[5, -8, -66, 4]` and assign it to `abs_values` variable. Print the result. Print the length of the `abs_values` array. Print the 3rd element of `abs_values`. Answer:<jupyter_code>abs_values = abs(np.array([5, -8, -66, 4]))
print(len(abs_values))
print(abs_values[2])<jupyter_output>66<jupyter_text>Question 2 Print all numbers greater than 25 in `abs_values` array. Answer:<jupyter_code>abs_values[abs_values > 25]<jupyter_output><empty_output><jupyter_text>Question 3 Create a matrix with 3 rows and assign to variable `mat1`.- [4, 5, 6]- [8, 3, 2]- [6, 7, 3] Find the shape of the matrix. Answer:<jupyter_code>mat1 = np.array([[4, 5, 6], [8, 3, 2], [6, 7, 3]])
mat1
print(mat1.shape)<jupyter_output>(3, 3)<jupyter_text>Question 4 Find the value in the 2nd row and 3rd column. Print the 2nd and 3rd rows. Print the 1st column and rows up to the 2nd row. Print all columns starting with column 2. Answer:<jupyter_code>print(mat1[1, 2])
print(mat1[1:, :])
print(mat1[:2, 0])
print(mat1[:, 1:])<jupyter_output>[[5 6]
 [3 2]
 [7 3]]<jupyter_text>Question 5 Filter and print the elements greater than 5 in `mat1`. Use a Boolean mask to filter all elements <= 5 and set them to 0. Print the resulting matrix. Answer:<jupyter_code>print(mat1[mat1>5])
mat1[mat1<=5] = 0
print(mat1)<jupyter_output>[[0 0 6]
 [8 0 0]
 [6 7 0]]<jupyter_text>Question 6 Create a `numpy` array of 16 numbers between 10 and 25 (inclusive), and call it `arr4`. Use linspace function. Reshape the array to a matrix of shape (4,4) and print it. Reshape the array to a matrix with 8 rows and let Python infer the number of columns. Answer:<jupyter_code>arr4 = np.linspace(10,25,16)
arr4
print(arr4.reshape(4,4))
print(arr4.reshape(8,-1))<jupyter_output>[[10. 11.]
 [12. 13.]
 [14. 15.]
 [16. 17.]
 [18. 19.]
 [20. 21.]
 [22. 23.]
 [24. 25.]]<jupyter_text>Question 7 Import `pandas` as `pd` and `os` packages. Set the working directory to data directory. Print the working directory. Answer:<jupyter_code>import pandas as pd
import os
# Windows only.
main_dir = "C:/Users/[user_name]/Desktop/skill-soft"
# Mac only.
main_dir = "/Users/[username]/Desktop/skill-soft"
data_dir = main_dir + "/data/"
# Set working directory.
os.chdir(data_dir)
# Check working directory.
print(os.getcwd())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12455346511074065, 'fraction_numerical': 0.06930221481305072, 'mean_word_length': 5.11353711790393, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '456809', 'license': 'MIT', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1507, 'n_words': 526, 'path': 'Exercises/day-2-data-wrangling-with-python-exercise-with-answers.ipynb', 'repo_name': 'tincho-mehaudy/data-wrangling-with-python', 'size': '4199'}