AI Algorithms for Data Analysis
1.1 Overview
1.2 Predictive Model for User Engagement Analysis
1.2.1 Data Preparation and Processing
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load and clean dataset
data = pd.read_csv("user_engagement.csv")
data.dropna(inplace=True) # Remove missing values
# Feature selection
X = data[['time_spent', 'actions', 'pages_visited', 'user_age']]
y = data['engagement_label'] # 1 for high engagement, 0 for low engagement
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature scaling
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
1.2.2 Model Training: Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Model setup and training
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate accuracy
y_pred = model.predict(X_test)
print("Model Accuracy:", accuracy_score(y_test, y_pred))
1.2.3 Saving and Loading the Model for Production
import joblib
# Save model to file
joblib.dump(model, 'user_engagement_model.pkl')
# Load model
model = joblib.load('user_engagement_model.pkl')
1.3 Data Analysis and Prediction API Implementation
from flask import Flask, request, jsonify
import joblib
import numpy as np
app = Flask(__name__)
# Load pre-trained model
model = joblib.load('user_engagement_model.pkl')
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
features = np.array([data['time_spent'], data['actions'], data['pages_visited'], data['user_age']]).reshape(1, -1)
prediction = model.predict(features)
return jsonify({'engagement_prediction': int(prediction[0])})
if __name__ == '__main__':
app.run(debug=True)
1.4 Request Example using cURL
curl -X POST -H "Content-Type: application/json" -d '{"time_spent": 30, "actions": 5, "pages_visited": 7, "user_age": 24}' http://localhost:5000/predict
1.5 Expected Response
{
"engagement_prediction": 1
}
1.6 Advanced Usage: Neural Network for Predictive Modeling
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Build neural network model
nn_model = Sequential([
Dense(32, activation='relu', input_shape=(X_train.shape[1],)),
Dense(16, activation='relu'),
Dense(1, activation='sigmoid')
])
# Compile model
nn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train model
nn_model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)
# Save neural network model
nn_model.save('user_engagement_nn_model.h5')
Last updated