fork download
  1. from sklearn.datasets import load_breast_cancer, load_iris
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.preprocessing import StandardScaler
  4.  
  5. from sklearn.linear_model import LinearRegression, LogisticRegression
  6. from sklearn.tree import DecisionTreeClassifier
  7. from sklearn.naive_bayes import GaussianNB
  8. from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
  9. from sklearn.svm import SVC
  10. #from xgboost import XGBClassifier
  11.  
  12.  
  13. # ======================================================
  14. # STEP 1: Dataset Load
  15. # ======================================================
  16. # Apnar nijer CSV thakle eikhane load_breast_cancer() er bodole
  17. # pd.read_csv("file.csv") diye X, y banaben
  18.  
  19. X, y = load_breast_cancer(return_X_y=True)
  20. # X, y = load_iris(return_X_y=True) # <-- onno dataset chaile eta use korun
  21.  
  22.  
  23. # ======================================================
  24. # STEP 2: Train-Test Split
  25. # ======================================================
  26. X_train, X_test, y_train, y_test = train_test_split(
  27. X, y, test_size=0.2, random_state=42
  28. )
  29.  
  30.  
  31. # ======================================================
  32. # STEP 3: Feature Scaling
  33. # ======================================================
  34. scaler = StandardScaler()
  35. X_train_scaled = scaler.fit_transform(X_train)
  36. X_test_scaled = scaler.transform(X_test)
  37.  
  38.  
  39. # ======================================================
  40. # STEP 4: Model Select (jeita use korben sheita active rakhun, baki comment)
  41. # ======================================================
  42.  
  43. clf = RandomForestClassifier(n_estimators=100, random_state=42)
  44.  
  45. # clf = LinearRegression()
  46. # clf = LogisticRegression(max_iter=1000)
  47. # clf = DecisionTreeClassifier(random_state=42)
  48. # clf = GaussianNB()
  49. # clf = SVC(kernel="rbf", probability=True, random_state=42)
  50. # clf = GradientBoostingClassifier(random_state=42)
  51. # clf = XGBClassifier(eval_metric="logloss", random_state=42)
  52.  
  53.  
  54. # ======================================================
  55. # STEP 5: Train (fit) Model
  56. # ======================================================
  57. clf.fit(X_train_scaled, y_train)
  58.  
  59.  
  60. # ======================================================
  61. # STEP 6: Evaluation (score)
  62. # ======================================================
  63. print("Algorithm Used:", type(clf).__name__)
  64. print("Test Accuracy:", clf.score(X_test_scaled, y_test))
  65.  
  66.  
  67. # ======================================================
  68. # STEP 7: Single Instance Prediction (jeita video te dekhano hoise)
  69. # ======================================================
  70. single_instance = X_test_scaled[1]
  71. print("\nSingle instance:\n", single_instance)
  72.  
  73. prediction = clf.predict([single_instance])
  74. print("\nPredicted label:", prediction)
  75. print("Actual label:", y_test[1])
  76.  
  77. if hasattr(clf, "predict_proba"):
  78. print("Predicted probabilities:", clf.predict_proba([single_instance]))
  79.  
Success #stdin #stdout 3.55s 114780KB
stdin
Standard input is empty
stdout
Algorithm Used: RandomForestClassifier
Test Accuracy: 0.9649122807017544

Single instance:
 [ 1.36536344  0.49866473  1.30551088  1.34147086 -0.40653902 -0.0137241
  0.24063659  0.82144876 -0.83398079 -1.13121527  1.36745547 -0.74874907
  1.27009847  1.18638199 -0.83350144 -0.49043919 -0.31559     0.28726031
 -0.82243534 -0.76235747  1.79461875  0.17237239  1.76366112  1.7441412
 -0.53051417 -0.12362004 -0.02818105  0.99177862 -0.561211   -1.00838949]

Predicted label: [0]
Actual label: 0
Predicted probabilities: [[1. 0.]]