Skip to content

Commit 6c2a21f

Browse files
committed
Completed Question 7 of Final Assignment
1 parent 9bc8f35 commit 6c2a21f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

02 DA0101EN Analyzing Data with Python/Module 6 - Final Assignment/AnalyzingWherePeopleDrink.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import matplotlib as mpl
1010
import seaborn as sns
1111
from sklearn.linear_model import LinearRegression
12+
from sklearn.model_selection import train_test_split
1213

1314
df= pd.read_csv('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/edx/project/drinks.csv')
1415
# df.to_csv('original.csv') <- Uncomment to save dataset
@@ -94,10 +95,20 @@
9495
#########################################################################################
9596
# Question 7 #
9697
# Use list of features to predict the 'total_litres_of_pure_alcohol', #
97-
# split the data into training and testing and determine the 𝑅2 on the test data, #
98-
# using the provided code: #
98+
# split the data into training and testing and determine the 𝑅2 on the test data #
99+
# #
99100
# Note: Please use test_size = 0.10 and random_state = 0 in the following questions. #
100101
#########################################################################################
101102

102103
print("\nQuestion 7\n")
103104

105+
lr = LinearRegression()
106+
107+
x_data = df.drop(['total_litres_of_pure_alcohol', 'continent', 'country'],axis=1)
108+
y_data = df[['total_litres_of_pure_alcohol']]
109+
110+
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.1, random_state=0)
111+
112+
lr.fit(x_train[['beer_servings', 'spirit_servings', 'wine_servings']], y_train)
113+
114+
print("The R^2 score of model using test data is:", lr.score(x_test, y_test))

0 commit comments

Comments
 (0)