-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
182 lines (144 loc) · 5.32 KB
/
model.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
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_excel('arhar2.xlsx')
print(df.info())
plt.figure(figsize=(15,20))
sns.countplot(y=df['area'])
country = df['area'].unique()
yield_per_country = []
for state in country:
yield_per_country.append(df[df['area']==state]['yield'].sum())
plt.figure(figsize=(15, 20))
sns.barplot(y=country, x=yield_per_country)
# plt.show()
col = ['state', 'district','season','start', 'end', 'area', 'production']
df = df[col]
df = df.dropna()
X = df.iloc[:, :-1]
y = df.iloc[:, -1]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=0, shuffle=True)
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
numeric_features = [3, 4, 5]
categorical_features = [0, 1, 2]
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(drop='first', handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)
])
clf = Pipeline(steps=[('preprocessor', preprocessor)])
clf.fit(X_train)
X_train_dummy = clf.transform(X_train)
X_test_dummy = clf.transform(X_test)
#linear regression
from sklearn.linear_model import LinearRegression,Lasso,Ridge
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor,AdaBoostRegressor, GradientBoostingRegressor, ExtraTreesRegressor,RandomForestRegressor
from sklearn.metrics import mean_absolute_error,r2_score
from xgboost import XGBRegressor
models = {
'knn':KNeighborsRegressor(),
'etr':ExtraTreesRegressor(),
'br':BaggingRegressor(),
'ar':AdaBoostRegressor(),
'xgb':XGBRegressor(),
'lr':LinearRegression(),
'rf':RandomForestRegressor(),
'lss':Lasso(),
'Rid':Ridge(),
'Dtr':DecisionTreeRegressor(),
'gbr':GradientBoostingRegressor()
}
for name, md in models.items():
md.fit(X_train_dummy,y_train)
y_pred = md.predict(X_test_dummy)
print(f"{name} : mae : {mean_absolute_error(y_test,y_pred)} score : {r2_score(y_test,y_pred)}")
#just for testing..
xgb = XGBRegressor()
xgb.fit(X_train_dummy,y_train)
xg_pred=xgb.predict(X_test_dummy)
etr = ExtraTreesRegressor()
etr.fit(X_train_dummy,y_train)
etr_pred=etr.predict(X_test_dummy)
knn = KNeighborsRegressor()
knn.fit(X_train_dummy,y_train)
knn_pred=knn.predict(X_test_dummy)
br = BaggingRegressor()
br.fit(X_train_dummy,y_train)
br_pred=br.predict(X_test_dummy)
ar = AdaBoostRegressor()
ar.fit(X_train_dummy,y_train)
ar_pred=ar.predict(X_test_dummy)
lr = LinearRegression()
lr.fit(X_train_dummy,y_train)
lr_pred=lr.predict(X_test_dummy)
rf = RandomForestRegressor()
rf.fit(X_train_dummy,y_train)
rf_pred=rf.predict(X_test_dummy)
gbr = GradientBoostingRegressor()
gbr.fit(X_train_dummy,y_train)
gbr_pred=gbr.predict(X_test_dummy)
dtr = DecisionTreeRegressor()
dtr.fit(X_train_dummy,y_train)
dtr_pred=dtr.predict(X_test_dummy)
def prediction(state,district,season,start,end,area):
# Create an array of the input features
features = np.array([[state,district,season,start,end,area]], dtype=object)
# Transform the features using the preprocessor
transformed_features = preprocessor.transform(features)
# Make the prediction
predicted_yield = xgb.predict(transformed_features).reshape(1, -1)
return predicted_yield[0]
state= 'Bihar'
district='GAYA'
season='Kharif'
start=2024
end =2025
area = 18
result = prediction(state,district,season,start,end,area)
print(result)
import joblib
joblib.dump(preprocessor,'./static/pre.joblib')
joblib.dump(knn,'./static/knn.joblib')
joblib.dump(etr,'./static/etr.joblib')
joblib.dump(br,'./static/br.joblib')
joblib.dump(ar,'./static/ar.joblib')
joblib.dump(xgb,'./static/xgb.joblib')
joblib.dump(lr,'./static/lr.joblib')
joblib.dump(rf,'./static/rf.joblib')
joblib.dump(gbr,'./static/gbr.joblib')
joblib.dump(dtr,'./static/dtr.joblib')
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
def create_neural_network_model(input_shape):
model = Sequential([
Dense(64, activation='relu', input_shape=input_shape),
Dense(32, activation='relu'),
Dense(1) # Output layer (single neuron for regression)
])
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mean_absolute_error'])
return model
# Instantiate the neural network model
neural_network_model = create_neural_network_model(X_train_dummy.shape[1])
# Train the model
neural_network_model.fit(X_train_dummy, y_train, epochs=50, batch_size=32, validation_split=0.2)
# Predict using the neural network model
neural_network_pred = neural_network_model.predict(X_test_dummy)
# Evaluate the neural network model
neural_network_mae = mean_absolute_error(y_test, neural_network_pred)
neural_network_score = r2_score(y_test, neural_network_pred)
print(f"Neural Network: MAE: {neural_network_mae}, R2 Score: {neural_network_score}")