<jupyter_start><jupyter_text>numpy library<jupyter_code># importing the numpy module
import numpy as np<jupyter_output><empty_output><jupyter_text>Creating arrays<jupyter_code># 1 dimensional
a = np.array([1,2,3,4,5])
print("The array: ", a)
print("Dimension of the array: ", a.ndim) 
print("Shape of the array: ", a.shape)
print("Legth of the array: ", len(a))
print("\n")

# 2 dimensional
b = np.array(([1,2,3],[4,5,6]))
print("The array: \n", b)
print("Dimension of the array: ", b.ndim) 
print("Shape of the array: ", b.shape)
print("Legth of the array: ", len(b))<jupyter_output>The array:  [1 2 3 4 5]
Dimension of the array:  1
Shape of the array:  (5,)
Legth of the array:  5


The array: 
 [[1 2 3]
 [4 5 6]]
Dimension of the array:  2
Shape of the array:  (2, 3)
Legth of the array:  2<jupyter_text>Generating Sequence of array<jupyter_code># Evenly spaced
a = np.arange(10)
print(a)
print(np.arange(10,50,10))
print("\n")

# By number of points
print(np.linspace(0,1,6))       # dividing 6 partitions from 0 to 1<jupyter_output>[0 1 2 3 4 5 6 7 8 9]
[10 20 30 40]


[0.  0.2 0.4 0.6 0.8 1. ]<jupyter_text>Common arrays<jupyter_code>print(np.ones((3,3)))
print("\n")

print(np.zeros((3,3)))
print("\n")

print(np.eye(3))
print("\n")

print(np.diag(np.array([1,2,3])))<jupyter_output>[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]


[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]


[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]


[[1 0 0]
 [0 2 0]
 [0 0 3]]<jupyter_text>Generating random numbers in numpy<jupyter_code>r = np.random.rand(4)       # generates random floating point numbers between 0 to 1
print("Random numbers generated are: ", r)
r.dtype<jupyter_output>Random numbers generated are:  [0.32782768 0.70724533 0.53552084 0.1775902 ]<jupyter_text>Datatypes in numpy<jupyter_code>a = np.array([1,2,3,4,5], dtype='float64')
print(a)
print(a.dtype)

b = np.array([1,2,3,4,5], dtype='int64')
print(b)
print(b.dtype)

com = np.array([1+ 1j, 2+5j])
print(com)
print(com.dtype)<jupyter_output>[1. 2. 3. 4. 5.]
float64
[1 2 3 4 5]
int64
[1.+1.j 2.+5.j]
complex128<jupyter_text>Indexing and Slicing<jupyter_code>num = np.arange(10)
print(num)
print(num[5])
print(num[:5])
print(num[-1])
print(num[: : -1])  # for reversing the sequence
print(num[2:9:3])
# for mutli dimensional ndarrays
mat = np.diag(np.array([1,2,3,4]))
print(mat)

print(mat[2,2])

mat[2,1] = 99   # updating the matrices
mat<jupyter_output>[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]
3<jupyter_text>* A slicing operation craetes a view on the original array, which is just a way of accessing array data 

* Thus the original array is not copied into the memory it's just a view

* we can use np.share_memory() to check whether if two arrays share the same memory or not<jupyter_code>a = np.arange(10)
print("a: ", a)

b = a[::2]          # view
print("b: ", b)

np.may_share_memory(a,b)
b[0] = 12

print("a: ", a)
print("b: ", b)     # when modifying the view(b) even the original array(a) also has been modified automatically
a = np.arange(10)
print("a: ", a)

b = a[::2].copy()   # forcing a copy to avoid data redundancy while doing array manipulations
print("b: ", b)

b[0] = 12
print("a: ", a)
print("b: ", b)<jupyter_output>a:  [0 1 2 3 4 5 6 7 8 9]
b:  [0 2 4 6 8]
a:  [0 1 2 3 4 5 6 7 8 9]
b:  [12  2  4  6  8]<jupyter_text>Numerical operations on ndarray<jupyter_code># Element-wise operations
a = np.array([0,1,2,3,4,5])
print("a:   ", a)
print("a+1: ", a+1)
print("2**a:", a**2)

# array multiplication
c = np.ones((3,3))
print("Array multiplicaiton: \n", c*c)

# matrix muliplication
print("Matrix multiplication: \n", c.dot(c))<jupyter_output>a:    [0 1 2 3 4 5]
a+1:  [1 2 3 4 5 6]
2**a: [ 0  1  4  9 16 25]
Array multiplicaiton: 
 [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
Matrix multiplication: 
 [[3. 3. 3.]
 [3. 3. 3.]
 [3. 3. 3.]]<jupyter_text>Array shape manipulations<jupyter_code># Flattening
a = np.array([[1,2,3],[4,5,6]])
print("a:\n", a)

print("Flattened a: ", a.ravel())   # flattening 2d to 1d
# Reshaping
print("Reshaped a:\n", a.reshape(3,2))  # reverse of flattening
# Resizing
a = np.arange(4)
print("a: ", a)
a.resize((8,))
print("resized a: \n", a)
# Sorting data
a = np.array([[3,2,1],[10,5,6]])
print("a:\n", a)

a.sort()
print("Sorted a: \n",a)<jupyter_output>a:
 [[ 3  2  1]
 [10  5  6]]
Sorted a: 
 [[ 1  2  3]
 [ 5  6 10]]
{'fraction_non_alphanumeric': 0.18499206529131715, 'fraction_numerical': 0.08116073452731806, 'mean_word_length': 3.605427974947808, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '199636', 'license': 'MIT', 'n_tokens_mistral': 2152, 'n_tokens_neox': 1946, 'n_words': 548, 'path': 'Numpy/numpy.ipynb', 'repo_name': 'Mithun162001/Python-Notebooks', 'size': '4411'}