-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathday_2.py
More file actions
168 lines (118 loc) · 3.71 KB
/
day_2.py
File metadata and controls
168 lines (118 loc) · 3.71 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
import matplotlib as mpl
import numpy as np
import pandas as pd
import sklearn as sk
import statsmodels as sm
list = [2, 3, 4, 5]
# Task: Add 1 to each number in a list
def addone(list):
"""Option 1: Create an empty list and append each value + 1 from the old list"""
new_list = []
for l in list:
new_list.append(l + 1)
return new_list
new = addone(list)
print(new)
# Alternative:
def add_one(x):
"""Directly pasting in the same list.
More efficient when working with big data, because
the allocated space is already there."""
for i, value in enumerate(x):
x[i] = value + 1
return x
# Third option:
list = [l + 1 for l in list]
print(list)
# Arrays
list2 = [4,3,2,1]
x1 = np.array(list)
x2 = np.array(list2)
x1 + x2
# Generate array
np.arange(1,10)
# Add a "linear space" -> equally spread values between values
np.linspace(0,10,15) # Add .astype(int) if you don't want floats
# Generate random numbers
np.random.normal(100, 5, 100).astype(int)
# Add the next number to a list
list = [1,2,3,5,6,2]
def addrange(list):
addition = np.arange(1, len(list) + 1)
l_as_array = np.array(list)
new = addition + l_as_array
return new
addrange(list)
## Prettier solution from other student:
def addrange(list):
newlist = np.array([i+1 for i in range(len(list))])
return newlist+np.array(list)
addrange(list)
# Get every third object
list[::3]
# Check which elements meet a criteria -> masking with boolean arrays
x = np.array(list)
mask = x <= 3
x[mask]
x[x == 2] # Can also be done directly
## Replace within the list based on criteria
x[x < 2] = 0
x
# Check mean
np.mean(x)
x.mean() # Works both ways
# Missing values
x[x == 0] = np.nan()
x = np.array([3, 5, np.nan, 3, 2])
x.mean() # by default NaN is not omitted, so mean with nan is not possible
# How to omit NaNs
## Solution 1: filter put NaNs
x[~np.isnan(x)].mean()
## Solution 2: NaN-friendly functions
np.nanmean(x) # Also possible for other math ops like sum, median, diff, etc.
# Exercise 12
vector = np.random.normal(100, 50, 100)
x = [2, 3, 4, 1, 3, 4, 8510, -102247, 2]
def drop_outlier(vector):
""" Replaces values that are superior or inferior to 3*SD by NaN """
vector = np.array(vector).astype(float)
sd = np.nanstd(vector) * 3
mean = np.nanstd(vector)
vector[(mean - sd > vector) | (vector > mean + sd)] = np.nan
return vector
drop_outlier(x)
def describe(vector):
""" Return a string with the mean, SD, and number of outliers """
mean = np.nanmean(vector)
sd = np.nanstd(vector)
outliers = len(vector[np.isnan(vector)])
return str(f'Mean: {mean}, SD: {sd}, Outliers: {outliers}')
describe(vector)
### For pretty printing: np.round(x, 2)
# Dataframes
data = {"x1": [1, 2, 3], "x2": [6, 5, 4], "x3": ["A", "A", "B"]}
data = pd.DataFrame(data)
data
# If you extract just one column it outputs a series instead of df
data['x1']
# You can get around this by calling it as a list inside of the list:
data[['x1']] # No difference for me, check later!
# Call index
data.index
data.index = range(2,5) # Changes the index, can also be changed to letters etc
# Location to filter
data.loc[data['x1'] >= 2]
data.loc[0:2] # Dependent on self-set index
data.iloc[0:2] # Uses the indexed order independent of indivualised names of the indeces
# Read csvs into Pandas
df = pd.read_csv('iris.csv')
df.head()
# Check how many unique values there are in a column (function from numpy)
df['Species'].unique()
# Describe function
df.describe()
df['Species'].describe()
# Plotting
df.plot.hist(subplots = True) # Allows separate histograms
df.plot.hist(bins = 100, alpha = 0.5) # Allows better overview of overlap in one histogram
df.plot.scatter(x = 'Sepal.Length', y = 'Sepal.Width')