Skip to content

Model Machine Learning for dataset mushroom using scikit learning

Notifications You must be signed in to change notification settings

watcharap0n/model-machine-mushroom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

model-machine-mushroom

Model Machine Learning for dataset mushroom using scikit learning

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from typing import Optional, Any
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.dummy import DummyClassifier

from google.colab import drive
drive.mount('/content/drive', force_remount=True)
path = '/content/drive/MyDrive/agaricus-lepiota.data.csv'

"""
อธิบายถึง features มีอะไรบ้าง เพราะในข้อมูลที่เขาให้มามันเป็นตัวย่อ เราต้องมาเปิด description ของเขาลองดูตารางด้านล่าง


    1. cap-shape:                bell=b,conical=c,convex=x,flat=f,
                                  knobbed=k,sunken=s
     2. cap-surface:              fibrous=f,grooves=g,scaly=y,smooth=s
     3. cap-color:                brown=n,buff=b,cinnamon=c,gray=g,green=r,
                                  pink=p,purple=u,red=e,white=w,yellow=y
     4. bruises?:                 bruises=t,no=f
     5. odor:                     almond=a,anise=l,creosote=c,fishy=y,foul=f,
                                  musty=m,none=n,pungent=p,spicy=s
     6. gill-attachment:          attached=a,descending=d,free=f,notched=n
     7. gill-spacing:             close=c,crowded=w,distant=d
     8. gill-size:                broad=b,narrow=n
     9. gill-color:               black=k,brown=n,buff=b,chocolate=h,gray=g,
                                  green=r,orange=o,pink=p,purple=u,red=e,
                                  white=w,yellow=y
    10. stalk-shape:              enlarging=e,tapering=t
    11. stalk-root:               bulbous=b,club=c,cup=u,equal=e,
                                  rhizomorphs=z,rooted=r,missing=?
    12. stalk-surface-above-ring: fibrous=f,scaly=y,silky=k,smooth=s
    13. stalk-surface-below-ring: fibrous=f,scaly=y,silky=k,smooth=s
    14. stalk-color-above-ring:   brown=n,buff=b,cinnamon=c,gray=g,orange=o,
                                  pink=p,red=e,white=w,yellow=y
    15. stalk-color-below-ring:   brown=n,buff=b,cinnamon=c,gray=g,orange=o,
                                  pink=p,red=e,white=w,yellow=y
    16. veil-type:                partial=p,universal=u
    17. veil-color:               brown=n,orange=o,white=w,yellow=y
    18. ring-number:              none=n,one=o,two=t
    19. ring-type:                cobwebby=c,evanescent=e,flaring=f,large=l,
                                  none=n,pendant=p,sheathing=s,zone=z
    20. spore-print-color:        black=k,brown=n,buff=b,chocolate=h,green=r,
                                  orange=o,purple=u,white=w,yellow=y
    21. population:               abundant=a,clustered=c,numerous=n,
                                  scattered=s,several=v,solitary=y
    22. habitat:                  grasses=g,leaves=l,meadows=m,paths=p,
                                  urban=u,waste=w,woods=d
                                 
"""

  dft = pd.read_csv(path)
  dft.head() # จะเห็นได้ว่าข้างล่างคือ features เกือบทั้งหมดยกเว้น จะมี target แค่ตัวเดียวคือ class ในข้อมูลจะใช้ตัวย่อ ลองเลื่อนไปอ่านข้างบน หรือไม่งั้นดาวโหลดไฟล์มาจากลิงค์ด้านบน
  
  sns.set_style('darkgrid')
  plt.figure()
  fig1 = sns.countplot(dft['class'], alpha=1, palette= ['red','green'])
  plt.title('Edible vs Poisonous')
  plt.ylabel('# Amount')
  plt.xlabel('Class')
  fig1.set(xticklabels=['Poisonous', 'Edible'])
  plt.show()
  
  _class = dft['class'] # แยกคลาสออกจาก features
  df = dft.drop('class', axis=1) # เอาคอลัมน์ออก เหลือแต่ class
  df = df.replace(np.nan, regex=True) # นำข้อมูล value ที่ว่างออกเพื่อให้ชัวต่อการนำไปใช้งาน
  
  
  def feature_plot(col: Optional[str] = None) -> Any:
    """
    count feature
    :param
        - col
    """
    sns.set_style('white')
    plt.figure()
    sns.countplot(df[col], alpha=1)
    plt.ylabel('# Amount')
    plt.title(f'Mushroom {col}')
    plt.xlabel(f'Type of {col}')
    plt.show()

  feature_plot('cap-shape') # รูปทรงหมวด
  feature_plot('cap-color') # สีหมวก
  feature_plot('odor') # กลิ่น
  feature_plot('gill-color') # สีเหงือก อยู่ใต้หมวก
  feature_plot('habitat') # ที่อยู่อาศัย
  feature_plot('stalk-root') # ก้านราก
  feature_plot('spore-print-color') # สีพิมพ์สปอร์
  feature_plot('cap-surface') # พื้นผิว
  

ส่วนหนึ่งของโค้ดสามารถเข้าไปดูได้ใน source code ที่มีด้านบน