0% found this document useful (0 votes)
11 views9 pages

PCA Scatter Plot Analysis in Python

Python Code for simple TikTok Game

Uploaded by

Vedant Gade
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

PCA Scatter Plot Analysis in Python

Python Code for simple TikTok Game

Uploaded by

Vedant Gade
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Import necessary libraries

import pandas as pd
from matplotlib import pyplot as plt
from [Link] import PCA
from [Link] import SVC
import seaborn as sns
from [Link] import KMeans
# Read the excel data using pandas
raw_data = pd.read_excel("C:/Users/Bobby/Documents/Food and [Link]")
# Print the first few rows of the data
print(raw_data.head())
# Print statistical summary of the data
print(raw_data.describe())
# Print information about the data
print(raw_data.info)
# Extract column names and first 100 rows
X = list(raw_data.head(76).columns)
y = tuple(raw_data.head(100).iterrows())
# Print column names and a subset of rows
print(X)
print(y[1:10])
# Convert data into a dictionary
data = dict(zip(X, y))
print([Link]())
print([Link]())
# Create an empty list to store dictionaries
food_data = []
# Loop through each row and convert it to a dictionary
for row in y:
row_data = [Link]("\t") # Assuming the data is tab-separated
food_dict = dict(zip(columns, row_data
# Convert values to a list where applicable
for key in food_dict:
if isinstance(food_dict[key], str) and key != "name" and key != "serving_size":
food_dict[key] = [food_dict[key]]
elif isinstance(food_dict[key], int):
food_dict[key] = [food_dict[key]]
# Append the dictionary to the list
food_data.append(food_dict)
# Now, 'food_data' is a list containing dictionaries for all rows
# Create an empty list to store dictionaries
list_of_dicts = []
for x in range(0, len(X)):
row_dict = raw_data.iloc[x].to_dict()
list_of_dicts.append(row_dict)
# Check for missing values
ms = missing_values = raw_data.isnull()
#raw_data.fillna(raw_data.sum(), inplace=True)
# Subset of columns for analysis
subset_columns = ["name", "serving_size", "calories", "total_fat", "saturated_fat",
"cholesterol", "sodium", "choline",
"folate", "folic_acid", "niacin", "pantothenic_acid", "riboflavin", "thiamin",
"vitamin_a",
"vitamin_a_rae",
"carotene_alpha", "carotene_beta", "cryptoxanthin_beta", "lutein_zeaxanthin",
"lucopene",
"vitamin_b12",
"vitamin_b6", "vitamin_c", "vitamin_d", "vitamin_e",
"tocopherol_alpha", "vitamin_k", "calcium", "copper",
"irom", "magnesium", "manganese", "phosphorous", "potassium",
"selenium", "zink", "protein", "alanine",
"arginine", "aspartic_acid", "cystine", "glutamic_acid", "glycine", "histidine",
"hydroxyproline",
"isoleucine", "leucine", "lysine", "methionine", "phenylalanine", "proline",
"serine", "threonine",
"tryptophan", "tyrosine", "valine", "carbohydrate", "fiber", "sugars", "fructose",
"galactose", "glucose",
"lactose", "maltose", "sucrose", "fat", "saturated_fatty_acids",
"monounsaturated_fatty_acids",
"polyunsaturated_fatty_acids", "fatty_acids_total_trans", "alcohol", "ash",
"caffeine", "theobromine",
"water"]
# Example: Create a scatter plot between two numeric columns
[Link](raw_data['serving_size'], raw_data['calories'], cmap='inferno', linewidths=0.56,
c='g')
[Link]()
[Link]('Serving Size')
[Link]('Calories')
[Link]('Scatter Plot for Bivariate Analysis')
[Link]()
# Calculate the correlation matrix
raw_data1 = raw_data.drop(columns='name')
correlation_matrix = raw_data1.corr()
filled_values = raw_data1.dropna()
# Multivariate Analysis
numerical_data = raw_data.drop(columns=['name'])
# Perform PCA
pca = PCA(n_components=3)
pca_result = pca.fit_transform(filled_values)
# Scatter plot of PCA result
[Link](figsize=(8, 6))
[Link](pca_result[:, 0], pca_result[:, 1], alpha=0.6, linewidths=(3, 4), cmap='gray',
c='gray')
[Link]()
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]()
[Link]('PCA Result: 2D Scatterplot')
[Link](True)
[Link]()
# Check if the columns exist in the DataFrame
# missing_columns = [col for col in subset_columns if col not in raw_data.columns]
# if not missing_columns:
# Your pair plot code here
# [Link](raw_data[subset_columns])
# [Link]()
# else:
# print("Columns not found in DataFrame:", missing_columns)
# print("Available columns:", raw_data.columns)
# print("Column data types:", raw_data.dtypes)
print(filled_values)
# Perform K-Means clustering
n_clusters = KMeans(n_clusters=50, n_init="auto", algorithm='lloyd', max_iter=1000)
no_clusters = n_clusters.fit_transform(filled_values)
# Scatter plot of K-Means clusters
[Link](figsize=(5, 8))
[Link](1, 1, 1)
[Link](no_clusters[:, 0], no_clusters[:, 1], linewidths=(2, 3), c='b', cmap='inferno')
[Link]()
[Link](True)
[Link]()
# Performing Bivariate analysis on the data using histogram
[Link](filled_values, x='serving_size', y='calories', stat="count", binwidth=0.56,
bins="auto", element='step',
palette='colorblind', binrange=(100, 200))
[Link]()
1)pandas:

