To further refine her analysis, Ana decided to build a simple predictive model using scikit-learn, a machine learning library for Python. She aimed to predict user engagement based on demographics and content preferences.
She began by importing the necessary libraries and loading the dataset into a Pandas DataFrame.
# Evaluate the model y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) print(f'Mean Squared Error: {mse}') Ana's model provided a reasonably accurate prediction of user engagement, which could be used to tailor content recommendations. Python Para Analise De Dados - 3a Edicao Pdf
import pandas as pd import numpy as np import matplotlib.pyplot as plt
# Calculate and display the correlation matrix corr = data.corr() plt.figure(figsize=(10,8)) sns.heatmap(corr, annot=True, cmap='coolwarm', square=True) plt.show() Ana's EDA revealed interesting patterns, such as a strong correlation between age and engagement frequency, and a preference for video content among younger users. These insights were crucial for informing the social media platform's content strategy. To further refine her analysis, Ana decided to
# Plot histograms for user demographics data.hist(bins=50, figsize=(20,15)) plt.show()
# Load the dataset data = pd.read_csv('social_media_engagement.csv') The dataset was massive, with millions of rows, and Ana needed to clean and preprocess it before analysis. She handled missing values, converted data types where necessary, and filtered out irrelevant data. # Evaluate the model y_pred = model
from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_squared_error