<jupyter_start><jupyter_text>Compare Numpy with List<jupyter_code>import numpy as np
my_array = np.arange(1000000)
my_list = list(range(1000000))
%time for _ in range(50): my_array * 2
%time for _ in range(50): my_list * 2<jupyter_output>Wall time: 6.31 s<jupyter_text>4.1 The NumPy ndarray: A Multidimensional Array Object<jupyter_code>#(rows, columns)
x = np.random.randn(2, 3)
#(rows, columns)
y = np.random.randn(8, 4)
x
y
x * 10
y * 10
x.dtype
x.shape<jupyter_output><empty_output><jupyter_text>Creating ndarray<jupyter_code>xarr = np.array([[1, 2, 3, 4, 5], [11, 22, 33, 44, 55]])
xarr.dtype
xarr.ndim
xarrr = np.array([[[1, 2, 3, 4, 5], [11, 22, 33, 44, 55]]])
xarrr.ndim
xarrr
xarr.shape
xarrr.shape
np.zeros(10)
np.zeros((2, 3))
np.empty((2, 3))
np.zeros((1, 2, 3))
np.zeros((2, 2, 3))
np.zeros((3, 2, 3))
list1 = [1, 2, 3, 4, 5]
np.asarray(list1)
abc1 = np.array([1, 2, 3])
abc1.dtype
abc2 = abc1.astype(np.float64)
abc1.dtype
abc2.dtype
list1
np.array(list1)
list1
np.ones((2, 3))
mn = np.ones((2, 3), dtype="float32")
mn
np.ones_like(mn, dtype="int64")
## alternate zeros() and zeros_like()
x = np.empty((2, 3))
x
gf = np.empty_like(x)
gf
c = np.full((2, 3), fill_value='3', dtype="int32")
c
np.full_like(c, fill_value=23)
np.eye(5, 5)
np.identity(5)<jupyter_output><empty_output><jupyter_text>Data Types for ndarrays<jupyter_code>arr1 = np.array([1, 2, 3], dtype=np.float64)
arr2 = np.array([1, 2, 3], dtype=np.int32)
arr1.dtype
arr2.dtype
# float32 is float
# int32 is integer
# float64 is Double<jupyter_output><empty_output><jupyter_text>Numpy more Data Types<jupyter_code>int8_array1 = np.array([1, 2, 3], dtype=np.int8)
int8_array2 = np.array([1, 2, 3], dtype=np.uint8)
print(int8_array1.dtype)
int8_array2.dtype
int16_a = np.array([1, 2, 3], dtype=np.int16)
int32_a = np.array([1, 2, 3], dtype=np.int32)
int64_a = np.array([1, 2, 3], dtype=np.int64)
float16_a = np.array([1, 2, 3], dtype=np.float16)
float32_a = np.array([1, 2, 3], dtype=np.float32)
float64_a = np.array([1, 2, 3], dtype=np.float64)
#float128_a = np.array([1, 2, 3], dtype=np.float128)
c64 = np.array([1, 2, 3], dtype=np.complex64)
c64
c64 = np.array([[1, 2, 3], [11, 22, 33]], dtype=np.complex64)
c64
# complex128 and complex256
bol = np.array([1, 2, 3], dtype=np.bool)
bol
list1 = list([1, 2, 3])
list2 = list([4, 5, 6])
obj = np.array([list1, list2], dtype=np.object)
obj
str1 = np.array([1.2, 3.3, 6.6, 1.0, 3.4, 1.2], dtype=np.string_)
str1
str1.astype(np.float32)
str2 = np.array(['aa','b', 'cd', 'd', 'efg', 'shk', 'ijkl'], dtype=np.string_)
str2
uni1 = np.array([1.2, 3.4, 0.1, 45.9], dtype=np.unicode_)
uni1
uni2 = np.array(['abc', 'shk', 'ger', 'arg', 'shakeel'], dtype=np.unicode_)
uni2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20383339476594176, 'fraction_numerical': 0.10357537781054184, 'mean_word_length': 5.416075650118203, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '384624', 'license': 'Unlicense', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1265, 'n_words': 285, 'path': 'Book Practice/1 - ndArray and Data Types.ipynb', 'repo_name': 'shkhaider2015/PIAIC-QUARTER-2', 'size': '2713'}