-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
208 lines (180 loc) · 6.96 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import joblib
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
import seaborn as sns
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Define crop dictionary at module level
CROP_DICT = {
'rice': 1, 'maize': 2, 'chickpea': 3, 'kidneybeans': 4,
'pigeonpeas': 5, 'mothbeans': 6, 'mungbean': 7, 'blackgram': 8,
'lentil': 9, 'pomegranate': 10, 'banana': 11, 'mango': 12,
'grapes': 13, 'watermelon': 14, 'muskmelon': 15, 'apple': 16,
'orange': 17, 'papaya': 18, 'coconut': 19, 'cotton': 20,
'jute': 21, 'coffee': 22
}
def load_and_preprocess_data():
try:
data = pd.read_csv('crop_recommendation.csv')
data['label'] = data['label'].map(CROP_DICT)
X = data.drop('label', axis=1)
y = data['label']
return X, y
except Exception as e:
logging.error(f"Error loading or preprocessing data: {e}")
raise
def evaluate_classifiers(X_train, X_test, y_train, y_test):
# Define classifiers
classifiers = {
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
'SVM': SVC(random_state=42),
'KNN': KNeighborsClassifier(n_neighbors=5),
'Decision Tree': DecisionTreeClassifier(random_state=42),
'Naive Bayes': GaussianNB()
}
# Evaluate each classifier
results = {}
for name, clf in classifiers.items():
# Train and predict
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# Store results
results[name] = {
'accuracy': accuracy_score(y_test, y_pred),
'cv_scores': cross_val_score(clf, X_train, y_train, cv=5),
'report': classification_report(y_test, y_pred),
'confusion_matrix': confusion_matrix(y_test, y_pred),
'model': clf
}
return results
def plot_classifier_comparison(results):
accuracies = [v['accuracy'] for v in results.values()]
names = list(results.keys())
plt.figure(figsize=(10, 6))
plt.bar(names, accuracies)
plt.title('Classifier Comparison')
plt.xticks(rotation=45)
plt.ylabel('Accuracy')
plt.tight_layout()
plt.savefig('classifier_comparison.png')
def plot_confusion_matrices(results, y_test):
for name, metrics in results.items():
plt.figure(figsize=(8, 6))
sns.heatmap(metrics['confusion_matrix'], annot=True, fmt='d', cmap='Blues')
plt.title(f'Confusion Matrix for {name}')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.tight_layout()
plt.savefig(f'confusion_matrix_{name}.png')
def plot_cv_scores(results):
plt.figure(figsize=(10, 6))
for name, metrics in results.items():
plt.plot(metrics['cv_scores'], label=name)
plt.title('Cross-Validation Scores')
plt.xlabel('Fold')
plt.ylabel('Accuracy')
plt.legend()
plt.tight_layout()
plt.savefig('cv_scores.png')
def plot_feature_importance(results, X):
best_clf = max(results.items(), key=lambda x: x[1]['accuracy'])[1]['model']
if hasattr(best_clf, 'feature_importances_'):
importances = best_clf.feature_importances_
indices = np.argsort(importances)[::-1]
plt.figure(figsize=(10, 6))
plt.title('Feature Importances')
plt.bar(range(X.shape[1]), importances[indices], align='center')
plt.xticks(range(X.shape[1]), X.columns[indices], rotation=90)
plt.tight_layout()
plt.savefig('feature_importances.png')
def plot_pairplot(X, y):
df = X.copy()
df['label'] = y
sns.pairplot(df, hue='label', diag_kind='kde')
plt.tight_layout()
plt.savefig('pairplot.png')
def plot_correlation_heatmap(X):
plt.figure(figsize=(12, 8))
sns.heatmap(X.corr(), annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Correlation Heatmap')
plt.tight_layout()
plt.savefig('correlation_heatmap.png')
def train_and_evaluate(test_size=0.2, random_state=42):
# Load and scale data
X, y = load_and_preprocess_data()
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=test_size, random_state=random_state
)
# Evaluate classifiers
results = evaluate_classifiers(X_train, X_test, y_train, y_test)
# Plot results
plot_classifier_comparison(results)
plot_confusion_matrices(results, y_test)
plot_cv_scores(results)
plot_feature_importance(results, X)
plot_pairplot(pd.DataFrame(X_scaled, columns=X.columns), y)
plot_correlation_heatmap(pd.DataFrame(X_scaled, columns=X.columns))
# Get best model
best_clf = max(results.items(), key=lambda x: x[1]['accuracy'])
# Save best model
joblib.dump(best_clf[1]['model'], 'best_crop_model.pkl')
joblib.dump(scaler, 'scaler.pkl')
return results
def test_crop_prediction(temperature, humidity, rainfall, N, P, K, ph):
# Load model and scaler
model = joblib.load('best_crop_model.pkl')
scaler = joblib.load('scaler.pkl')
# Prepare input data
input_data = pd.DataFrame([{
'N': N,
'P': P,
'K': K,
'temperature': temperature,
'humidity': humidity,
'ph': ph,
'rainfall': rainfall
}])
# Scale input
input_scaled = scaler.transform(input_data)
# Get predictions
predictions = model.predict(input_scaled)
# Map predictions to crop names
crop_dict = {
1: 'rice', 2: 'maize', 3: 'chickpea', 4: 'kidneybeans',
5: 'pigeonpeas', 6: 'mothbeans', 7: 'mungbean', 8: 'blackgram',
9: 'lentil', 10: 'pomegranate', 11: 'banana', 12: 'mango',
13: 'grapes', 14: 'watermelon', 15: 'muskmelon', 16: 'apple',
17: 'orange', 18: 'papaya', 19: 'coconut', 20: 'cotton',
21: 'jute', 22: 'coffee'
}
recommended_crops = [crop_dict.get(pred, "Unknown Crop") for pred in predictions]
return recommended_crops
if __name__ == "__main__":
results = train_and_evaluate()
temperature = 25.0
humidity = 71.0
rainfall = 103.0
N = 90
P = 42
K = 43
ph = 6.5
crop = test_crop_prediction(temperature, humidity, rainfall, N, P, K, ph)
print(f"Recommended crops: {crop}")
for name, metrics in results.items():
print(f"\n{name} Results:")
print(f"Accuracy: {metrics['accuracy']:.3f}")
print(f"CV Scores: {metrics['cv_scores'].mean():.3f} (+/- {metrics['cv_scores'].std() * 2:.3f})")
print(f"Classification Report:\n{metrics['report']}")