numpy.result_type() মেথড নামপাই টাইপ প্রোমোশন (type promotion) নিয়ম প্রয়োগ করে আর্গুমেন্টগুলোর ফলাফল টাইপ ফেরত দেয়। নামপাই-তে টাইপ প্রোমোশন C++-এর নিয়মের মতো কাজ করে, কিছু পার্থক্যের সাথে। স্কেলার এবং অ্যারে উভয় থাকলে অ্যারের টাইপ প্রাধান্য পায় এবং স্কেলারের আসল মান বিবেচনা করা হয়।
প্যারামিটার
- operands: যেসব অপারেশনের অপারেন্ড, তাদের ফলাফল টাইপ জানতে হবে
পদ্ধতি
লাইব্রেরি ইম্পোর্ট করুন
import numpy as np
result_type() মেথড দিয়ে টাইপ প্রোমোশন যাচাই করুন
print("Numpy-এ result_type() মেথড ব্যবহার\n")
print("ফলাফল...", np.result_type(2, np.arange(4, dtype='i1')))
print("ফলাফল...", np.result_type(5, 8))
print("ফলাফল...", np.result_type('i4', 'c8'))
print("ফলাফল...", np.result_type(3.8, 8))
print("ফলাফল...", np.result_type(5, 20.7))
print("ফলাফল...", np.result_type(-8, 20.7))
print("ফলাফল...", np.result_type(10.0, -4))
সম্পূর্ণ উদাহরণ
import numpy as np
print("Numpy-এ result_type() মেথড ব্যবহার\n")
print("ফলাফল...", np.result_type(2, np.arange(4, dtype='i1')))
print("ফলাফল...", np.result_type(5, 8))
print("ফলাফল...", np.result_type('i4', 'c8'))
print("ফলাফল...", np.result_type(3.8, 8))
print("ফলাফল...", np.result_type(5, 20.7))
print("ফলাফল...", np.result_type(-8, 20.7))
print("ফলাফল...", np.result_type(10.0, -4))
আউটপুট
Numpy-এ result_type() মেথড ব্যবহার
ফলাফল... int8
ফলাফল... int64
ফলাফল... complex128
ফলাফল... float64
ফলাফল... float64
ফলাফল... float64
ফলাফল... float64
ব্যাখ্যা:
2 (int)+int8 array→ অ্যারে টাইপ প্রাধান্য পায় →int85, 8(পাইথন int) → ডিফল্টint64'i4' (int32)+'c8' (complex64)→complex1283.8 (float)+8 (int)→float645 (int)+20.7 (float)→float64-8 (int)+20.7 (float)→float6410.0 (float)+-4 (int)→float64