|
| 1 | +# https://deeplearningcourses.com/c/deep-learning-prerequisites-the-numpy-stack-in-python |
| 2 | +# https://www.udemy.com/deep-learning-prerequisites-the-numpy-stack-in-python |
| 3 | + |
| 4 | +# Get the data from: |
| 5 | +# https://archive.ics.uci.edu/ml/datasets/Airfoil+Self-Noise |
| 6 | + |
| 7 | +from __future__ import print_function, division |
| 8 | +from future.utils import iteritems |
| 9 | +from builtins import range, input |
| 10 | +# Note: you may need to update your version of future |
| 11 | +# sudo pip install -U future |
| 12 | + |
| 13 | + |
| 14 | +# just in case we need it |
| 15 | +import numpy as np |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | + |
| 19 | +# load the data |
| 20 | +# important note: this is where we will usually put data files |
| 21 | +df = pd.read_csv('../large_files/airfoil_self_noise.dat', sep='\t', header=None) |
| 22 | + |
| 23 | +# check the data |
| 24 | +df.head() |
| 25 | +df.info() |
| 26 | + |
| 27 | +# get the inputs |
| 28 | +data = df[[0,1,2,3,4]].values |
| 29 | + |
| 30 | +# get the outputs |
| 31 | +target = df[5].values |
| 32 | + |
| 33 | +# tiny update: pandas is moving from .as_matrix() to the equivalent .values |
| 34 | + |
| 35 | + |
| 36 | +# normally we would put all of our imports at the top |
| 37 | +# but this lets us tell a story |
| 38 | +from sklearn.model_selection import train_test_split |
| 39 | + |
| 40 | + |
| 41 | +# split the data into train and test sets |
| 42 | +# this lets us simulate how our model will perform in the future |
| 43 | +X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.33) |
| 44 | + |
| 45 | + |
| 46 | +# instantiate a classifer and train it |
| 47 | +from sklearn.linear_model import LinearRegression |
| 48 | + |
| 49 | + |
| 50 | +model = LinearRegression() |
| 51 | +model.fit(X_train, y_train) |
| 52 | + |
| 53 | + |
| 54 | +# evaluate the model's performance |
| 55 | +print(model.score(X_train, y_train)) |
| 56 | +print(model.score(X_test, y_test)) |
| 57 | + |
| 58 | + |
| 59 | +# how you can make predictions |
| 60 | +predictions = model.predict(X_test) |
| 61 | + |
| 62 | +# what did we get? |
| 63 | +predictions |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +# we can even use random forest to solve the same problem! |
| 68 | +from sklearn.ensemble import RandomForestRegressor |
| 69 | + |
| 70 | +model2 = RandomForestRegressor() |
| 71 | +model2.fit(X_train, y_train) |
| 72 | + |
| 73 | + |
| 74 | +# evaluate the model's performance |
| 75 | +print(model2.score(X_train, y_train)) |
| 76 | +print(model2.score(X_test, y_test)) |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# we can even use deep learning to solve the same problem! |
| 82 | +from sklearn.neural_network import MLPRegressor |
| 83 | + |
| 84 | +# you'll learn why scaling is needed in a later course |
| 85 | +from sklearn.preprocessing import StandardScaler |
| 86 | + |
| 87 | +scaler = StandardScaler() |
| 88 | +X_train2 = scaler.fit_transform(X_train) |
| 89 | +X_test2 = scaler.transform(X_test) |
| 90 | +scaler2 = StandardScaler() |
| 91 | +y_train2 = scaler2.fit_transform(np.expand_dims(y_train, -1)).ravel() |
| 92 | +y_test2 = scaler2.fit_transform(np.expand_dims(y_test, -1)).ravel() |
| 93 | + |
| 94 | +model = MLPRegressor(max_iter=500) |
| 95 | +model.fit(X_train2, y_train2) |
| 96 | + |
| 97 | + |
| 98 | +# evaluate the model's performance |
| 99 | +print(model.score(X_train2, y_train2)) |
| 100 | +print(model.score(X_test2, y_test2)) |
| 101 | +# not as good as a random forest! |
| 102 | +# but not as bad as linear regression |
0 commit comments