Keras and Tensorflow Basics in Python – Simple Example
Below is a small example showing how to utilize Keras/Tensorflow 2.0 to predict a value utilizing a small dataset. More explanations to follow in the Jupyter notebook below...
Keras / Tensorflow Basics - A Simple ExampleThe dataset utilized here is fake, for the sake of example use only. It contains a price and two "features". We're assuming the dataset is a price listing of gemstones, and based on the features we can predict what the price of a new gemstone added to the list may be.
The data can be found here.
In [1]:
#Imports
import pandas as pd
import numpy as np
import seaborn as sns
Data
In [2]:
df = pd.read_csv('Keras/fake_reg.csv')
In [3]:
df.head()
Out[3]:
price
feature1
feature2
0
461.527929
999.787558
999.766096
1
548.130011
998.861615
1001.042403
2
410.297162
1000.070267
998.844015
3
540.382220
999.952251
1000.440940
4
546.024553
1000.446011
1000.338531
In [4]:
sns.pairplot(df)
Out[4]:
<seaborn.axisgrid.PairGrid at 0x18188b92e48>
This is a very simply dataset, but the pairplot can show us how the two features may correlate to pricing.
Training the Model
In [5]:
from sklearn.model_selection import train_test_split
In [6]:
#We need .values because it's best to pass in numpy arrays due to how tensorflow works
X = df[['feature1', 'feature2']].values
y = df['price'].values
In [7]:
#Split into test/train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
In [8]:
#Scale data to be...