{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Data Preprocessing\n", "\n", "# Importing the libraries\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Importing the dataset\n", "url ='https://raw.githubusercontent.com/cagriemreakin/Machine-Learning/master/1-%20Data%20Preprocessing/dataset.csv'\n", "dataset = pd.read_csv(url)\n", "X = dataset.iloc[:, :-1].values\n", "y = dataset.iloc[:, 3].values" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Dealing with missing data\n", "from sklearn.preprocessing import Imputer #Imputer Class' ının yüklenmesi\n", "\n", "# Missing_values : Hangi değerler üzerinde işlem yapmak istediğimizi belirtme\n", "# Strategy: Bu değerleri ne ile değiştirmek istediğimiz.\n", "# axis= satır mı sütun mu\n", "imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0) \n", "# Veri setimizde NaN değere sahip sütunlar; Age 1. Salary ise 2. eleman olduğundan\n", "\n", "imputer = imputer.fit(X[:, 1:3])\n", "X[:, 1:3] = imputer.transform(X[:, 1:3])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['France', 44.0, 72000.0],\n", " ['Spain', 27.0, 48000.0],\n", " ['Germany', 30.0, 54000.0],\n", " ['Spain', 38.0, 61000.0],\n", " ['Germany', 40.0, 63777.77777777778],\n", " ['France', 35.0, 58000.0],\n", " ['Spain', 38.77777777777778, 52000.0],\n", " ['France', 48.0, 79000.0],\n", " ['Germany', 50.0, 83000.0],\n", " ['France', 37.0, 67000.0]], dtype=object)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yukarıda X[0] sütunundaki değerlerin kategorik olduğunu görüyoruz.Kategorik değerlerden kurtulmak için LabelEncoder class' ını kullanrak bu labellar için değer atamamız gerekli." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 44.0, 72000.0],\n", " [2, 27.0, 48000.0],\n", " [1, 30.0, 54000.0],\n", " [2, 38.0, 61000.0],\n", " [1, 40.0, 63777.77777777778],\n", " [0, 35.0, 58000.0],\n", " [2, 38.77777777777778, 52000.0],\n", " [0, 48.0, 79000.0],\n", " [1, 50.0, 83000.0],\n", " [0, 37.0, 67000.0]], dtype=object)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Encoding categorical data\n", "from sklearn.preprocessing import LabelEncoder\n", "labelencoder_X = LabelEncoder()\n", "X[:, 0] = labelencoder_X.fit_transform(X[:, 0])\n", "X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "X[0] sütunundaki değerlerin France:0, Spain:1, Germany:2 ile değiştiğini gördük.Fakat bu da başka bir problemi meydana getirdi. Makine öğrenmesi algoritmaları matematiksel formuller ile çalıştığından hesaplamlarda bu değerleri kullanacak ve yanlış sonuçlar çıkarmamıza sebep olacaktır. Aslında yukarıdaki işlemi yaptığımızda şöyle bir varsayım yapmış olduk:

Germany > Spain > France


Bu durumun üstesinden gelmek için OneHotEncoder kullanmamız gerekiyor. Onuda kısaca çöyle açıklayalım.
\n", "One-hot Encoding: Ana fikir etiketin olduğu sütun için (X[0]) dummy features yaratmak ve binary olarak göstermek. Aşağıdaki tablodan görsel olarak daha iyi anlaşılabilir.He satırdaki label baktık ve var mı yok mu işaretledi. France 1.satırda var 1 yazdık fakat Spain ve Germany olmadığı için = yazdık.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
FranceSpainGermany
0100
1010
2001
3010
4001
5100
6010
7100
8001
9100
\n", "
" ], "text/plain": [ " France Spain Germany\n", "0 1 0 0\n", "1 0 1 0\n", "2 0 0 1\n", "3 0 1 0\n", "4 0 0 1\n", "5 1 0 0\n", "6 0 1 0\n", "7 1 0 0\n", "8 0 0 1\n", "9 1 0 0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame([ [1,0,0],\n", " [0,1,0],\n", " [0,0,1],\n", " [0,1,0],\n", " [0,0,1],\n", " [1,0,0],\n", " [0,1,0],\n", " [1,0,0],\n", " [0,0,1],\n", " [1,0,0]])\n", "df.columns = ['France','Spain','Germany']\n", "df" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from sklearn.preprocessing import OneHotEncoder\n", "ohe = OneHotEncoder(categorical_features = [0])\n", "X = ohe.fit_transform(X).toarray()\n", "\n", "# Diğer kategorik değer ise, dependent variable olan Purchase sütunu \n", "labelencoder_y = LabelEncoder()\n", "y = labelencoder_y.fit_transform(y)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 4.40000000e+01, 7.20000000e+04],\n", " [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,\n", " 2.70000000e+01, 4.80000000e+04],\n", " [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,\n", " 3.00000000e+01, 5.40000000e+04],\n", " [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,\n", " 3.80000000e+01, 6.10000000e+04],\n", " [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,\n", " 4.00000000e+01, 6.37777778e+04],\n", " [ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 3.50000000e+01, 5.80000000e+04],\n", " [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,\n", " 3.87777778e+01, 5.20000000e+04],\n", " [ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 4.80000000e+01, 7.90000000e+04],\n", " [ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,\n", " 5.00000000e+01, 8.30000000e+04],\n", " [ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 3.70000000e+01, 6.70000000e+04]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 0, 0, 1, 1, 0, 1, 0, 1], dtype=int64)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }