<jupyter_start><jupyter_text>Brain Teasersfor more brain teasers, please see: http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html<jupyter_code>import numpy as np

np.set_printoptions(precision=2)<jupyter_output><empty_output><jupyter_text>What version of NumPy are you using?<jupyter_code># note double underscrore 
print(np.__version__)<jupyter_output>1.10.4<jupyter_text>Display array elements in reverse order<jupyter_code>my_array = np.array(np.arange(10))
my_array
my_array[::-1]<jupyter_output><empty_output><jupyter_text>Triple elements in array<jupyter_code>my_array * 3<jupyter_output><empty_output><jupyter_text>Create array with 20 zeros; every fifth element equals four<jupyter_code>my_array = np.zeros(20)
my_array[0::5] = 4
my_array<jupyter_output><empty_output><jupyter_text>Create a 3x3 matrix whose elements equal 0 through 8<jupyter_code>my_matrix = np.asmatrix(np.array(np.arange(9)).reshape((3,3)))
my_matrix<jupyter_output><empty_output><jupyter_text>Transpose the rows and columns<jupyter_code>my_matrix.T<jupyter_output><empty_output><jupyter_text>Create a 5x5 identity matrix with integer components<jupyter_code>my_matrix = np.asmatrix(np.eye(5, dtype='int'))
my_matrix<jupyter_output><empty_output><jupyter_text>Find the mean of a vector with 30 random elements.  Can you guess the approximate value for the mean?<jupyter_code>my_random_vector = np.random.random(30)
my_random_vector.mean()<jupyter_output><empty_output><jupyter_text>Repeat, and visually estimate the mean for the sum of the two means<jupyter_code>my_random_vector = np.random.random(30)
my_random_vector.mean()<jupyter_output><empty_output><jupyter_text>Create a 2d array with ones on the border and zeros inside the border<jupyter_code>my_bordered_array = np.ones((5,5))
my_bordered_array[1:-1,1:-1] = 0
my_bordered_array<jupyter_output><empty_output><jupyter_text>Create an 8x8 checker board with alternating zeros and ones<jupyter_code>my_checker_board = np.zeros((8,8),dtype=int)
my_checker_board[1::2,::2] = 1
my_checker_board[::2,1::2] = 1
print(my_checker_board)
my_alternative_checker_board = np.tile( np.array([[0,1],[1,0]]), (4,4))
print(my_alternative_checker_board)<jupyter_output>[[0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]
 [0 1 0 1 0 1 0 1]
 [1 0 1 0 1 0 1 0]]<jupyter_text>Create a sorted vector that contain 'n' random numbers<jupyter_code>vector_size = 12
my_random_vector = np.random.random(vector_size)
my_sorted_random_vector = np.sort(my_random_vector)
print (my_sorted_random_vector)<jupyter_output>[ 0.04  0.09  0.16  0.17  0.18  0.25  0.33  0.46  0.61  0.68  0.84  0.86]<jupyter_text>Without sorting, replace largest element in random array with the value 1234<jupyter_code>my_random_vector[my_random_vector.argmax()] = 1234
my_random_vector<jupyter_output><empty_output><jupyter_text>Given the following data type and data set; sort according to 'height'source: http://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html<jupyter_code>camelot_dtype = [('name', 'S10'), ('height', float), ('age', int)]
camelot_values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)]
camelot_structured_array = np.array(camelot_values, dtype=camelot_dtype)
camelot_sorted_array = np.sort(camelot_structured_array, order='height')
for n in np.arange(camelot_sorted_array.size):
    print (camelot_sorted_array[n])<jupyter_output>(b'Galahad', 1.7, 38)
(b'Arthur', 1.8, 41)
(b'Lancelot', 1.9, 38)<jupyter_text>Make an array read-only (immutable)<jupyter_code>my_ordinary_array = np.array(np.arange(12))
my_ordinary_array.flags.writeable = False
my_ordinary_array[5] = 1234<jupyter_output><empty_output><jupyter_text>Print enumerated values from a 3x3 NumPy array<jupyter_code>my_3_3_array = np.arange(9).reshape(3,3)
for index, value in np.ndenumerate(my_3_3_array):
    print(index, value)<jupyter_output>(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
{'fraction_non_alphanumeric': 0.12687687687687688, 'fraction_numerical': 0.06006006006006006, 'mean_word_length': 7.27536231884058, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '341831', 'license': 'MIT', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1606, 'n_words': 344, 'path': 'numpy-data-science-essential-training/Ex_Files_NumPy_Data_EssT/Exercise Files/Ch 6/06_06/Finish/Brain Teasers.ipynb', 'repo_name': 'saint1729/in-learning', 'size': '3996'}