【发布时间】:2021-01-16 18:14:36
【问题描述】:
my_dict = {'Stype': {'black': 1, 'clayey': 2, 'loamy': 3, 'red': 4, 'sandy': 5},
'Ph': {'extremely acidic': 1, 'moderately acidic': 2, 'moderately alkaline': 3, 'neutral': 4, 'slightly acidic': 5, 'slightly alkaline': 6, 'strongly acidic': 7, 'strongly alkaline': 8, 'very strongly acidic': 9, 'very strongly alkaline': 10},
}
labels = cropname['Stype'].astype('category').cat.categories.tolist()
replace1 = {'Stype': {k: v for k,v in zip(labels, list(range(1,len(labels) + 1)))}}
print(replace1)
labels1 = cropname['Ph'].astype('category').cat.categories.tolist()
replace2 = {'Ph': {k: v for k,v in zip(labels1, list(range(1,len(labels1) + 1)))}}
print(replace2)
cropname_replace = cropname.copy()
cropname_replace.replace(replace1, inplace=True)
cropname_replace.replace(replace2, inplace=True)
print(cropname_replace.head())
我从上面的程序得到的输出是:
Temparature Humidity Moisture Stype suitable-crop Ph
0 26 52 38 5 Maize 5
1 32 62 34 4 Ground Nuts 4
2 29 52 45 3 Sugarcane 6
3 34 65 62 1 Cotton 2
4 26 14 35 5 Barley 4
然后我用随机森林模型拟合我的模型
y = cropname_replace['suitable-crop']
X = cropname_replace.drop(columns=['suitable-crop'])
from sklearn.model_selection import train_test_split
X_train, X_test, y_tain, y_test = train_test_split(X, y, test_size=0.2)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model = model.fit(X_train,y_train)
predictions = model.predict(X_test)
该模型还给出了 0.9 的准确度值。但是当我传递它的输入时,它会继续给出结果“大麦”。
pd.to_pickle(model,r'Desktop')
model = pd.read_pickle(r'Desktop')
Soiltype = input('Enter the soil type:').lower()
pH = input('Enter the pH type:').lower()
Temperature = input('Enter temperature:')
Humidity = input('Eneter Humidity:')
Moisture = input('Enter moisture:')
Stype1 = (my_dict['Stype'][Soiltype])
pH1 = (my_dict['Ph'][pH])
result = model.predict([[Stype1, pH1, Temperature, Humidity, Moisture]])
print(result)
【问题讨论】:
-
如果您创建模型实例一次,那么对于不同的训练循环,它将给出相同的结果,因为模型的权重将存储在缓存中。您需要做的是在进入训练部分之前运行创建模型实例的单元。这就是修复 jupyter 中的错误的方法。如果您仍然有错误,那我不知道。
-
它确实有效。非常感谢
-
问题实际上与
jupyter-notebook无关,请不要发送无关标签(已删除)。 -
这是在 jupyter-notebook 中完成的。所以,我提到了标签。对不起,如果结果无关紧要。
标签: python python-3.x machine-learning