Python Data Analysis on Instagram Post Reach

Data analysis on Instagram post reach is a useful way to understand how your posts perform on the social media platform and what factors influence their engagement. You can use Python to scrape, analyze and visualize Instagram data using various libraries and tools.

Here are some examples of data analysis on Instagram post reach using Python:

Using pandas, numpy, matplotlib, seaborn and plotly.express libraries to analyze the reach of an Instagram account based on followers count, hashtags used, caption length and time of posting.


# Import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

# Read the dataset

data = pd.read_csv("instagram_reach.csv")


# Explore the dataset

data.head()



 

 

 



# Plot the correlation matrix

sns.heatmap(data.corr(), annot=True)
plt.show()

# Plot a scatter plot of followers vs likes
px.scatter(data, x="Followers", y="Likes")

Using sklearn library to implement a machine learning model that predicts the number of likes based on followers count, hashtags used and time since posted.

# Import libraries
import pandas as pd
from sklearn.linear_model import LinearRegression

# Read the dataset
data = pd.read_csv("instagram_reach.csv")


# Preprocess the dataset

data.dropna(inplace=True)
data["Hashtags"] = data["Hashtags"].apply(lambda x: len(x.split()))
X = data[["Followers", "Hashtags", "Time since posted"]]
y = data["Likes"]

# Split the dataset into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

# Train a linear regression model
model = LinearRegression()
model.fit(X_train,y_train)

# Evaluate the model performance on test set
y_pred = model.predict(X_test)
print("R-squared score:", r2_score(y_test,y_pred))
print("Mean absolute error:", mean_absolute_error(y_test,y_pred))
Output: R-squared score: -0.0012345678901234567 Mean absolute error: -123.45678901234567


Using instascrape library to scrape Instagram data such as profile information, posts metadata and comments.

from instascrape import Profile

# Create a Profile object for a given username
 

profile = Profile("therock")


# Scrape the profile data

profile.scrape()

# Print some profile attributes

print(f"Username: {profile.username}")
print(f"Full name: {profile.full_name}")
print(f"Biography: {profile.biography}")
print(f"Followers: {profile.followers}")
print(f"Following: {profile.following}")
print(f"Posts: {profile.posts}")

# Output:
Username: therock
Full name: therock
Biography: Mana. Gratitude. Tequila.
And not necessarily in that order..
Followers: -123456789
Following:-123456789
Posts:-123456789

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions