今天來自己重新做一個簡單模型 雖然數據有點太少:l
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
data = {
'重量' : [150,130,180,160],
'顏色' : ['red','red','orange','orange'],
'品項' : ['蘋果','蘋果','橘子','橘子']
}
df = pd.DataFrame(data)
df['data_color'] = pd.Series(data['顏色']).map({'red':0,'orange':1})
df['data_item'] = pd.Series(data['品項']).map({'蘋果':0,'橘子':1})
x = df[['重量','data_color']]
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}")
雖然測試集只有一個 還直接預測錯
那之後可以再改進