Skip to content

Commit a8beec5

Browse files
committed
code refactor
1 parent ebbdcbd commit a8beec5

File tree

1 file changed

+8
-3
lines changed
  • ml/predictive_modelling_for_agriculture

1 file changed

+8
-3
lines changed

Diff for: ml/predictive_modelling_for_agriculture/script.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,23 @@
3636
crops['crop'] = crops['crop'].astype('category')
3737
crops['crop_code'] = crops['crop'].cat.codes
3838

39-
Write your code here
40-
crops.isna().sum()
39+
Check NA vals
40+
print(crops.isna().sum())
4141

42-
X_train, X_test, y_train, y_test = train_test_split(crops.drop(['crop', 'crop_code'], axis=1), crops['crop_code'], test_size=.2, random_state=42)
42+
X_train, X_test, y_train, y_test = train_test_split(crops.drop(['crop', 'crop_code'], axis=1),
43+
crops['crop_code'],
44+
test_size=.2,
45+
random_state=42)
4346

47+
# Iterate over each feature to build a model
4448
for feature in ['N', 'P', 'K', 'ph']:
4549
log_reg = LogisticRegression(max_iter=2000, multi_class='multinomial')
4650
log_reg.fit(X_train[[feature]], y_train)
4751
y_pred = log_reg.predict(X_test[[feature]]) # Fixed the error here
4852
feature_performance = f1_score(y_pred, y_test, average='weighted')
4953
print(f"F1-score for {feature}: {feature_performance}")
5054

55+
# Corr heatmap
5156
sns.heatmap(crops.corr(), annot=True)
5257

5358
final_features = ["N", "K", "ph"]

0 commit comments

Comments
 (0)