From d314d760af6ddcabef554cf07f317a690bff51cb Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 21:46:30 +0530 Subject: [PATCH 01/15] Datatypes.md file is added to the index.md --- contrib/numpy/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index 82596a2..6d02025 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Numpy Datatypes](datatypes.md) \ No newline at end of file From 2e33a9b997189dc81f0dc73abd1f0c8ff8793cf0 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 21:47:17 +0530 Subject: [PATCH 02/15] NumPy datatypes list is added --- contrib/numpy/datatypes.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 contrib/numpy/datatypes.md diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md new file mode 100644 index 0000000..768add1 --- /dev/null +++ b/contrib/numpy/datatypes.md @@ -0,0 +1,35 @@ +# Numpy Data Types +In NumPy, data types play a crcial role in representing and manipulating numerical data. + +Numpy supports the following data types: + +- `i` - integer +- `b` - boolean +- `u` - unsigned integer +- `f` - float +- `c` - complex float +- `m` - timedelta +- `M` - datetime +- `O` - object +- `S` - string +- `U` - unicode string + +## Example for integer type + +## Example for float type + +## Example for boolean type + +## Example for unsigned integer type + +## Example for complex type + +## Example for datetime type + +## Example for string type + +## Example for object type + +## Example for unicode string type + +## Example for timedelta type From 92beeba6c21b6cb27242d91fb6f6bd4ed3b019ba Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 21:54:39 +0530 Subject: [PATCH 03/15] dtype() definition and example is added --- contrib/numpy/datatypes.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 768add1..39dd46b 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -14,6 +14,17 @@ Numpy supports the following data types: - `S` - string - `U` - unicode string +## dtype() Function +The `dtype()` function returns the type of the NumPy array object. +``` python + import numpy as np + + arr = np.array([1, 2, 3, 4]) + + print(arr.dtype) + + # Output: int64 +``` ## Example for integer type ## Example for float type @@ -33,3 +44,6 @@ Numpy supports the following data types: ## Example for unicode string type ## Example for timedelta type + +# Data Type Conversion + From 8c8544540948ec757d5b2cc97df09278fcaf9e61 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:08:40 +0530 Subject: [PATCH 04/15] Example for integer type is added --- contrib/numpy/datatypes.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 39dd46b..c70e810 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -16,17 +16,51 @@ Numpy supports the following data types: ## dtype() Function The `dtype()` function returns the type of the NumPy array object. + +Example 1 ``` python import numpy as np arr = np.array([1, 2, 3, 4]) print(arr.dtype) - + # Output: int64 ``` -## Example for integer type +Example 2 +``` python + import numpy as np + + arr = np.array(['apple', 'banana', 'cherry']) + + print(arr.dtype) + + # Output: Date: Mon, 20 May 2024 22:17:35 +0530 Subject: [PATCH 05/15] Example for float type is added --- contrib/numpy/datatypes.md | 40 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index c70e810..1a355d2 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -46,8 +46,10 @@ Way 1: Using function `int_()` import numpy as np arr = np.int_([2,4,6]) - + # Size: int8, int16, int32, int64 + print(arr.dtype()) + # Output: int64 ``` @@ -56,13 +58,47 @@ Way 2: Using `dtype()` import numpy as np arr = np.array([2,4,6], dtype='i4') + # Size: i1, i2, i4, i8 print(arr.dtype) - # Output: int8 + # Output: int32 ``` + ## Example for float type +Way 1: Using function `float_()` +``` python + import numpy as np + + arr = np.float_(1) + # Size: float8, float16, float32, float64 + + print(arr) + print(arr.dtype()) + + # Output: + # 1.0 + # float64 +``` + +Way 2: Using `dtype()` +``` python + import numpy as np + + arr = np.array([2,4,6], dtype='f4') + # Size: f1, f2, f4, f8 + + print(arr) + print(arr.dtype) + + # Output: + # [1. 2. 3. 4.] + # float32 +``` + +Note: `np.single()` has the same function as `float32()`. + ## Example for boolean type ## Example for unsigned integer type From c64b262eda0bc8a2e74fbb63f9ba9df5423a91a3 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:28:30 +0530 Subject: [PATCH 06/15] Example for unsigned integer type is added --- contrib/numpy/datatypes.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 1a355d2..ea9430b 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -65,6 +65,7 @@ Way 2: Using `dtype()` # Output: int32 ``` +Note: `np.intc()` has the same function as `int32()`. ## Example for float type Way 1: Using function `float_()` @@ -101,8 +102,32 @@ Note: `np.single()` has the same function as `float32()`. ## Example for boolean type +``` python + import numpy as np + + x = np.bool_(1) + + print(x) + print(x.dtype) + + # Output: + # True + # bool +``` ## Example for unsigned integer type +``` python + import numpy as np + + x = np.uintc(1) + + print(x) + print(x.dtype) + + # Output: + # 1 + # uint32 +``` ## Example for complex type ## Example for datetime type From 092c550b67fb80bcd63cb813babe4d7bb4471547 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:32:10 +0530 Subject: [PATCH 07/15] Example for complex type is added --- contrib/numpy/datatypes.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index ea9430b..8bf52ad 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -128,7 +128,22 @@ Note: `np.single()` has the same function as `float32()`. # 1 # uint32 ``` + ## Example for complex type +Complex type is a combination of real number + imaginary number. The `complex_()` is used to define the complex type NumPy object. +``` python + import numpy as np + + x = np.complex_(1) + # Size: complex64, complex128 + + print(x) + print(x.dtype) + + # Output: + # (1+0j) + # complex128 +``` ## Example for datetime type From 13c485e7dee1b7563c5c8b4e3f5e17343066d4f7 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:39:24 +0530 Subject: [PATCH 08/15] Example for datetime type is added --- contrib/numpy/datatypes.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 8bf52ad..bf18837 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -136,7 +136,7 @@ Complex type is a combination of real number + imaginary number. The `complex_() x = np.complex_(1) # Size: complex64, complex128 - + print(x) print(x.dtype) @@ -146,6 +146,24 @@ Complex type is a combination of real number + imaginary number. The `complex_() ``` ## Example for datetime type +The `datetime64()` is used to define the date, month and year. + +``` python + import numpy as np + + x = np.datetime64('1970-05') + y = np.datetime64('2024-20-05') + z = np.datetime64('2024') + + print(x,x.dtype) + print(y,y.dtype) + print(z,z.dtype) + + # Output: + # 1970-05 datetime64[M] + # 1970-01-11 datetime64[D] + # 1970 datetime64[Y] +``` ## Example for string type From 722d34de63e7ef058cd3275fdefcbc726dc60f35 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:46:00 +0530 Subject: [PATCH 09/15] Example for timedelta type is added --- contrib/numpy/datatypes.md | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index bf18837..616d6c3 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -151,8 +151,8 @@ The `datetime64()` is used to define the date, month and year. ``` python import numpy as np - x = np.datetime64('1970-05') - y = np.datetime64('2024-20-05') + x = np.datetime64('2024-05') + y = np.datetime64('2024-05-20') z = np.datetime64('2024') print(x,x.dtype) @@ -160,9 +160,9 @@ The `datetime64()` is used to define the date, month and year. print(z,z.dtype) # Output: - # 1970-05 datetime64[M] - # 1970-01-11 datetime64[D] - # 1970 datetime64[Y] + # 2024-05 datetime64[M] + # 2024-20-05 datetime64[D] + # 2024 datetime64[Y] ``` ## Example for string type @@ -172,6 +172,21 @@ The `datetime64()` is used to define the date, month and year. ## Example for unicode string type ## Example for timedelta type +The `timedelta64()` used to find the difference between the `datetime64()`. The arguments for timedelta64 are a number, to represent the number of units, and a date/time unit, such as (D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds. The timedelta64 data type also accepts the string “NAT” in place of the number for a “Not A Time” value. +``` python + import numpy as np + + x = np.datetime64('2024-05-20') + y = np.datetime64('2023-05-20') + res = x - y + + print(res) + print(res.dtype) + + # Output: + # 366 days + # timedelta64[D] +``` # Data Type Conversion From 26a0dc814d76da7c6fd9be4ed40d22ea1c0e04fc Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:48:00 +0530 Subject: [PATCH 10/15] Example for object type is added --- contrib/numpy/datatypes.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 616d6c3..6107d8d 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -168,7 +168,18 @@ The `datetime64()` is used to define the date, month and year. ## Example for string type ## Example for object type +``` python + import numpy as np + arr = np.object_([1, 2, 3, 4]) + + print(arr) + print(arr.dtype) + + # Output: + # [1, 2, 3, 4] + # object +``` ## Example for unicode string type ## Example for timedelta type From cf4a924c0eedfa9672377d967c6ea93efd25b146 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 22:56:17 +0530 Subject: [PATCH 11/15] Example for String type is added --- contrib/numpy/datatypes.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 6107d8d..d365623 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -166,6 +166,15 @@ The `datetime64()` is used to define the date, month and year. ``` ## Example for string type +``` python + import numpy as np + + arr = np.str_("roopa") + + print(arr.dtype) + + # Output: Date: Mon, 20 May 2024 23:03:59 +0530 Subject: [PATCH 12/15] Example type conversion is added --- contrib/numpy/datatypes.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index d365623..12690b9 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -216,4 +216,35 @@ The `timedelta64()` used to find the difference between the `datetime64()`. The # 366 days # timedelta64[D] ``` -# Data Type Conversion \ No newline at end of file +# Data Type Conversion +`astype()` function is used to the NumPy object from one type to another type. + +It creates a copy of the array and allows to specify the data type of our choice. + +## Example 1 + +``` python + import numpy as np + + x = np.array([1.2, 3.4, 5.6]) + y = x.astype(int) + + print(y,y.dtype) + + # Output: + # [1 3 5] int64 +``` + +## Example 2 + +``` python + import numpy as np + + x = np.array([1, 3, 0]) + y = x.astype(bool) + + print(y,y.dtype) + + # Output: + # [True True False] bool +``` \ No newline at end of file From 5effa72de84fae6d7ee5d4cbba07d0f7a4d2f8e6 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 23:08:12 +0530 Subject: [PATCH 13/15] index.md is modified --- contrib/numpy/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index 6d02025..fa6ae9e 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -1,4 +1,4 @@ # List of sections -- [Section title](filename.md) -- [Numpy Datatypes](datatypes.md) \ No newline at end of file +- [NumPy Data Types](datatypes.md) +- [Section title](filename.md) \ No newline at end of file From bfdd5dbc41e60d3193991d3687456515f1c17b39 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 23:14:25 +0530 Subject: [PATCH 14/15] Additional datatype longdouble is added --- contrib/numpy/datatypes.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index 12690b9..f58cee5 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -216,6 +216,20 @@ The `timedelta64()` used to find the difference between the `datetime64()`. The # 366 days # timedelta64[D] ``` +## Additional Data Type (`longdouble`) +`longdouble` is a data type that provides higher precision than the standard double-precision floating-point (`float64`) type. + +``` python + import numpy as np + + arr = np.longdouble([1.222222, 4.44, 45.55]) + + print(arr, arr.dtype) + + # Output: + # [1.222222 4.44 45.55] float128 +``` + # Data Type Conversion `astype()` function is used to the NumPy object from one type to another type. From 4e36263a1cf3cf117f51af3643c1e75e2617e329 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Mon, 20 May 2024 23:23:29 +0530 Subject: [PATCH 15/15] datatypes.md modified --- contrib/numpy/datatypes.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contrib/numpy/datatypes.md b/contrib/numpy/datatypes.md index f58cee5..fb9df47 100644 --- a/contrib/numpy/datatypes.md +++ b/contrib/numpy/datatypes.md @@ -14,6 +14,9 @@ Numpy supports the following data types: - `S` - string - `U` - unicode string + +_Referred from: W3schools_ + ## dtype() Function The `dtype()` function returns the type of the NumPy array object. @@ -261,4 +264,4 @@ It creates a copy of the array and allows to specify the data type of our choice # Output: # [True True False] bool -``` \ No newline at end of file +```