Module 8 of 10

Top Student (1st Place) හඳුනාගැනීම

අපේ ව්‍යාපෘතියේ උච්චතම අවස්ථාව! Pandas හි බලවත් functions යොදාගෙන, ජයග්‍රාහකයා කවුදැයි සොයා ගනිමු.

පෙර මොඩියුලයේදී, අපි අපගේ පුහුණු වූ Machine Learning model එක භාවිතා කර, එක් එක් ශිෂ්‍යයා සඳහා `FinalScore` එකක් පුරෝකථනය කළෙමු. එම අනාවැකි, `PredictedScore` නමින් නව තීරුවක් ලෙස අපගේ මුල් DataFrame එකට එකතු කර, සැබෑ ලකුණු සමඟ සසඳා බැලුවෙමු. අපගේ model එකේ අනාවැකි, සැබෑ ලකුණු වලට ඉතා ආසන්න බව අපට පෙනී ගියා. දැන්, අපගේ ව්‍යාපෘතියේ මූලිකම අරමුණ ඉටු කිරීමට අවශ්‍ය සියලුම තොරතුරු අප සතුව ඇත: එනම්, අනාවැකි අනුව, පළමු ස්ථානය ලබා ගන්නේ කුමන ශිෂ්‍යයාද යන්න සොයා ගැනීම.

මෙම කාර්යය සඳහා, අපට නැවතත් Pandas library එකේ ඇති ප්‍රබල දත්ත හැසිරවීමේ හැකියාවන් මත විශ්වාසය තැබිය හැකිය. මෙම මොඩියුලයේදී, DataFrame එකකින් ඉහළම අගය සහ එම අගය පිහිටි ස්ථානය (index) සොයා ගන්නේ කෙසේදැයි අපි ඉගෙන ගනිමු.

Find Highest Predicted Score

අපගේ පළමු පියවර වන්නේ, `PredictedScore` තීරුවේ ඇති ඉහළම ලකුණ කුමක්දැයි සොයා ගැනීමයි. Pandas Series එකක (DataFrame එකක තනි තීරුවක්) ඇති උපරිම අගය සොයා ගැනීම සඳහා, ඉතා සෘජු සහ පහසු method එකක් ඇත. ඒ තමයි `.max()` method එක.

# Assuming 'df' is our DataFrame with the 'PredictedScore' column

# Find the highest value in the 'PredictedScore' column
highest_score = df['PredictedScore'].max()

print(f"The highest predicted score is: {highest_score}")

මෙම code එක, `PredictedScore` තීරුවේ ඇති සියලුම අගයන් අතරින් උපරිම අගය සොයාගෙන, එය අපට print කර පෙන්වයි. උදාහරණයක් ලෙස, ප්‍රතිදානය `97.8` වැනි අගයක් විය හැක.

මෙය ප්‍රයෝජනවත් වුවත්, අපට අවශ්‍ය වන්නේ ඉහළම ලකුණ පමණක් නොවේ. අපට දැන ගැනීමට අවශ්‍ය වන්නේ, මෙම ලකුණ ලබාගත්තේ කුමන ශිෂ්‍යයාද? යන්නයි.

.idxmax() Usage in Pandas

ඉහත ගැටළුව විසඳීම සඳහා Pandas සතුව කදිම විසඳුමක් ඇත. ඒ `.idxmax()` method එකයි.

`.idxmax()` යන්නෙහි තේරුම "Index of Maximum value" හෙවත් "උපරිම අගයේ දර්ශකය" යන්නයි. `.max()` මගින් උපරිම අගය ලබා දෙනවා වෙනුවට, `.idxmax()` මගින් අපට ලබා දෙන්නේ, උපරිම අගය පිහිටා ඇති පේළියේ දර්ශකය (index) කුමක්ද යන්නයි.

සරල උපමාවක්: පන්තියේ ළමුන් පේළියට සිටගෙන සිටිනවා යැයි සිතන්න. එක් එක් ළමයාට අංකයක් (index) ඇත (0, 1, 2, ...).

  • `.max()` function එක call කිරීම, "මේ පන්තියේ ඉන්න උසම ළමයාගේ උස කීයද?" කියා අසනවා වැනිය. පිළිතුර: "සෙන්ටිමීටර 150".
  • `.idxmax()` function එක call කිරීම, "මේ පන්තියේ ඉන්න උසම ළමයා, පේළියේ කීවෙනියටද ඉන්නේ?" කියා අසනවා වැනිය. පිළිතුර: "අංක 8".
