-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromclass-PythonIntro.py
More file actions
248 lines (148 loc) · 4.85 KB
/
fromclass-PythonIntro.py
File metadata and controls
248 lines (148 loc) · 4.85 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
#!/usr/bin/env python
# coding: utf-8
# # Introduction to Python for Data Science
# ## Dr. Clif Baldwin
# ## Machine Learning Fundamentals, Spring 2025
# January 21, 2025
# In[1]:
# Set some Python data structures
# Constant number
year = 2025
# Constant string
MLprofessor = "Baldwin"
DLprofessor = "Olsen"
DVprofessor = "Dunn"
DGprofessor = "Laurino"
DEprofessor = "Sokol"
# Vector of numbers
primes = [2,3,5,7,11,13,17,19,23,29]
# List of Strings
classes = ["Introduction to Data", "Data Exploration", "Data Visualization",
"Data Gathering", "Machine Learning", "Deep Learning",
"Data Entrepreneurship"]
# List of string variables
professors = [DLprofessor, MLprofessor, DVprofessor, DGprofessor, DEprofessor]
# List of Strings
classes = ["Introduction to Data", "Data Exploration", "Data Visualization",
"Data Gathering", "Machine Learning", "Deep Learning",
"Data Entrepreneurship"]
# List of lists
courses = [[classes[0], professors[0]],
[classes[1], professors[1]],
[classes[1], professors[3]],
[classes[2], professors[2]],
[classes[3], professors[3]],
[classes[4], professors[1]],
[classes[5], professors[0]],
[classes[6], professors[4]]]
# List of booleans
FallFilter = [True, True, True, False, False, False, False]
# In[2]:
professors
# In[3]:
courses
# In[4]:
classes[0:3]
# In[5]:
import numpy as np
# In[6]:
test = np.array([1, 2, 3, 4])
test
# In[7]:
names = np.array(["Bob", "Clif", "Keiana", "Melissa", "John"])
names
# In[8]:
classes = np.array(classes)
classes
# In[9]:
fall = classes[FallFilter]
fall
# <br>
# While R and other languages use brackets or parentheses to delimit blocks of code, Python uses indentation.<br>
# <br>
# In[10]:
print("The year is " + str(year))
if year < 2025:
print("Completed")
else:
print("Not finished yet")
# In[28]:
year = 2024
print("The year is " + str(year))
text = "Not finished yet"
if year < 2025:
text = "Completed"
print(text)
# In[13]:
# Find the professors who teach classes that have "Data" in the title
for course in courses:
if "Data" in course[0]:
print(course[1])
# In[12]:
# Find all classes that have "Data" in the name
for c in classes:
if "Data" in c:
print(c)
# The Pythonic way to do For Loops is to use something called List Comprehension.
# In[24]:
# Non-Pythonic way
odds = []
for x in range(2, 25):
if x % 2 != 0:
odds.append(x)
print(odds)
# In[25]:
# Or the Pythonic way
odds = [x for x in range(2,25) if x % 2 != 0]
print(odds)
# ## Penguins Data
# In[1]:
import numpy as np
import pandas as pd
penguins = pd.read_csv("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/inst/extdata/penguins.csv",delimiter=",")
# If you have the data saved locally
#penguins = pd.read_csv("penguins.csv",delimiter=",")
# If you load the Python library palmerpenguins
#from palmerpenguins import load_penguins
#penguins = load_penguins()
# In[2]:
# Explore the data
penguins.head()
# In[3]:
# Explore the data
penguins.info()
# From the list of column names, I am initially interested in 'species', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', and 'body_mass_g'. I could add 'island' or any of the other variables, but at this time, I will just explore these five columns. I will save this subset dataset as ds.
# In[13]:
ds = penguins[['species', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']]
ds.head()
# In[12]:
# Graph the data
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title('Palmer Penguins')
plt.xlabel('FlipperLength_mm')
plt.ylabel('BodyMass_g')
ax.scatter(ds.iloc[:,1].to_numpy(), ds.iloc[:,2].to_numpy())
# In[31]:
# Convert categories to numerical values
unique_categories = np.unique(penguins.iloc[:,0])
category_numbers = np.array([np.where(unique_categories == cat)[0][0] for cat in penguins.iloc[:,0]])
# Graph the data by Species
plt.scatter(x=penguins.iloc[:,2].to_numpy(), y=penguins.iloc[:,3].to_numpy(), c=category_numbers, cmap='viridis')
plt.colorbar(ticks=np.arange(len(unique_categories)), format=lambda x, pos: unique_categories[int(x)])
plt.title('Palmer Penguins')
plt.xlabel('FlipperLength_mm')
plt.ylabel('BodyMass_g')
plt.show()
# # Homework
# Read chapters 2, 3, and 4 of <b>Data Science from Scratch</b>
#
# ## Next Week
# Chapters 12 and 20 of <b>Data Science from Scratch</b>
# Assign Project 1 – Write a Python script to read in the Palmer Penguin dataset and explore it.
# Document each step of the code to demonstrate you understand what each block of code does.
# At a minimum, document what columns are in the dataset, what penguins are included, what islands,
# # how many penguins in each group, any outliers among the quantitative data, and any missing data.
#