Skip to content

Commit

Permalink
Perceptron, MLPs Examples added
Browse files Browse the repository at this point in the history
  • Loading branch information
sayedgamal99 committed Jun 13, 2024
1 parent fce7502 commit dcb6ab1
Showing 1 changed file with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CH10 Introduction to Artificial Neural Networks with Keras"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"example of using Perceptron:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.linear_model import Perceptron\n",
"from sklearn.datasets import load_iris\n",
"\n",
"iris = load_iris(as_frame=True)\n",
"X, y = iris.data[[\"petal length (cm)\", \"petal width (cm)\"]\n",
" ].values, (iris.target == 0)\n",
"\n",
"per_clf = Perceptron(random_state=34)\n",
"per_clf.fit(X, y)\n",
"\n",
"X_new = [[2, 0.5], [3, 1]]\n",
"y_pred = per_clf.predict(X_new)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, False])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_pred"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"example of using MLPs (multi-layer-perceptron)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\sayed\\anaconda3\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.\n",
" warnings.warn(\n"
]
},
{
"data": {
"text/plain": [
"0.5324025638801033"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.datasets import fetch_california_housing\n",
"from sklearn.metrics import mean_squared_error\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neural_network import MLPRegressor\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"housing = fetch_california_housing()\n",
"X_train, xtemp, y_train, ytemp = train_test_split(\n",
" housing.data, housing.target, test_size=.4, random_state=34)\n",
"X_valid, X_test, y_valid, y_test = train_test_split(\n",
" xtemp, ytemp, test_size=.5, random_state=3)\n",
"\n",
"mlp = MLPRegressor(hidden_layer_sizes=[50, 50, 50], random_state=3)\n",
"pipeline = make_pipeline(StandardScaler(), mlp)\n",
"pipeline.fit(X_train, y_train)\n",
"y_pred = pipeline.predict(X_valid)\n",
"rmse = mean_squared_error(y_valid, y_pred, squared=False)\n",
"rmse"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit dcb6ab1

Please sign in to comment.