Description: Pandas is a popular Python library for data manipulation and


analysis. It provides data structures like dataframes and series for working
with structured data.

2)[Link]:
Description: Matplotlib is a data visualization library for creating static,
animated, or interactive plots in Python. pyplot is a collection of functions
that provide a simple interface for creating various types of plots.

3) [Link]:

Description: This is part of the scikit-learn library (sklearn) and provides


Principal Component Analysis (PCA) for dimensionality reduction and
feature extraction.

4)[Link]:

Description: This is also part of scikit-learn and stands for Support Vector
Classification. It is used for classification tasks using Support Vector
Machines (SVM).

5)seaborn:

Description: Seaborn is a data visualization library based on Matplotlib. It


provides a high-level interface for creating informative and attractive
statistical graphics.

6)[Link]:
Description: Another part of scikit-learn, KMeans is an unsupervised
machine learning algorithm used for clustering data into groups based on
similarity.

a)Import necessary libraries:

import pandas as pd
from matplotlib import pyplot as plt
from [Link] import PCA
from [Link] import SVC
import seaborn as sns
from [Link] import KMeans

b)Read the data from an Excel file using Pandas:

raw_data = pd.read_excel("C:/Users/Bobby/Documents/Food and


[Link]")

c)Extract and print the first few rows and statistical summary of the data:

print(raw_data.head())
print(raw_data.describe())

d)Extract column names and a subset of rows:


X = list(raw_data.head(76).columns)
y = tuple(raw_data.head(100).iterrows())
e)Convert data into a dictionary and print values and keys:

data = dict(zip(X, y))


print([Link]())
print([Link]())

f)Create an empty list to store dictionaries and convert rows to dictionaries:


food_data = []
for row in y:
row_data = [Link]("\t")
food_dict = dict(zip(columns, row_data))
# Convert values to lists where applicable
and append the dictionary
food_data.append(food_dict)

g)Create a list of dictionaries and check for missing values:


list_of_dicts = []
for x in range(0, len(X)):
row_dict = raw_data.iloc[x].to_dict()
list_of_dicts.append(row_dict)

ms = missing_values = raw_data.isnull()

h)Define a subset of columns for analysis:

subset_columns = [list of column names]


i)Create a scatter plot between two numeric columns:

[Link](raw_data['serving_size'], raw_data['calories'], cmap='inferno',


linewidths=0.56, c='g')
[Link]()
[Link]('Serving Size')
[Link]('Calories')
[Link]('Scatter Plot for Bivariate Analysis')
[Link]()

j)Calculate the correlation matrix and perform PCA:

raw_data1 = raw_data.drop(columns='name')
correlation_matrix = raw_data1.corr()
filled_values = raw_data1.dropna()

pca = PCA(n_components=3)
pca_result = pca.fit_transform(filled_values)

k)Create a scatter plot of PCA result:

[Link](figsize=(8, 6))
[Link](pca_result[:, 0], pca_result[:, 1], alpha=0.6, linewidths=(3, 4),
cmap='gray', c='gray')
[Link]()
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]()
[Link]('PCA Result: 2D Scatterplot')
[Link](True)
[Link]()

