<jupyter_start><jupyter_text>**20 Basic Numpy Exercises - From Array Creation to making an array Immutable**<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>**1. Check your version**<jupyter_code>np.__version__<jupyter_output><empty_output><jupyter_text>**2. Create a 3x3 Array filled with zeroes**<jupyter_code>arr_0 = np.zeros((3,3))
arr_0<jupyter_output><empty_output><jupyter_text>**3. Create a 3x3 Array filled with the number 7**<jupyter_code>arr_7 = np.full((3,3),7)
arr_7<jupyter_output><empty_output><jupyter_text>**4. Create a vector of at least size 5 filled with Random Values between -1 and 1**<jupyter_code>v = np.random.uniform(-1,1,(1,5))
v<jupyter_output><empty_output><jupyter_text>**5. Multiply the two matricies (A and B) below.  (Assign the answer to the variable ANS) - Run the cell below to check your answer**<jupyter_code>A = np.array([[.15,.55,.7]])
B = np.array([[1,2,3],[4,5,6],[7,8,9]])
ans = np.dot(A,B)
# Checking the answer!
assert np.sum(ans) == 25.95,'Try again'
print('Good job!', ans)<jupyter_output>Good job! [[ 7.25  8.65 10.05]]<jupyter_text>**6. Combine the [ans] array created in 5 with the following 2 arrays below.**<jupyter_code>first_row = np.array([[0.1, 0.1, 9.9]])
last_row = np.array([[9.9, 9.9, 0.1]])
new_ans = np.vstack((first_row, ans ,last_row))
new_ans
new_ans = np.concatenate((first_row, ans ,last_row),axis=0)
new_ans<jupyter_output><empty_output><jupyter_text>**7. Swap the first and last columns in the combined array.**<jupyter_code>swapped_ans = np.concatenate(
    (new_ans[:,2].reshape(3,1),new_ans[:,1].reshape(3,1),new_ans[:,0].reshape(3,1)),axis=1)
swapped_ans<jupyter_output><empty_output><jupyter_text>**8. Swap the first and the middle row in the combined array**<jupyter_code>row_swap = np.vstack((swapped_ans[1,:],swapped_ans[0,:],swapped_ans[2,:]))
row_swap<jupyter_output><empty_output><jupyter_text>**9. Save the combined array from 8 to file.**<jupyter_code>np.savetxt('my_txt_array.txt', row_swap, delimiter=' ')
np.savetxt('my_csv_array.csv', row_swap, delimiter=',')<jupyter_output><empty_output><jupyter_text>**10. Load the saved array from file into a new array.**<jupyter_code>load_txt = np.loadtxt('my_txt_array.txt')
load_txt
load_csv = np.genfromtxt('my_csv_array.csv', delimiter=',')
load_csv<jupyter_output><empty_output><jupyter_text>**11. Convert all array elements to float32**<jupyter_code>fp32_arr = row_swap.astype(np.float32)<jupyter_output><empty_output><jupyter_text>**12. Find the mean of the middle row of the following array.**<jupyter_code># For Questions 12-19 use the following array
arr = np.random.randint(1,10,(7,7))
arr
arr[len(arr)//2,:].mean()<jupyter_output><empty_output><jupyter_text>**13. Find the Variance of the array.**<jupyter_code>arr.var()<jupyter_output><empty_output><jupyter_text>**14. Slice the array into a new array of just it's middle column.**<jupyter_code>arr[:,len(arr[0])//2]
# If you need it to still be the same shape, you would want to reshape it with
# .reshape(len(arr[0]),1)<jupyter_output><empty_output><jupyter_text>**15. Create a new Array of all the elements on the left to right, top to bottom, diagnal.**<jupyter_code>np.diag(arr)<jupyter_output><empty_output><jupyter_text>**16. Find the most frequently occuring value in the array.**<jupyter_code>np.argmax(np.bincount(arr.flatten()))<jupyter_output><empty_output><jupyter_text>**17. Normalize the array**<jupyter_code>norm_arr = arr / np.max(arr)
norm_arr<jupyter_output><empty_output><jupyter_text>**18. Round the normalized array down to the nearest int.**<jupyter_code>np.floor(norm_arr)<jupyter_output><empty_output><jupyter_text>**19. Make the array Unmutable**<jupyter_code>norm_arr.flags.writeable = False<jupyter_output><empty_output><jupyter_text>**20. What do you get when you multiply the following arrays `[True, True, False] * [False, False, True]`?**<jupyter_code>arr = np.array([1,1,0])
f_arr = arr == 1
s_arr = arr < 1
print(f_arr, s_arr)
print()
print(f_arr * s_arr)<jupyter_output>[ True  True False] [False False  True]

[False False False]
{'fraction_non_alphanumeric': 0.15612745098039216, 'fraction_numerical': 0.03333333333333333, 'mean_word_length': 7.682978723404255, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 83, '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': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '501356', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1605, 'n_words': 331, 'path': 'numpy/Numpy_Exercises - Solutions.ipynb', 'repo_name': 'digitalvirtuoso/practical-python', 'size': '4080'}