forked from NovaVolunteer/DS1001-LABS-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclass(1).py
More file actions
369 lines (302 loc) · 11.3 KB
/
inclass(1).py
File metadata and controls
369 lines (302 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: -all
# custom_cell_magics: kql
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.11.2
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %%
# load our libraries setup up our environment for data analysis and visualization
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression # (pip install scikit-learn)
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# %% [markdown]
# ## We are going to work through the Data Science Lifecyle exploring each step
# ### Step 1 Define a question...we can also start with the data but today we are doing the question first
# %% [markdown]
# #### **Question: Which feature(s) of a vehicle most contributes to lower Miles Per Gallon?**
# We are going to use a dataset inside of the seaborn package called "mpg"
# %%
df = sns.load_dataset("mpg")
# %%
print(df.head()) # shows the first 5 rows of the dataframe
# shows a summary of the dataframe including data types and non-null counts
# %%
print(df.info()) # What do we notice here?
# %%
df.describe()
# %% [markdown]
# ### Now lets explore the data types
# %% [markdown]
# #### Common Python Data Types
#
# - **int**: Integer type, used to represent whole numbers.
# *Example*: `x = 5`
#
# - **float**: Floating point type, used for real numbers (decimals).
# *Example*: `y = 3.14`
#
# - **str**: String type, used for text data.
# *Example*: `name = "Alice"`
#
# - **bool**: Boolean type, represents `True` or `False` values.
# *Example*: `is_valid = True`
#
# - **list**: Ordered, mutable collection of items.
# *Example*: `numbers = [1, 2, 3]`
#
# - **tuple**: Ordered, immutable collection of items.
# *Example*: `point = (10, 20)`
#
# - **dict**: Dictionary type, stores key-value pairs.
# *Example*: `person = {"name": "Bob", "age": 30}`
#
# - **set**: Unordered collection of unique items.
# *Example*: `unique_numbers = {1, 2, 3}`
#
# - **NoneType**: Represents the absence of a value.
# *Example*: `result = None`
# %%
print(df.dtypes) # check for the data types of each column
# %% [markdown]
# #### Common Pandas Data Types
#
# - **int64**: Integer values (whole numbers).
# *Example*: `1, 42, -7`
#
# - **float64**: Floating point numbers (decimals).
# *Example*: `3.14, -0.001, 100.0`
#
# - **object**: Typically used for text or mixed types (strings, categorical data, or Python objects).
# *Example*: `"sedan", "usa", "ford mustang"`
#
# - **bool**: Boolean values (`True` or `False`).
# *Example*: `True, False`
#
# - **datetime64[ns]**: Date and time values with nanosecond precision.
# *Example*: `2024-06-01 12:00:00`
#
# - **category**: Categorical data for efficient storage of repeated string values.
# *Example*: `["low", "medium", "high"]` as categories
#
# - **timedelta[ns]**: Differences between two datetime values (time durations).
# *Example*: `2 days, 5 hours`
#
# These data types help pandas optimize storage and operations on your data.
# %% [markdown]
# ### What data type is Cylinders?
# %%
sns.kdeplot(df['cylinders'], fill=True)
plt.title('Density Plot of Cylinders')
plt.xlabel('Cylinders')
plt.ylabel('Density')
plt.show()
# %%
sns.countplot(x='cylinders', data=df)
plt.title('Count of Cars by Number of Cylinders')
plt.xlabel('Cylinders')
plt.ylabel('Count')
plt.show()
# %%
print(df.dtypes) # Are there other features that might need to be changed?
# %% [markdown]
# ### Changing data types
# %%
df['cylinders'] = df['cylinders'].astype('category')
df['model_year'] = df['model_year'].astype('category')
# %%
print(df.dtypes) # check for the data types of each column after the change
# %%
# What is going to happen if we try to plot a density plot of cylinders again?
sns.kdeplot(df['cylinders'], fill=True)
plt.title('Density Plot of Cylinders')
plt.xlabel('Cylinders')
plt.ylabel('Density')
plt.show()
# %%
# what about the bar plot?
sns.countplot(x='cylinders', data=df)
plt.title('Count of Cars by Number of Cylinders')
plt.xlabel('Cylinders')
plt.ylabel('Count')
plt.show()
# %%
# what about a histogram of cylinders?
plt.hist(df['cylinders'])
plt.title('Histogram of Cylinders')
plt.xlabel('Cylinders')
plt.ylabel('Frequency')
plt.show()
# A bar plot is used to display the frequency of categories (discrete values), while a histogram is used to show the
# distribution of a continuous variable by grouping values into bins.
# In this case, since 'cylinders' is categorical, a bar plot (like countplot) is more appropriate than a histogram.
# %%
plt.hist(df['mpg'], bins=20, edgecolor='black')
plt.title('Histogram of MPG')
plt.xlabel('Miles Per Gallon (mpg)')
plt.ylabel('Frequency')
plt.show()
# %% [markdown]
# ### What about missing data, did we have any?
# %%
# check for missing values
print(df.isnull().sum())
# %%
## create boxplot for horsepower
sns.boxplot(x='horsepower', data=df)
plt.title('Boxplot of Horsepower')
plt.xlabel('Horsepower')
plt.show()
# %%
# show the rows with missing horsepower
print(df[df['horsepower'].isnull()])
# %%
# Check if missing 'horsepower' is associated with other features
missing_hp = df['horsepower'].isnull()
print(df[missing_hp].groupby(['cylinders', 'model_year', 'origin']).size())
# Compare summary statistics for rows with and without missing horsepower
print("Summary for rows with missing horsepower:")
print(df[missing_hp].describe(include='all'))
print("\nSummary for rows without missing horsepower:")
print(df[~missing_hp].describe(include='all'))
# %%
# Show means of each variable for rows with and without missing horsepower
means_missing = df[missing_hp].mean(numeric_only=True)
means_not_missing = df[~missing_hp].mean(numeric_only=True)
comparison_df = pd.DataFrame({
'Missing Horsepower': means_missing,
'Not Missing Horsepower': means_not_missing
})
print(comparison_df)
# %% [markdown]
# <details>
# <summary>Click to show/hide content</summary>
# We really don't see much of a pattern, which is good. This likely means there is no systemic problem with the data collections process. It also means we can likely just delete these rows.
# </details>
# %%
df = df.dropna()
print(df.info()) # confirm that there are no missing values left
# %% [markdown]
# ### Ok now we have a clean dataset with are data types labelled correctly, now what do we do?
# %% [markdown]
# #### **Correlation** is a statistical measure that describes the strength and direction of a relationship between two variables. In data analysis, correlation helps us understand how changes in one variable are associated with changes in another. A positive correlation means that as one variable increases, the other tends to increase as well. A negative correlation means that as one variable increases, the other tends to decrease. Correlation values range from -1 (perfect negative correlation) to +1 (perfect positive correlation), with 0 indicating no linear relationship.
# %%
# Calculate correlation of all numeric variables with mpg
correlations = df.corr(numeric_only=True)['mpg'].sort_values()
print("Correlation of variables with mpg (lower means more negative correlation):")
print(correlations)
# %%
# Visualize the correlations
# Remove 'mpg' from the correlations before plotting
correlations_no_mpg = correlations.drop('mpg')
plt.figure(figsize=(6, 4))
sns.barplot(x=correlations_no_mpg.values, y=correlations_no_mpg.index, orient='h')
plt.title('Correlation of Features with MPG (excluding mpg itself)')
plt.xlabel('Correlation Coefficient')
plt.ylabel('Feature')
plt.show()
# %%
plt.figure(figsize=(8, 5))
plt.scatter(df['weight'], df['mpg'], c='tab:blue', label='MPG vs Weight')
plt.scatter(df['weight'], df['weight'], c='tab:orange', label='Weight')
plt.xlabel('Weight')
plt.ylabel('Value')
plt.title('Scatter Plot: Weight vs MPG (blue) and Weight (orange)')
plt.legend()
plt.show()
# %% [markdown]
# #### Why doesn't this really work?
# %%
from sklearn.preprocessing import MinMaxScaler
numeric_cols = ['mpg', 'displacement', 'horsepower', 'weight', 'acceleration']
scaler = MinMaxScaler()
df_normalized = df.copy()
df_normalized[numeric_cols] = scaler.fit_transform(df[numeric_cols])
plt.figure(figsize=(8, 5))
plt.scatter(df_normalized['weight'], df_normalized['mpg'], c='tab:blue', label='MPG vs Weight (normalized)')
plt.xlabel('Normalized Weight')
plt.ylabel('Normalized MPG')
plt.title('Scatter Plot: Normalized Weight vs Normalized MPG')
plt.legend()
plt.show()
# %%
plt.figure(figsize=(8, 5))
sns.scatterplot(x='weight', y='mpg', data=df, color='tab:blue', label='MPG vs Weight')
sns.regplot(x='weight', y='mpg', data=df, scatter=False, color='red', label='Trend Line')
plt.xlabel('Weight')
plt.ylabel('Miles Per Gallon (mpg)')
plt.title('Scatter Plot: Weight vs MPG with Trend Line')
plt.legend()
plt.show()
# %%
plt.figure(figsize=(8, 5))
sns.scatterplot(x='horsepower', y='mpg', data=df, color='tab:blue', label='MPG vs Horsepower')
sns.regplot(x='horsepower', y='mpg', data=df, scatter=False, color='red', label='Trend Line')
plt.xlabel('Horsepower')
plt.ylabel('Miles Per Gallon (mpg)')
plt.title('Scatter Plot: Horsepower vs MPG with Trend Line')
plt.legend()
plt.show()
# %% [markdown]
# #### What about the out categorical feature number of cylinders? Do we think more cylinders worse MPG?
# %%
plt.figure(figsize=(8, 5))
sns.boxplot(x='cylinders', y='mpg', data=df)
plt.title('MPG by Number of Cylinders')
plt.xlabel('Number of Cylinders')
plt.ylabel('Miles Per Gallon (mpg)')
plt.show()
# %%
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
# Select features: all columns except 'name' and 'origin' (object types)
X = df.drop(columns=['name', 'origin', 'mpg'])
# Convert categorical columns to dummy variables
X = pd.get_dummies(X, drop_first=True)
y = df['mpg']
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build and fit the regression model
reg = LinearRegression()
reg.fit(X_train, y_train)
# Predict and evaluate
y_pred = reg.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"Test MSE: {mse:.2f}")
print(f"Test R^2: {r2:.2f}")
coef_table = pd.DataFrame({
'Feature': X.columns,
'Coefficient': reg.coef_
}).sort_values(by='Coefficient', key=abs, ascending=False)
print(coef_table)
# %% [markdown]
# ```markdown
# The regression coefficient for horsepower is approximately -0.03. This means that, holding all other features constant, each additional unit increase in horsepower is associated with a decrease of about 0.03 miles per gallon (mpg) in fuel efficiency.
# ```
# %%
import numpy as np
import plotly.express as px
fig_std = px.scatter_3d(df_normalized, x='weight', y='horsepower', z='mpg',
color='mpg', color_continuous_scale='Viridis',
title='3D Scatter Plot of Normalized MPG, Weight, and Horsepower')
fig_std.update_layout(scene=dict(
xaxis_title='Normalized Weight',
yaxis_title='Normalized Horsepower',
zaxis_title='Normalized MPG'
))
fig_std.update_layout(width=900, height=700)
fig_std.show()