<jupyter_start><jupyter_text>More on NumPy<jupyter_code># !pip install numpy<jupyter_output><empty_output><jupyter_text>Reshape array The reshape() function is used to give a new shape to an array without changing its data. Array to be reshaped. The new shape should be compatible with the original shape.<jupyter_code>import numpy as np
m = np.array([[1, 2, 3, 4], 
              [5, 6, 7, 8], 
              [9, 10, 11, 12]]) 
print('Original array: shape =',m.shape, ' dim =', m.ndim)
m
m2 = m.reshape(4, 3)
print('shape =',m2.shape, ' dim =', m2.ndim)
m2
m3 = m.reshape(2,6)
print('shape =',m3.shape, ' dim =', m3.ndim)
m3
m4 = m.reshape(1,12)
print('shape =',m4.shape, ' dim =', m4.ndim)
m4
m5 = m.reshape(12,1)
print('shape =',m5.shape, ' dim =', m5.ndim)
m5<jupyter_output>shape = (12, 1)  dim = 2<jupyter_text>Remember the new shape must be compatible with the original shape.<jupyter_code>m7 = m.reshape(2,5)         # This will raise an error<jupyter_output><empty_output><jupyter_text>Flatten array In some cases, we need a one-dimensional array. That is, we need a copy of the array collapsed into one dimension.<jupyter_code>m6 = m.reshape(12)
print('shape =',m6.shape, ' dim =', m6.ndim)
m6<jupyter_output>shape = (12,)  dim = 1<jupyter_text>There is an easy way:<jupyter_code>m7 = m.flatten()
print('shape =',m7.shape, ' dim =', m7.ndim)
m7<jupyter_output>shape = (12,)  dim = 1<jupyter_text>Iterating Iterating means going through elements one by one. Iterating 1-D array<jupyter_code>m6
for x in m6:
    print(x)<jupyter_output>1
2
3
4
5
6
7
8
9
10
11
12<jupyter_text>Iterating 2-D array<jupyter_code>m3
for x in m3:
    print(x)<jupyter_output>[1 2 3 4 5 6]
[ 7  8  9 10 11 12]<jupyter_text>If we iterate on a n-D array it will go through all dimensions one by one.  Array concatenation Concatenate a sequence of arrays  1-D arrays<jupyter_code>a1 = np.array([1, 1, 1])
a2 = np.array([2, 2, 2])
np.concatenate([a1, a2])<jupyter_output><empty_output><jupyter_text>Remember, NumPy array are not Python lists. If you use the + operator, yo will not concatenate the arrays,  you will get:<jupyter_code>print(a1 + a2)<jupyter_output>[3 3 3]<jupyter_text>2-D arrays 2-D arrays have two dimensions. You need to specify the dimension you want to use for concatenation.axis:{0 or ‘index’, 1 or ‘columns’}<jupyter_code>x0=np.zeros((2,2))
x0
x1=np.ones((2,2))
x1
concat1 = np.concatenate([x0,x1])   # Default value: axis=0
print(concat1,'\n')
print('Shape =',concat1.shape)<jupyter_output>[[0. 0.]
 [0. 0.]
 [1. 1.]
 [1. 1.]] 

Shape = (4, 2)<jupyter_text>You can include `axis=0`. It will produce the same result:<jupyter_code>concat1 = np.concatenate([x0,x1], axis=0)
print(concat1,'\n')
print('Shape =',concat1.shape)
concat2 = np.concatenate([x0,x1], axis=1)
print(concat2,'\n')
print('Shape =',concat2.shape)<jupyter_output>[[0. 0. 1. 1.]
 [0. 0. 1. 1.]] 

Shape = (2, 4)<jupyter_text>Arrays of mixed dimensions For working with arrays of mixed dimensions, it can be clearer to use:- `np.vstack` (vertical stack) - `np.hstack` (horizontal stack) functions<jupyter_code># Using vstack
a1 = np.array([1,2,3])
print('a1 =\n',a1)
a2 = np.array([[4,4,4],[5,5,5]])
print('a2 =\n',a2)
np.vstack([a1,a2])
# Using hstack
print('a2 =\n',a2)
a3 = np.array([[8],[8]])
print('a3 =\n',a3)
np.hstack([a2,a3])<jupyter_output>a2 =
 [[4 4 4]
 [5 5 5]]
a3 =
 [[8]
 [8]]<jupyter_text>Array split Splitting is the opposite of concatenation<jupyter_code>x = np.arange(9)
print('x =',x)
x1, x2, x3 = np.split(x, 3)
print(x1)
print(x2)
print(x3)<jupyter_output>x = [0 1 2 3 4 5 6 7 8]
[0 1 2]
[3 4 5]
[6 7 8]<jupyter_text>You can decide where to cut the original array to get arrays with different sizes.Here, we will get 3 arrays of different sizes:- x1, from 0 to 2-1 (2 is not included)- x2, from 2 to 6-1 (6 is not included)- x3, from 6 to the latest element<jupyter_code>x1, x2, x3 = np.split(x, [2,6])
print('x1 =', x1)
print('x2 =', x2)
print('x3 =', x3)
g = np.arange(16).reshape((4,4))
g
# vertical split
up, low = np.vsplit(g, 2)
print('up =\n',up)
print('low =\n',low)
# horizontal split
l, r = np.hsplit(g, 2)
print('left  =\n', l)
print('right =\n',r)<jupyter_output>left  =
 [[ 0  1]
 [ 4  5]
 [ 8  9]
 [12 13]]
right =
 [[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]<jupyter_text>Search You can search an array for a certain value, and return the indexes that get a match.<jupyter_code>ar = np.array([-5, 6, 4, 4, 1, 0, -6])
print('Searching for 1', np.where(ar == 1))
print('Searching for 4', np.where(ar == 4))
print('Searching for 9', np.where(ar == 9))<jupyter_output>Searching for 9 (array([], dtype=int64),)<jupyter_text>Find the index where the values are oddRemember % returns the remainder of the floor division<jupyter_code>print('Searching for odd', np.where(ar%2 == 1))
print('Searching for positive numbers', np.where(ar > 0))<jupyter_output>Searching for positive numbers (array([1, 2, 3, 4], dtype=int64),)<jupyter_text>Sort<jupyter_code>ar = np.random.randint(10, size=10)
print('Original array:',ar)
print('Sorted array:  ',np.sort(ar))<jupyter_output>Original array: [7 6 2 5 0 4 6 5 2 0]
Sorted array:   [0 0 2 2 4 5 5 6 6 7]<jupyter_text>This method returns a copy of the array, leaving the original array unchanged.<jupyter_code># Sorting a 2-D array
ar2 = np.array([[3, 2, 5], [7, 0, 1]])

print(np.sort(ar2))
ar2 = np.array([[1,0,2],[5,6,4],[3,-1,6]])
ar2
# array elements in sorted order by row
print(np.sort(ar2))
# column-wise sorted array elements
print(np.sort(ar2, axis=0))<jupyter_output>[[ 1 -1  2]
 [ 3  0  4]
 [ 5  6  6]]
{'fraction_non_alphanumeric': 0.16567513971516135, 'fraction_numerical': 0.06526050117180458, 'mean_word_length': 4.731404958677686, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '643731', 'license': 'MIT', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2288, 'n_words': 670, 'path': '03-Introduction to Numpy/03-More on Numpy.ipynb', 'repo_name': 'Goliath-Research/Introduction-to-Data-Science', 'size': '5547'}