අපට ශිෂ්‍යයා හඳුනා ගැනීමට අවශ්‍ය නිසා, දෙවන ප්‍රශ්නයට ලැබෙන පිළිතුර වඩාත් ප්‍රයෝජනවත් වේ.

අපගේ DataFrame එකේ index එක, 0 සිට 9 දක්වා දිවෙන සරල සංඛ්‍යා මාලාවකි. අපි `.idxmax()` භාවිතා කර, ඉහළම `PredictedScore` එක ඇති index එක සොයා ගනිමු.

# Find the index of the row with the highest predicted score
top_student_index = df['PredictedScore'].idxmax()

print(f"The index of the top student is: {top_student_index}")

අපගේ දත්ත කට්ටලයට අනුව, `PredictedScore` තීරුවේ ඉහළම අගය ඇත්තේ ශිෂ්‍ය අංක 9 ට අදාළ පේළියේය. නමුත්, DataFrame index එක 0 සිට ආරම්භ වන නිසා, 9 වන ශිෂ්‍යයා සිටින්නේ index අංක 8 හිදීය. එබැවින්, මෙම code එකේ ප්‍රතිදානය `8` වනු ඇත.

Display Student ID + Score

නියමයි! දැන් අප සතුව පළමු ස්ථානය ලබා ගන්නා ශිෂ්‍යයා සිටින පේළියේ index එක ඇත (`top_student_index`). දැන් අපට අවශ්‍ය වන්නේ, එම index එකට අදාළ සම්පූර්ණ පේළියම (එනම්, එම ශිෂ්‍යයාගේ සියලුම විස්තර) DataFrame එකෙන් තෝරාගෙන පෙන්වීමයි.

Pandas DataFrame එකකින් නිශ්චිත පේළියක් (හෝ පේළි කිහිපයක්) එහි index එක අනුව තෝරා ගැනීම සඳහා, අපි `.loc[]` accessor එක භාවිතා කරමු.

# Use the index found by .idxmax() to select the entire row using .loc[]
top_student_info = df.loc[top_student_index]

print("\n--- Top Student Information ---")
print(top_student_info)

මෙම code එක ක්‍රියාත්මක වූ විට, ඔබට ලැබෙන ප්‍රතිදානය, තනි ශිෂ්‍යයෙකුගේ සියලුම විස්තර සහිත Pandas Series එකක් වනු ඇත.

StudentID 9.00
Math 98.00
Science 95.00
English 97.00
FinalScore 97.00
PredictedScore 97.80
Name: 8, dtype: float64

මෙම ප්‍රතිදානයෙන්, අපගේ model එකට අනුව, පළමු ස්ථානය දිනාගන්නේ StudentID 9 බව ඉතා පැහැදිලිය. ඔහුගේ සැබෑ ලකුණ 97 ක් වන අතර, model එක 97.8 ක් ලෙස ඉතා ආසන්න අනාවැකියක් පළ කර ඇත.

සම්පූර්ණ Script එක

මෙම මොඩියුලයේ සියලුම පියවර එකට එකතු කළ විට, අපගේ script එක මෙසේ දිස්වනු ඇත:

# Assume 'df' is our final DataFrame from Module 7,
# which includes the 'PredictedScore' column.

# 1. Find the index of the student with the highest predicted score
top_student_index = df['PredictedScore'].idxmax()

# 2. Use that index to retrieve all information about that student
top_student_info = df.loc[top_student_index]

# 3. Display the results in a user-friendly way
print("----- Analysis Complete: Finding the 1st Place Student -----")
print(f"\nThe highest predicted score was: {top_student_info['PredictedScore']:.2f}")
print(f"This score belongs to Student with ID: {int(top_student_info['StudentID'])}")
print("\nHere is the full information for the top student:")
print("--------------------------------------------------")
print(top_student_info)
print("--------------------------------------------------")