<jupyter_start><jupyter_text>Numpy Basics NumPy provides an N-dimensional array type, the ndarray, which describes a collection of “items” of the *same* type.  The items can be indexed using for example N integers.  All ndarrays are homogeneous: every item takes up the same size block of memory, and all blocks are interpreted in exactly the same way.  An item extracted from an array, e.g., by indexing, is represented by a Python object whose type is one of the array scalar types built in NumPy.   NumPy Array Attributes<jupyter_code>import numpy as np
np.random.seed(0)
def array_info(array: np.ndarray) -> None:
    print(f"ndim: {array.ndim}")
    print(f"shape: {array.shape}")
    print(f"size: {array.size}")
    print(f"dtype: {array.dtype}")
    print(f"values:\n{array}\n")<jupyter_output><empty_output><jupyter_text>Array Indexing and SlicingArray indexing refers to any use of the square brackets ([]) to index array values. There are many options to indexing, which give numpy indexing great power.Most of the following examples show the use of indexing when referencing data in an array. The examples work just as well when assigning to an array.Note that slices of arrays do not copy the internal array data but only produce new views of the original data. ![](../media/np_matrix_indexing.png)<jupyter_code>x = np.array([[1, 2], [3, 4], [5, 6]])

array_info(x)
print(x[:3])
print(x[1:])
print(x[1:2])
print(x[::-1])
print(x[0, :])
print(x[0])
print(x[:, 0])
mean = [0, 0]
cov = [[1, 2],
       [2, 5]]

x = np.random.multivariate_normal(mean=mean, cov=cov, size=10)

print(x)
print(x.shape)
rand_idxs = np.random.randint(low=0, high=x.shape[0], size=3)

print(rand_idxs)
x_subsample = x[rand_idxs, :]

print(x_subsample)
x_subsample = x[rand_idxs]

print(x_subsample)<jupyter_output>[[ 0.03761145 -0.8336645 ]
 [ 0.03761145 -0.8336645 ]
 [-1.78290539 -3.87118733]]<jupyter_text>Subarrays are views<jupyter_code>print(x)
x_sub_array = x[:2, :2]

array_info(x_sub_array)
x_sub_array[0, 0] = -1

array_info(x_sub_array)
array_info(x)<jupyter_output>ndim: 2
shape: (10, 2)
size: 20
dtype: float64
values:
[[-1.         -3.87118733]
 [-1.76178869 -1.82780883]
 [-1.35141055 -4.32039163]
 [-0.81984535 -2.14310962]
 [-0.06176746  0.29530878]
 [-0.68960528 -0.09076013]
 [-0.74967019 -1.67816385]
 [-0.53776779 -0.93711981]
 [-1.30183841 -3.36497764]
 [ 0.03761145 -0.8336645 ]]<jupyter_text>Creating copies of arrays<jupyter_code>x_copy = x[:2, :2].copy()

array_info(x_copy)
x_copy[0, 0] = 42

array_info(x_copy)
array_info(x)<jupyter_output>ndim: 2
shape: (10, 2)
size: 20
dtype: float64
values:
[[-1.         -3.87118733]
 [-1.76178869 -1.82780883]
 [-1.35141055 -4.32039163]
 [-0.81984535 -2.14310962]
 [-0.06176746  0.29530878]
 [-0.68960528 -0.09076013]
 [-0.74967019 -1.67816385]
 [-0.53776779 -0.93711981]
 [-1.30183841 -3.36497764]
 [ 0.03761145 -0.8336645 ]]<jupyter_text>Reshaping of Arrays<jupyter_code>a = np.arange(start=1, stop=10)

array_info(a)
grid = np.reshape(a, newshape=(3, 3))

array_info(grid)
x = np.array([1, 2, 3])

array_info(x)
x = np.reshape(x, newshape=(1, 3))

array_info(x)
array_info(x)

x = x[np.newaxis, :]

array_info(x)
array_info(x)

x = x.reshape((3, 1))

array_info(x)
array_info(x)

x = x.ravel()

array_info(x)
x = x.reshape((3, 1))
array_info(x)

x = x.flatten()

array_info(x)<jupyter_output>ndim: 2
shape: (3, 1)
size: 3
dtype: int32
values:
[[1]
 [2]
 [3]]

ndim: 1
shape: (3,)
size: 3
dtype: int32
values:
[1 2 3]<jupyter_text>“Automatic” Reshaping<jupyter_code>a = np.arange(30)

array_info(a)
b = a.reshape((2, -1, 3))

array_info(b)<jupyter_output>ndim: 3
shape: (2, 5, 3)
size: 30
dtype: int32
values:
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]
  [12 13 14]]

 [[15 16 17]
  [18 19 20]
  [21 22 23]
  [24 25 26]
  [27 28 29]]]<jupyter_text>Changing the Dtype| Numpy type | C type   | Description ||-|-|-|| numpy.int8 | int8_t | Byte (-128 to 127) || numpy.int16 | int16_t |   Integer (-32768 to 32767) || numpy.int32 | int32_t |   Integer (-2147483648 to 2147483647) || numpy.int64 | int64_t |   Integer (-9223372036854775808 to 9223372036854775807) || numpy.uint8 | uint8_t |   Unsigned integer (0 to 255) || numpy.uint16 | uint16_t |   Unsigned integer (0 to 65535) || numpy.uint32 | uint32_t |   Unsigned integer (0 to 4294967295) || numpy.uint64 | uint64_t |   Unsigned integer (0 to 18446744073709551615) || numpy.intp | intptr_t |   Integer used for indexing, typically the same as ssize_t || numpy.uintp | uintptr_t |   Integer large enough to hold a pointer || numpy.float32 | float |  || numpy.float64 | double |   Note that this matches the precision of the builtin python float. || numpy.complex64 | float complex |   Complex number, represented by two 32-bit floats. || numpy.complex128 | double complex |   Note that this matches the precision of the builtin python complex. |<jupyter_code>x = np.float32([-1.0, 2.0, 3.0])

array_info(x)
x = np.array([-1.0, 2.0, 3.0], dtype=np.float32)
y = x.astype(np.int8)

array_info(y)
z = np.uint16(x)

array_info(z)<jupyter_output>ndim: 1
shape: (3,)
size: 3
dtype: uint16
values:
[65535     2     3]<jupyter_text>Concatenation of arrays<jupyter_code>x = np.array([1, 2, 3])
y = np.array([3, 2, 1])

result = np.concatenate([x, y])

array_info(result)
grid = np.array([[1, 2, 3],
                 [4, 5, 6]])

array_info(grid)
result = np.concatenate([grid, grid])

array_info(result)
result = np.concatenate([grid, grid], axis=0)

array_info(result)
result = np.concatenate([grid, grid], axis=1)

array_info(result)
x = np.array([1, 2, 3])
grid = np.array([[4, 5, 6],
                 [7, 8, 9]])

result = np.vstack([x, grid])

array_info(result)
y = np.array([[-1], [-1]])

result = np.hstack([grid, y])

array_info(result)<jupyter_output>ndim: 2
shape: (2, 4)
size: 8
dtype: int32
values:
[[ 4  5  6 -1]
 [ 7  8  9 -1]]
{'fraction_non_alphanumeric': 0.15626602838091982, 'fraction_numerical': 0.1309625577021713, 'mean_word_length': 4.172413793103448, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '16033', 'license': 'MIT', 'n_tokens_mistral': 2862, 'n_tokens_neox': 2434, 'n_words': 635, 'path': '2_Numpy/02_NumpyBasics.ipynb', 'repo_name': 'tkaya94/UdemyDataScience', 'size': '5849'}