Create predictive_analytics.py
Browse files- predictive_analytics.py +62 -0
predictive_analytics.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
|
6 |
+
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score, classification_report
|
7 |
+
|
8 |
+
class PredictiveAnalytics:
|
9 |
+
def __init__(self):
|
10 |
+
self.model = None
|
11 |
+
self.scaler = StandardScaler()
|
12 |
+
self.target_column = None
|
13 |
+
|
14 |
+
def predict(self, data):
|
15 |
+
# Identify the target column (assuming it's the last column)
|
16 |
+
self.target_column = data.columns[-1]
|
17 |
+
|
18 |
+
# Prepare the data
|
19 |
+
X = data.drop(columns=[self.target_column])
|
20 |
+
y = data[self.target_column]
|
21 |
+
|
22 |
+
# Split the data
|
23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
24 |
+
|
25 |
+
# Scale the features
|
26 |
+
X_train_scaled = self.scaler.fit_transform(X_train)
|
27 |
+
X_test_scaled = self.scaler.transform(X_test)
|
28 |
+
|
29 |
+
# Determine if it's a regression or classification problem
|
30 |
+
if y.dtype == 'object' or len(np.unique(y)) < 10:
|
31 |
+
self.model = RandomForestClassifier(n_estimators=100, random_state=42)
|
32 |
+
is_classification = True
|
33 |
+
else:
|
34 |
+
self.model = RandomForestRegressor(n_estimators=100, random_state=42)
|
35 |
+
is_classification = False
|
36 |
+
|
37 |
+
# Train the model
|
38 |
+
self.model.fit(X_train_scaled, y_train)
|
39 |
+
|
40 |
+
# Make predictions
|
41 |
+
y_pred = self.model.predict(X_test_scaled)
|
42 |
+
|
43 |
+
# Evaluate the model
|
44 |
+
if is_classification:
|
45 |
+
accuracy = accuracy_score(y_test, y_pred)
|
46 |
+
report = classification_report(y_test, y_pred)
|
47 |
+
return f"Classification Results:\nAccuracy: {accuracy:.2f}\n\nClassification Report:\n{report}"
|
48 |
+
else:
|
49 |
+
mse = mean_squared_error(y_test, y_pred)
|
50 |
+
r2 = r2_score(y_test, y_pred)
|
51 |
+
return f"Regression Results:\nMean Squared Error: {mse:.2f}\nR-squared Score: {r2:.2f}"
|
52 |
+
|
53 |
+
def get_feature_importance(self):
|
54 |
+
if self.model is None:
|
55 |
+
return "Model has not been trained yet."
|
56 |
+
|
57 |
+
feature_importance = pd.DataFrame({
|
58 |
+
'feature': self.model.feature_names_in_,
|
59 |
+
'importance': self.model.feature_importances_
|
60 |
+
}).sort_values('importance', ascending=False)
|
61 |
+
|
62 |
+
return feature_importance
|