iT邦幫忙

0

每日一篇學習筆記 直到我做完專題 :( [Day40]

  • 分享至 

  • xImage
  •  

昨天所做的模型沒辦法成功預測
今天嘗試把更多特徵加進來
也讓原本兩個判斷改成3個來訓練

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder


data = {
     '重量': [150, 130, 140, 155, 160, 180, 170, 175, 165, 185, 120, 110, 115, 125, 130],
    '顏色': ['紅色', '紅色', '紅色', '紅色', '紅色',
            '橙色', '橙色', '橙色', '橙色', '橙色',
            '黃色', '黃色', '黃色', '黃色', '黃色'],
    '品項': ['蘋果', '蘋果', '蘋果', '蘋果', '蘋果',
            '橘子', '橘子', '橘子', '橘子', '橘子',
            '香蕉', '香蕉', '香蕉', '香蕉', '香蕉'],
    '形狀': ['圓形', '圓形', '圓形', '圓形', '圓形',
            '圓形', '圓形', '圓形', '圓形', '圓形',
            '條狀', '條狀', '條狀', '條狀', '條狀'],
    '光滑度': ['光滑', '光滑', '光滑', '光滑', '光滑',
             '粗糙', '粗糙', '粗糙', '粗糙', '粗糙',
             '光滑', '光滑', '光滑', '光滑', '光滑']
}

df = pd.DataFrame(data)
encoder = LabelEncoder()

df['data_color'] = encoder.fit_transform(data['顏色'])
df['data_item'] = encoder.fit_transform(data['品項'])
df['data_shape'] = encoder.fit_transform(data['形狀'])
df['data_smooth'] = encoder.fit_transform(data['光滑度'])
x = df[['重量','data_color','data_shape','data_smooth']]
y = df[['品項']]
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)

model = LogisticRegression()

model.fit(x_train,y_train)

y_pred = model.predict(x_test)

print("測試集的實際標籤:")
print(y_test.values)
print("模型的預測結果")
print(y_pred)

accuracy = accuracy_score(y_test,y_pred)
print(f"模型的準確率:{accuracy * 100:.2f}")

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言