l)Perform K-Means clustering and create a scatter plot of clusters:

n_clusters = KMeans(n_clusters=50, n_init="auto", algorithm='lloyd',


max_iter=1000)
no_clusters = n_clusters.fit_transform(filled_values)

[Link](figsize=(5, 8))
[Link](1, 1, 1)
[Link](no_clusters[:, 0], no_clusters[:, 1], linewidths=(2, 3), c='b',
cmap='inferno')
[Link]()
[Link](True)
[Link]()

m)Perform bivariate analysis using a histogram:

[Link](filled_values, x='serving_size', y='calories', stat="count",


binwidth=0.56, bins="auto",
element='step', palette='colorblind', binrange=(100, 200))
[Link]()

Common questions

Powered by AI

Python libraries such as pandas, sklearn, and seaborn significantly enhance the efficiency and accuracy of nutritional data analysis. Pandas provide powerful tools for data manipulation and analysis with dataframes, while sklearn offers algorithms for machine learning tasks including clustering and dimensionality reduction. Seaborn complements these by enabling high-level data visualization tailored to statistical graphics. Together, they streamline complex workflows, allowing researchers to focus more on analysis and less on data processing .

A correlation matrix is essential in nutritional data analysis as it quantifies the degree to which pairs of nutritional variables, such as vitamins or minerals, are linearly related. By identifying these relationships, the matrix helps in understanding dependencies or multicollinearities that might exist within the data. This information is vital for feature selection and dimensionality reduction processes prior to modeling, ensuring the extracted insights are robust and reliable .

Defining a subset of columns is crucial as it helps streamline the data analysis process by focusing only on relevant variables that may impact the outcome of interest. This strategy reduces computational complexity, improves model performance, and enhances interpretability by removing irrelevant or redundant columns. By concentrating on key nutritional components, the analysis becomes more efficient and targeted .

Missing values in nutritional datasets can significantly skew analysis and lead to unreliable conclusions. They might introduce bias if the missingness is systematic, for example, if certain types of nutritional information are missing for specific food groups. This can result in data misinterpretation or data-driven predictions that do not accurately reflect reality. Handling missing data through imputation methods or analysis adjustment is crucial for maintaining the integrity of statistical conclusions .

Validating PCA results through visualization like scatter plots is crucial because it helps in assessing the separation or overlap of data points in reduced dimensions. Effective visual validation confirms that PCA has captured the essential patterns and variations present in the high-dimensional data. This visual context ensures the principal components are meaningful and that any subsequent interpretation or decision-making based on PCA is sound .

Scatter plots allow visualization of the relationship between food servings and calories by depicting data points along coordinate axes. Patterns or trends such as positive, negative, or no correlation become evident at a glance. Histograms, on the other hand, show frequency distributions which can highlight central tendencies and variability, further uncovering how portions relate to calorie intake. Both aid in visual assessment of relationships, guiding more in-depth statistical analysis .

Challenges include handling missing or incomplete data, which can distort analysis and model predictions. Another challenge is data inconsistencies, such as different measuring units, that complicate direct comparisons. Additionally, varying data formats, unexpected special characters, or incorrect data types can lead to errors in processing and analysis, requiring extensive data cleaning and transformation .

Principal Component Analysis (PCA) helps in multivariate analysis by reducing the dimensionality of the data while preserving as much variance as possible. It transforms the original nutritional data variables, such as vitamins and minerals, into a new set of uncorrelated variables called principal components. This aids in simplifying the complexity in high-dimensional data and helps in visualizing differences in nutritional profiles across food items effectively .

K-Means clustering helps in identifying patterns in nutritional data by grouping data points—for example, food items—into clusters based on similarities in their nutritional content. This unsupervised learning technique reveals the natural grouping in the dataset, which can be useful to identify similar nutritional profiles across different food items. This can further assist in personalizing diet plans based on cluster characteristics .

Matplotlib plays a pivotal role in visualizing nutritional data by allowing researchers to create a range of plots such as scatter, line, and histograms, which can highlight different aspects of the data. These visual representations facilitate easier understanding and interpretation of complex nutritional datasets, making trends, correlations, and outliers more apparent. Effective visualization aids in drawing more informed conclusions and supports data-driven decision-making .

You might also like