<jupyter_start><jupyter_text>Analyzing Where Do People Drink?Estimated time needed: **30** minutes ObjectivesAfter completing this lab you will be able to:-   Be confident about your data analysis skills<jupyter_code>This Dataset is from the story <a href=https://fivethirtyeight.com/features/dear-mona-followup-where-do-people-drink-the-most-beer-wine-and-spirits/>  Dear Mona Followup: Where Do People Drink The Most Beer, Wine And Spirits? </a>  The dataset contains Average serving sizes per person such as average wine, spirit, beer servings. As well as several other metrics. You will be asked to analyze the data and predict the total liters served given the servings. See how to share your lab at the end.<jupyter_output><empty_output><jupyter_text>You will need the following libraries:<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl<jupyter_output><empty_output><jupyter_text>1.0 Importing the Data Load the csv:<jupyter_code>df= pd.read_csv('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/edx/project/drinks.csv')
df<jupyter_output><empty_output><jupyter_text>We use the method  head()  to display the first 5 columns of the dataframe:<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Question 1:  Display the data types of each column using the attribute dtype.<jupyter_code>df.dtypes<jupyter_output><empty_output><jupyter_text>Question 2 use the method groupby to get the number of wine servings per continent:<jupyter_code>servings = df[["wine_servings"]]
servings = servings.groupby(df["continent"]).count()
servings<jupyter_output><empty_output><jupyter_text>Question 3: Perform a statistical summary and analysis of beer servings for each continent:<jupyter_code>df.corr()<jupyter_output><empty_output><jupyter_text>Question 4: Use the function boxplot in the seaborn library to produce a plot that can be used to show the number of beer servings on each continent.<jupyter_code>df.count()
Q1 = df.quantile(0.25)
Q1
Q2 = df.quantile(0.50)
Q2
Q3 = df.quantile(0.75)
Q3
IQR = Q3 - Q1
IQR<jupyter_output><empty_output><jupyter_text>Question 5: Use the function  regplot in the seaborn library to determine if the number of wine servings isnegatively or positively correlated with the number of beer servings.<jupyter_code>import seaborn as sns 
sns.regplot(x = "beer_servings", y = "wine_servings", data = df)<jupyter_output><empty_output><jupyter_text>Question 6: Fit a linear regression model to predict the 'total_litres_of_pure_alcohol' using the number of 'wine_servings' then calculate $R^{2}$:<jupyter_code>from sklearn.linear_model import LinearRegression
lr = LinearRegression()
x = df[["wine_servings"]]
y = df["total_litres_of_pure_alcohol"]
lr.fit(x, y)
lr.score(x, y)
yhat = lr.predict(x)
yhat<jupyter_output><empty_output><jupyter_text>Note: Please use test_size = 0.10 in the following questions.  Question 7Use the list of features to predict the 'total_litres_of_pure_alcohol', split the data into training and testing and determine the $R^2$ on the test data, using the provided code:<jupyter_code>from sklearn.linear_model import LinearRegression
df.columns
x = df[["beer_servings", "spirit_servings", "wine_servings"]]
y = df["total_litres_of_pure_alcohol"]

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.10, random_state = 0)

lr.fit(x, y)
lr.score(x, y)
yhat = lr.predict(x)
yhat[0:4]<jupyter_output><empty_output><jupyter_text>Question 8 : Create a pipeline object that scales the data, performs a polynomial transform and fits a linear regression model. Fit the object using the training data in the question above, then calculate the R^2 using. the test data. Take a screenshot of your code and the $R^{2}$. There are some hints in the notebook: 'scale''polynomial''model'The second element in the tuple contains the model constructorStandardScaler()PolynomialFeatures(include_bias=False)LinearRegression()<jupyter_code>from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler,PolynomialFeatures

Input = [("scale", StandardScaler()), ("polynomial", PolynomialFeatures(include_bias = False)), ("model", LinearRegression())]
pipe = Pipeline(Input)
pipe.fit(x, y)
yhat = pipe.predict(x)
yhat<jupyter_output><empty_output><jupyter_text>Question 9: Create and fit a Ridge regression object using the training data, setting the regularization parameter to 0.1 and calculate the $R^{2}$ using the test data. Take a screenshot of your code and the $R^{2}$<jupyter_code>from sklearn.linear_model import Ridge
RidgeModel = Ridge(alpha = 0.1)
RidgeModel.fit(x, y)
RidgeModel.score(x, y)
yhat = RidgeModel.predict(x)
yhat<jupyter_output><empty_output><jupyter_text>Question 10 : Perform a 2nd order polynomial transform on both the training data and testing data.  Create and fit a Ridge regression object using the training data, setting the regularization parameter to 0.1. Calculate the $R^{2}$ utilizing the test data provided. Take a screen-shot of your code and the $R^{2}$.<jupyter_code>from sklearn.preprocessing import PolynomialFeatures
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.1, random_state = 0)

pr = PolynomialFeatures(degree = 2)
x_train_pf = pr.fit_transform(x_train)
x_test_pf = pr.fit_transform(x_test)

from sklearn.linear_model import Ridge
RidgeModel = Ridge(alpha = 0.1)
RidgeModel.fit(x_train_pf, y_train)
yhat = RidgeModel.predict(x_test_pf)
print("Predicted:", yhat[0:4])
print("test set:", y_test[0:4].values)<jupyter_output>Predicted: [0.67484044 4.41230278 8.54359723 0.78459143]
test set: [0.  4.6 8.2 0.1]
{'fraction_non_alphanumeric': 0.09430667132378624, 'fraction_numerical': 0.02008382815228781, 'mean_word_length': 6.605577689243028, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 2, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '512220', 'license': 'Apache-2.0', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1879, 'n_words': 584, 'path': 'Jupiter Book/EDA.ipynb', 'repo_name': 'Pandula1234/PythonDeepSource', 'size': '5726'}