#####
Feb 24, 2016 ·
post

# "Hello world" in Keras (or, Scikit-learn versus Keras)

```
>>> %matplotlib inline
>>> import seaborn as sns
>>> import numpy as np
>>> from sklearn.cross_validation import train_test_split
>>> from sklearn.linear_model import LogisticRegressionCV
>>> from keras.models import Sequential
>>> from keras.layers.core import Dense, Activation
>>> from keras.utils import np_utils
```

```
>>> iris = sns.load_dataset("iris")
>>> iris.head()
```

```
>>> sns.pairplot(iris, hue='species')
```

```
>>> X = iris.values[:, 0:4]
>>> y = iris.values[:, 4]
```

```
>>> train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.5, random_state=0)
```

```
>>> lr = LogisticRegressionCV()
>>> lr.fit(train_X, train_y)
```

```
>>> pred_y = lr.predict(test_X)
>>> print("Test fraction correct (Accuracy) = {:.2f}".format(lr.score(test_X, test_y)))
# Test fraction correct (Accuracy) = 0.83
```

```
setosa
versicolor
setosa
virginica
...
```

```
setosa versicolor virginica
1 0 0
0 1 0
1 0 0
0 0 1
```

```
>>> def one_hot_encode_object_array(arr):
'''One hot encode a numpy array of objects (e.g. strings)'''
uniques, ids = np.unique(arr, return_inverse=True)
return np_utils.to_categorical(ids, len(uniques))
>>> train_y_ohe = one_hot_encode_object_array(train_y)
>>> test_y_ohe = one_hot_encode_object_array(test_y)
```

```
>>> model = Sequential()
>>> model.add(Dense(16, input_shape=(4,)))
>>> model.add(Activation('sigmoid'))
>>> model.add(Dense(3))
>>> model.add(Activation('softmax'))
>>> model.compile(loss='categorical_crossentropy', optimizer='adam')
```

```
>>> model.fit(train_X, train_y_ohe, verbose=0, batch_size=1)
```

```
>>> loss, accuracy = model.evaluate(test_X, test_y_ohe, show_accuracy=True, verbose=0)
>>> print("Test fraction correct (Accuracy) = {:.2f}".format(accuracy))
# Test fraction correct (Accuracy) = 0.99
```