Uploaded by Thamarai Selvi

numpy array

advertisement
NumPy
NumPy:
• NumPy stands for Numerical Python.
• NumPy was created in 2005 by Travis Oliphant. It is an open source
project and you can use it freely.
• It also has functions for working in domain of linear algebra, fourier
transform, matrices and scientific computation.
• NumPy is a Python library used for working with arrays.
The array object in NumPy is called ndarray
Numpy:
• In Python we have lists that serve the purpose of arrays, but they are
slow to process.
• NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
• The array object called ndarray,supports lots of function working
ndarray easy.
• Arrays are frequently used in data science,where speed and
resource are important.
• pip install numpy
• Import numpy
Datatypes in numpy:
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - unicode string
Eg:
import numpy as np
arr = np.array(['apple', 'banana', 'cherry'])
print(arr.dtype)
Output: <U6
Data types in py:
strings - used to represent text data, the text is given under quote marks. e.g. "ABCD“
integer - used to represent integer numbers. e.g. -1, -2, -3
float - used to represent real numbers. e.g. 1.2, 42.42
boolean - used to represent True or False.
complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j
dtype:
i: Integer data type (signed). Examples include int8, int16, int32, and int64.(-,+)
b: Boolean data type. Example: bool.
u: Unsigned integer data type. Examples include uint8, uint16, uint32, and uint64.(0,+)
f: Float data type. Examples include float16, float32, and float64.
c: Complex float data type. Examples include complex64 and complex128.
m: Timedelta data type. Example: timedelta64.
M: Datetime data type. Example: datetime64.
O: Object data type. This is a generic data type that can hold any Python object.
S: String data type (bytes). Example: bytes.
U: Unicode string data type. Example: str.
NumPy datatypes:
1. bool: Boolean type representing True or False.
- Range: True or False
2. int8, uint8: 8-bit integer types (signed and unsigned).
- Range for int8: -128 to 127
- Range for uint8: 0 to 255
3. int16, uint16: 16-bit integer types (signed and unsigned).
- Range for int16: -32768 to 32767
- Range for uint16: 0 to 65535
4. int32, uint32: 32-bit integer types (signed and unsigned).
- Range for int32: -2147483648 to 2147483647
- Range for uint32: 0 to 4294967295
Eg:
import numpy as np
signed_integer = -100
# Adding 2^32 to convert signed to unsigned integer
unsigned_integer = np.uint32(signed_integer + 2**32)
print(unsigned_integer)
print(type(unsigned_integer))
Output:
4294967196
<class 'numpy.uint32'>
Eg:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)
#dtype –returns datatype of an
array.
Output: int64 #default
import numpy as np
arr = np.array([1, 2, 3, 4],
dtype=np.int8)
print(arr.dtype)
Output: int8
5. int64, uint64: 64-bit integer types (signed and unsigned).
- Range for int64: -9223372036854775808 to 9223372036854775807
- Range for uint64: 0 to 18446744073709551615
6. float16: 16-bit floating-point type.
- Range: Approximately -65504 to +65504 with a precision of about 1e-5.
7. float32: 32-bit floating-point type (single precision).
- Range: Approximately -3.4e38 to +3.4e38 with a precision of about 1e-7.
8. float64: 64-bit floating-point type (double precision).
- Range: Approximately -1.8e308 to +1.8e308 with a precision of about 1e-15.
9. complex64: Complex number represented by two 32-bit floats (real and
imaginary parts).
- Range: Real and imaginary parts follow the range of float32.
10. complex128: Complex number represented by two 64-bit floats (real and
imaginary parts).
- Range: Real and imaginary parts follow the range of float64.
11. object: This data type can hold any Python object.
- Range: Depends on the objects being stored.
12. string_: Fixed-length string type.
- Range: Length of the string is fixed.
13. unicode_: Fixed-length unicode type.
- Range: Length of the unicode string is fixed.
14. datetime64: Represents dates and times.
- Range: From the year 1678 to 2262.
15. timedelta64: Represents differences in dates and times.
- Range: Depends on the range of the datetime64 type.
#Letters, symbols, emoji
unicode_str = 'Hello, 你好, नमस्ते ’
# Length of the string
print(len(unicode_str)) # Output: 16
# Accessing individual characters
print(unicode_str[0]) # Output: H
print(unicode_str[7]) # Output: 你print(unicode_str[-1]) # Output: त
Creating array
import numpy as np
# Creating a boolean array
arr = np.array([True, False, True])
print(arr)
Output:
[ True False True]
Timedelta:
import numpy as np
# Create a timedelta object representing a duration of 2 days
duration = np.timedelta64(2, 'D')
# Print the duration
print(duration) # Output: 2 days
# Perform arithmetic operations with timedelta
start_date = np.datetime64('2023-06-29')
end_date = start_date + duration
# Print the end date
print(end_date) # Output: 2023-07-01
•
•
•
•
•
Y-year
M- month
D- days
m-minute
s-second
Syntax for creating Numpy array
import numpy as np
# Syntax for creating a NumPy array from a Python list
arr = np.array([element1, element2, ...])
# creating NumPy array from a Python tuple
arr = np.array((element1, element2, ...))
Syntax for creating Numpy array
# Syntax for creating a NumPy array with specified data type
arr = np.array([element1, element2, ...], dtype=data_type)
# Syntax for creating a NumPy array with specific dimensions
arr = np.array([[element1, element2, ...], [element3, element4, ...]])
Eg:• import numpy as np
# Creating a NumPy array from a Python list
arr1 = np.array([1, 2, 3, 4, 5])
# Creating a NumPy array from a Python tuple
arr2 = np.array((6, 7, 8, 9, 10))
# Creating a NumPy array with specified data type
arr3 = np.array([1, 2, 3, 4, 5], dtype=np.float32)
# Creating a 2D NumPy array
arr4 = np.array([[1, 2, 3], [4, 5, 6]])
Creating array
arr = np.array([1, 2, 3, 4, 5]) #using np. Array function
zeros_arr = np.zeros((3, 3)) # Creates a 3x3 array of zeros
ones_arr = np.ones((2, 4)) # Creates a 2x4 array of ones
empty_arr = np.empty((2, 2)) # Creates a 2x2 array of uninitialized
values
Download