<jupyter_start><jupyter_text>Gym Crowdedness Analysis with PCA >  Objective :  To **predict** how crowded a university gym would be at a given time of day (and some other features, including weather) >  Data Decription :  The dataset consists of 26,000 people counts (about every 10 minutes) over one year. The dataset also contains information about the weather and semester-specific information that might affect how crowded it is. The label is the number of people, which has to be predicted given some subset of the features.**Label**:- Number of people**Features**:     1. date (string; datetime of data)     2. timestamp (int; number of seconds since beginning of day)     3. dayofweek (int; 0 [monday] - 6 [sunday])     4. is_weekend (int; 0 or 1) [boolean, if 1, it's either saturday or sunday, otherwise 0]     5. is_holiday (int; 0 or 1) [boolean, if 1 it's a federal holiday, 0 otherwise]     6. temperature (float; degrees fahrenheit)     7. isstartof_semester (int; 0 or 1) [boolean, if 1 it's the beginning of a school semester, 0 otherwise]     8. month (int; 1 [jan] - 12 [dec])     9. hour (int; 0 - 23) >  Approach The model would be built and PCA would be implemented in the following way : - **Data Cleaning and PreProcessing**- **Exploratory Data Analysis :**        - Uni-Variate Analysis : Histograms , Distribution Plots      - Bi-Variate Analysis : Pair Plots      - Correlation Matrix      - **Processing :**            - OneHotEncoding       - Feature Scaling : Standard Scaler- **Splitting Dataset** - **Principal Component Analysis**- **Modelling : Random Forest**      - Random forest without PCA      - Random Forest with PCA      - **Conclusion**  `1` Data Cleaning and PreProcessing  **Importing Libraries and loading Dataset**<jupyter_code>import numpy as np # linear algebra
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df=pd.read_csv(r'C:\Users\kusht\OneDrive\Desktop\Excel-csv\PCA analysis.csv') #Replace it with your path where the data file is stored
df.head()<jupyter_output><empty_output><jupyter_text>**TASK : Print the `info()` of the dataset**<jupyter_code>### START CODE HERE (~ 1 Line of code)

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Describe the dataset using `describe()`**<jupyter_code>### START CODE HERE (~ 1 Line of code)

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Convert temperature in farenheit into celsius scale using the formula `Celsius=(Fahrenheit-32)* (5/9)`**<jupyter_code>### START CODE HERE (~1 Line of code)

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Convert the timestamp into hours in 12 h format as its currently in seconds and drop `date` coulmn**<jupyter_code>### START CODE HERE: (~ 1 Line of code)

### END CODE<jupyter_output><empty_output><jupyter_text>`2` Exploratory Data Analysis  `2.1` Uni-Variate and Bi-Variate Analysis - **Pair Plots** **TASK : Use `pairplot()` to make different pair scatter plots of the entire dataframe**<jupyter_code>### START CODE HERE :

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK: Now analyse scatter plots between `number_people` and all other attributes using a `for loop` to properly know what are the ideal conditions for people to come to the gym**<jupyter_code>### START CODE HERE 
    
### END CODE<jupyter_output><empty_output><jupyter_text>**Analyse the plots and understand :**1. **At what time , temperature , week of the day more people come in?**        2. **Whether people like to come to the gym in a holiday or a weekend or they prefer to come to gym during working days?**       3. **Which month is most preferable for people to come to the gym?**  - **Distribution Plots** **TASK : Plot individual `distplot()` for `temperature` and `number_people` to check out the individual distribution of the attributes**<jupyter_code>### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>`2.2` Correlation Matrix **TASK : Plot a correlation matrix and make it more understandable using `sns.heatmap`**<jupyter_code>### START CODE HERE : 


### END CODE HERE<jupyter_output><empty_output><jupyter_text>**Analyse the correlation matrix and understand the different dependencies of attributes on each other**   `3.` Processing :   `3.1` One hot encoding :One hot encoding certain attributes to not give any ranking/priority to any instance **TASK: One Hot Encode following attributes `month` , `hour` , `day of week`**<jupyter_code>## YOU CAN USE EITHER get_dummies() OR OneHotEncoder()

### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>`3.2` Feature Scaling :Some attributes ranges are ver different compared to other values and during PCA implementation this might give a problem thus you need to standardise some of the attributes **TASK: Using `StandardScaler()` , standardise `temperature` and `timestamp`**<jupyter_code>## You can use two individual scalers one for temperature and other for timestamp
## you can use an array type data=df.values and standradise data then split data into X and y
from sklearn.preprocessing import StandardScaler
### START CODE HERE : (Replace places having '#' with the code)
data=df.values
scaler1 = StandardScaler()
scaler1.fit(#) # for timestamp
data[#] = scaler1.transform(#)

scaler2 = StandardScaler()
scaler2.fit(data[#]) # for temperature
data[#] = scaler2.transform(data[#])

### END CODE HERE<jupyter_output><empty_output><jupyter_text>`4.` Splitting the dataset :  **TASK : Split the dataset into dependent and independent variables and name them y and X respectively**<jupyter_code>### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Split the X ,y into training and test set**<jupyter_code>from sklearn.model_selection import train_test_split
### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>`5.` Principal Component Analysis Principal component analysis (PCA) is a technique for reducing the dimensionality of such datasets, increasing interpretability but at the same time minimizing information loss. It does so by creating new uncorrelated variables that successively maximize variance. **How does it work? :**- First, a matrix is calculated that summarizes how our variables all relate to one another.- Secondly , The matrix is broken down into two separate components: direction and magnitude. so its easy to understand the “directions” of the data and its “magnitude” (or how “important” each direction is). The photo below, displays the two main directions in this data: the “red direction” and the “green direction.” In this case, the “red direction” is the more important one as given how the dots are arranged, “red direction”  comprises most of the data and thus is s more important than the “green direction” (Hint: Think of  What would fitting a line of best fit to this data look like?)  - Then the data is transformed to align with these important directions (which are combinations of our original variables). The photo below is the same exact data as above, but transformed so that the x- and y-axes are now the “red direction” and “green direction.”  What would the line of best fit look like here?  So PCA tries to find the most important directions in which most of the data is spread and thus reduces it to those components thereby reducing the number of attributes to train and increasing computational speed. A 3D example is given below :   As you can see above a 3D plot is reduced to a 2d plot still retaining most of the data **Now that you have understood this , lets try to implement it**  **TASK : Print the PCA fit_transform of X(independent variables)**<jupyter_code>from sklearn.decomposition import PCA

### START CODE HERE : (Replace spaces having '#' with the code)
pca = PCA()
pca.fit_transform(#)

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Get covariance using `get_covariance()`**<jupyter_code>### START CODE HERE (~ 1 line of code) 

### END CODE HERE<jupyter_output><empty_output><jupyter_text>**TASK : Get explained variance using `explained_variance_ratio`**<jupyter_code>### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Plot a bar graph of `explained variance`**<jupyter_code># you can use plt.bar()

### START CODE HERE : (Replace spaces having '#' with the code)
with plt.style.context('dark_background'):
    plt.figure(figsize=(15,12))

    plt.bar(range(49), '#', alpha=0.5, align='center',
            label='individual explained variance')
    plt.ylabel('#')
    plt.xlabel('#')
    plt.legend(loc='best')
    plt.tight_layout()

### END CODE<jupyter_output><empty_output><jupyter_text>**Analyse the plot and estimate how many componenets you want to keep** **TASK : Make a `PCA()` object with n_components =20 and fit-transform in the dataset (X) and assign to a new variable `X_new`**<jupyter_code>### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>Now , `X_new` is the dataset for PCA **TASK : Get Covariance using `get_covariance`**<jupyter_code>### START CODE HERE (~1 Line of code)

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Get the explained variance using `explained_variance_ratio`**<jupyter_code>### START CODE HERE :


### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Plot bar plot of `exlpained variance`**<jupyter_code># You can use plt.bar()

### START CODE HERE: 
    
### END CODE<jupyter_output><empty_output><jupyter_text>`6.` Modelling : Random Forest To understand Random forest classifier , lets first get a brief idea about Decision Trees in general. Decision Trees are very intuitive and at everyone have used this knowingly or unknowingly at some point . Basically the model keeps sorting them into categories forming a large tree by responses of some questons (decisions) and thats why its called decision tree. An image example would help understand it better :  `Random Forest` : Random forest, like its name implies, consists of a large number of individual decision trees that operate as an [ensemble](https://en.wikipedia.org/wiki/Ensemble_learning) . Each individual tree in the random forest spits out a class prediction and the class with the most votes becomes our model’s prediction.  The fundamental concept is large number of relatively uncorrelated models (trees) operating as a committee will outperform any of the individual constituent models. Since this dataset has very low correlation between attributes , random forest can be a good option. In this section you'll have to make a random forest model and train it on both without PCA dataset and with PCA datset to analyse the differences  `6.1` Random Forest Without PCA **TASK : Make a random forest model and train it on without PCA training set**<jupyter_code># Establish model
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
# Try different numbers of n_estimators and print the scores
# You can use a variable estimators = np.arrange(10,200,10) and then a for loop to take all the values of estimators

### START CODE HERE : (Replace spaces having '#' with code)
estimators = np.arange(10, 200, 10)
scores = []
for n in estimators:
    model.set_params(n_estimators='#')
    model.fit('#', '#')
    scores.append(model.score(X_test, y_test))
print(scores)    

### END CODE HERE<jupyter_output><empty_output><jupyter_text>**TASK : Make a plot between `n_estimator` and `scores` to properly get the best number of estimators**<jupyter_code>## Use plt.plot

### START CODE HERE : 

### END CODE HERE<jupyter_output><empty_output><jupyter_text>`6.2` Random Forest With PCA **TASK : Split the your dataset with PCA into training and testing set using `train_test_split`**<jupyter_code>from sklearn.model_selection import train_test_split
### START CODE HERE  :

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Make a random forest model called `model_pca` and fit it into the new X_train and y_train and then print out the random forest scores for dataset with PCA applied to it**<jupyter_code># Establish model
from sklearn.ensemble import RandomForestRegressor
model_pca = RandomForestRegressor()
# You can use different number of estimators
# # You can use a variable estimators = np.arrange(10,200,10) and then a for loop to take all the values of estimators

### START CODE HERE : 

### END CODE<jupyter_output><empty_output><jupyter_text>**TASK : Make a plot between `n_estimator` and `score` and find the best parameter**<jupyter_code># you can use plt.plot
### START CODE HERE : 


### END CODE<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09354559066908345, 'fraction_numerical': 0.00859011742454094, 'mean_word_length': 5.11271676300578, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 108, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '668531', 'license': 'MIT', 'n_tokens_mistral': 3858, 'n_tokens_neox': 3561, 'n_words': 1555, 'path': 'Gym Crowd Analysis/Gym Crowd Analysis with PCA (ToDo Template).ipynb', 'repo_name': 'abhisngh/Data-Science', 'size': '12689'}