<jupyter_start><jupyter_text>Reshaping arrays<jupyter_code>import numpy as np
a = np.floor(10*np.random.random((3,4)))
a
a.shape
b=a.ravel()
b
b.shape
a.shape
a.T
a.T.shape
a.shape
a.reshape(2,6)
a.shape

a.resize(2,6)
a.shape
a
a.reshape(3,4)
a.shape<jupyter_output><empty_output><jupyter_text>Stacking arrays<jupyter_code>a = np.floor(10*np.random.random((2,2)))
a

b = np.floor(10*np.random.random((2,2)))
b
np.vstack((a,b))
a.shape
np.hstack((a,b))
c = np.hstack((a,b))
c
a.shape

np.column_stack((a,b))
np.row_stack((a,b))
# The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays
d = np.array([2.,3.])
e = np.array([4.,5.])
np.column_stack((d,e))
np.hstack((d,e))
#the function row_stack is equivalent to vstack for any input arrays. 
np.row_stack((d,e))
 from numpy import newaxis
d[:,newaxis]               # this allows to have a 2D columns vector
np.column_stack((d[:,newaxis],e[:,newaxis]))
np.row_stack((d[:,newaxis],e[:,newaxis]))
np.r_[1:5,0,4]

np.c_[1:4]
np.concatenate((d,e))<jupyter_output><empty_output><jupyter_text>Splitting one array into several smaller ones<jupyter_code>a = np.floor(10*np.random.random((2,12)))
a
np.hsplit(a,3)   # Split a into 3
f=np.hsplit(a,3)
f[0]
f[1]
f[0][0]
f[0][0][0]
np.hsplit(a,(3,4))   # Split a after the third and the fourth column
g= np.hsplit(a,(3,4)) 
g[0]

data=np.arange(6).reshape((3,2))
data
 np.average(range(1,5),weights=range(4,0,-1))
a=np.array([-1.7, -1.3, -0.2,0.2,1.2,1.75,1.7,2.3])
np.floor(a)
np.sum([[1,2],[3,4]], axis=0)
np.sum([[1,5],[3,4]], axis=1)<jupyter_output><empty_output><jupyter_text>Copies and Views   No copy at all   Simple assignments make no copy of array objects or of their data.<jupyter_code>a = np.arange(12)
a
b = a[::2]
b

a is b # a and b are two names for the same ndarray object
b=a
a is b
 b.shape = 3,4    # changes the shape of a
a.shape<jupyter_output><empty_output><jupyter_text>Python passes mutable objects as references, so function calls make no copy.<jupyter_code>def f(x):
    print(id(x))

id(a) # id is a unique identifier of an object
f(a)<jupyter_output>2974836268688<jupyter_text>View or Shallow CopyDifferent array objects can share the same data. The view method creates a new array object that looks at the same data.<jupyter_code>c = a.view()
c is a 
c.base is a # c is a view of the data owned by a
c.flags.owndata
c.shape = 6,2
a.shape # a's shape doesn't change
c
 c[0,1] = 1234                      # a's data changes
c
a
s = a[ : , 1:3]
s
s[:]
s[:]=10
s[:]
s=10
s
a<jupyter_output><empty_output><jupyter_text>Deep copy<jupyter_code>d = a.copy()                          # a new array object with new data is created
d is a
d.base is a                           # d doesn't share anything with a
d[0,0] = 9999
a
d<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16100495399858458, 'fraction_numerical': 0.04423213021939137, 'mean_word_length': 4.28411214953271, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '73797', 'license': 'MIT', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1194, 'n_words': 303, 'path': 'Numpy.ipynb', 'repo_name': 'ramshagit/Helloworld', 'size': '2826'}