【问题标题】:Machine learning model keeps on giving the same result even with different outputs即使输出不同,机器学习模型也会继续给出相同的结果
【发布时间】: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)

This is the SS of my outputs2

The dataset is given here:

【问题讨论】:

  • 如果您创建模型实例一次,那么对于不同的训练循环,它将给出相同的结果,因为模型的权重将存储在缓存中。您需要做的是在进入训练部分之前运行创建模型实例的单元。这就是修复 jupyter 中的错误的方法。如果您仍然有错误,那我不知道。
  • 它确实有效。非常感谢
  • 问题实际上与jupyter-notebook无关,请不要发送无关标签(已删除)。
  • 这是在 jupyter-notebook 中完成的。所以,我提到了标签。对不起,如果结果无关紧要。

标签: python python-3.x machine-learning


【解决方案1】:
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X_train, y_train = make_classification(n_samples=1000, n_features=5,
                           n_informative=2, n_redundant=0,
                           random_state=0, shuffle=False)
model = RandomForestClassifier(max_depth=2, random_state=0)
model.fit(X_train, y_train)

请同时参考: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html

https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html

【讨论】:

  • 我试过这样做,现在显示的准确度为零。
猜你喜欢
  • 1970-01-01
  • 2013-11-16
  • 2021-01-18
  • 2019-11-04
  • 2020-01-26
  • 2021-09-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多