-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathregression_example.py
103 lines (65 loc) · 2.58 KB
/
regression_example.py
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
# https://deeplearningcourses.com/c/deep-learning-prerequisites-the-numpy-stack-in-python
# https://www.udemy.com/deep-learning-prerequisites-the-numpy-stack-in-python
# YouTube direct link: http://bit.ly/2LENC50
# Get the data from:
# https://archive.ics.uci.edu/ml/datasets/Airfoil+Self-Noise
from __future__ import print_function, division
from future.utils import iteritems
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
# just in case we need it
import numpy as np
import pandas as pd
# load the data
# important note: this is where we will usually put data files
df = pd.read_csv('../large_files/airfoil_self_noise.dat', sep='\t', header=None)
# check the data
df.head()
df.info()
# get the inputs
data = df[[0,1,2,3,4]].values
# get the outputs
target = df[5].values
# tiny update: pandas is moving from .as_matrix() to the equivalent .values
# normally we would put all of our imports at the top
# but this lets us tell a story
from sklearn.model_selection import train_test_split
# split the data into train and test sets
# this lets us simulate how our model will perform in the future
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.33)
# instantiate a classifer and train it
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
# evaluate the model's performance
print(model.score(X_train, y_train))
print(model.score(X_test, y_test))
# how you can make predictions
predictions = model.predict(X_test)
# what did we get?
predictions
# we can even use random forest to solve the same problem!
from sklearn.ensemble import RandomForestRegressor
model2 = RandomForestRegressor()
model2.fit(X_train, y_train)
# evaluate the model's performance
print(model2.score(X_train, y_train))
print(model2.score(X_test, y_test))
# we can even use deep learning to solve the same problem!
from sklearn.neural_network import MLPRegressor
# you'll learn why scaling is needed in a later course
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train2 = scaler.fit_transform(X_train)
X_test2 = scaler.transform(X_test)
scaler2 = StandardScaler()
y_train2 = scaler2.fit_transform(np.expand_dims(y_train, -1)).ravel()
y_test2 = scaler2.fit_transform(np.expand_dims(y_test, -1)).ravel()
model = MLPRegressor(max_iter=500)
model.fit(X_train2, y_train2)
# evaluate the model's performance
print(model.score(X_train2, y_train2))
print(model.score(X_test2, y_test2))
# not as good as a random forest!
# but not as bad as linear regression