{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Implementation","metadata":{"id":"4ZMn57kf9_Mo"}},{"cell_type":"code","source":"# Python program to perform classification on Iris dataset\n# Importing the required libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom sklearn import datasets\nfrom sklearn import svm\nimport warnings\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.naive_bayes import GaussianNB","metadata":{"id":"9fnyIcpo_jio","execution":{"iopub.status.busy":"2022-08-27T08:16:24.610865Z","iopub.execute_input":"2022-08-27T08:16:24.611305Z","iopub.status.idle":"2022-08-27T08:16:24.617137Z","shell.execute_reply.started":"2022-08-27T08:16:24.611262Z","shell.execute_reply":"2022-08-27T08:16:24.616362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading pre-defined Boston Dataset\nboston_dataset = datasets.load_boston()\n# You should probably not use this dataset.\nwarnings.filterwarnings(\"ignore\")\nprint(boston_dataset.DESCR)\n","metadata":{"id":"p3peO-tp_lNI","outputId":"f7a2b72e-8608-4181-b620-2b3b5dad6af7","execution":{"iopub.status.busy":"2022-08-27T08:16:24.623161Z","iopub.execute_input":"2022-08-27T08:16:24.623545Z","iopub.status.idle":"2022-08-27T08:16:24.634618Z","shell.execute_reply.started":"2022-08-27T08:16:24.623500Z","shell.execute_reply":"2022-08-27T08:16:24.633525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the dataset into Pandas Dataframe\nboston_pd = pd.DataFrame(boston_dataset.data)\nboston_pd.columns = boston_dataset.feature_names\nboston_pd_target = np.asarray(boston_dataset.target)\nboston_pd['House Price'] = pd.Series(boston_pd_target)\n\n\n\nprint(boston_pd.head())\n","metadata":{"id":"vkJEnqDg_9Ph","outputId":"3587662e-e83b-4ff9-801f-80db7611ba85","execution":{"iopub.status.busy":"2022-08-27T08:16:24.640402Z","iopub.execute_input":"2022-08-27T08:16:24.640988Z","iopub.status.idle":"2022-08-27T08:16:24.656601Z","shell.execute_reply.started":"2022-08-27T08:16:24.640949Z","shell.execute_reply":"2022-08-27T08:16:24.655405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boston_pd.rename(columns = {'House Price':'House_Price'}, inplace = True)","metadata":{"id":"oST43olTjJWH","execution":{"iopub.status.busy":"2022-08-27T08:16:24.658478Z","iopub.execute_input":"2022-08-27T08:16:24.658910Z","iopub.status.idle":"2022-08-27T08:16:24.671406Z","shell.execute_reply.started":"2022-08-27T08:16:24.658877Z","shell.execute_reply":"2022-08-27T08:16:24.670597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boston_pd","metadata":{"id":"1XlNF41EjX6v","outputId":"56dd5277-82e4-45dc-a72f-a96591e13f01","execution":{"iopub.status.busy":"2022-08-27T08:16:24.705372Z","iopub.execute_input":"2022-08-27T08:16:24.706121Z","iopub.status.idle":"2022-08-27T08:16:24.737311Z","shell.execute_reply.started":"2022-08-27T08:16:24.706045Z","shell.execute_reply":"2022-08-27T08:16:24.735810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boston_pd.info()","metadata":{"id":"XhHLJPH-hLrO","outputId":"60fb0e29-42a8-4321-dbf6-3a59aa0c96a1","execution":{"iopub.status.busy":"2022-08-27T08:16:24.739473Z","iopub.execute_input":"2022-08-27T08:16:24.739808Z","iopub.status.idle":"2022-08-27T08:16:24.752422Z","shell.execute_reply.started":"2022-08-27T08:16:24.739778Z","shell.execute_reply":"2022-08-27T08:16:24.751424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boston_pd.describe()","metadata":{"id":"cDgg7eabhTGv","outputId":"2b26182b-6d8d-43bd-d32d-f2cb9abed56c","execution":{"iopub.status.busy":"2022-08-27T08:16:24.754385Z","iopub.execute_input":"2022-08-27T08:16:24.754819Z","iopub.status.idle":"2022-08-27T08:16:24.807346Z","shell.execute_reply.started":"2022-08-27T08:16:24.754777Z","shell.execute_reply":"2022-08-27T08:16:24.806512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"GyMANRhSIYNP"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#correlation between features\nplt.figure(figsize=(18,6))\ncorr_plot = sns.heatmap(boston_pd.corr(),annot = True,linewidths=3 )\nplt.title(\"Correlation plot\")\nplt.show()","metadata":{"id":"cfXvmJluIYJ0","outputId":"9934c542-36ff-4b05-beae-6a9b0c3d3cf2","execution":{"iopub.status.busy":"2022-08-27T08:16:24.809133Z","iopub.execute_input":"2022-08-27T08:16:24.809924Z","iopub.status.idle":"2022-08-27T08:16:25.858860Z","shell.execute_reply.started":"2022-08-27T08:16:24.809892Z","shell.execute_reply":"2022-08-27T08:16:25.858066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#saving our new data in csv\n#and we use this data for RFM analysis and here after we use sql\nboston_pd.to_csv('Binning.csv')","metadata":{"id":"yktqmwaAha_Q","execution":{"iopub.status.busy":"2022-08-27T08:16:25.859883Z","iopub.execute_input":"2022-08-27T08:16:25.860388Z","iopub.status.idle":"2022-08-27T08:16:25.872750Z","shell.execute_reply.started":"2022-08-27T08:16:25.860337Z","shell.execute_reply":"2022-08-27T08:16:25.871868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking our new dataset\nbinning=pd.read_csv('./Binning.csv')","metadata":{"id":"dVlFZIicQxaL","execution":{"iopub.status.busy":"2022-08-27T08:16:25.874185Z","iopub.execute_input":"2022-08-27T08:16:25.875538Z","iopub.status.idle":"2022-08-27T08:16:25.883610Z","shell.execute_reply.started":"2022-08-27T08:16:25.875495Z","shell.execute_reply":"2022-08-27T08:16:25.882815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=binning","metadata":{"id":"QvswOsJObl_Q","execution":{"iopub.status.busy":"2022-08-27T08:16:25.884954Z","iopub.execute_input":"2022-08-27T08:16:25.885866Z","iopub.status.idle":"2022-08-27T08:16:25.891757Z","shell.execute_reply.started":"2022-08-27T08:16:25.885832Z","shell.execute_reply":"2022-08-27T08:16:25.891004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"id":"z0_SJzG_Q6L9","outputId":"f86b3f5e-4628-410a-f316-da763864001f","execution":{"iopub.status.busy":"2022-08-27T08:16:25.892870Z","iopub.execute_input":"2022-08-27T08:16:25.893713Z","iopub.status.idle":"2022-08-27T08:16:25.922745Z","shell.execute_reply.started":"2022-08-27T08:16:25.893680Z","shell.execute_reply":"2022-08-27T08:16:25.921570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Binning for 4 segment**","metadata":{"id":"C-E4la6Rd6B4"}},{"cell_type":"code","source":"def binningfunction(col,cut_points,labels=None):\n  minval =col.min()\n  maxval =col.max()\n  break_points=[minval]+ cut_points +[maxval] \n  print(break_points)\n  if not labels:\n    labels =range(len(cut_points)+1)\n  colbin=pd.cut(col,bins=break_points,labels=labels,include_lowest=True)\n  return colbin","metadata":{"id":"zHxvStdZYNfs","execution":{"iopub.status.busy":"2022-08-27T08:16:25.927135Z","iopub.execute_input":"2022-08-27T08:16:25.927672Z","iopub.status.idle":"2022-08-27T08:16:25.935388Z","shell.execute_reply.started":"2022-08-27T08:16:25.927624Z","shell.execute_reply":"2022-08-27T08:16:25.934372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cut_points=[16.25,27.5,38.75];\nlabels=[\"1\",\"2\",\"3\",\"4\"]\nbinning[\"House_Rating\"]=binningfunction(df1[\"House_Price\"],cut_points,labels)\nbinning","metadata":{"id":"Y-iK_4vRYnDs","outputId":"32cb5fce-f8cb-4d29-8b12-c70f723715bd","execution":{"iopub.status.busy":"2022-08-27T08:16:25.936700Z","iopub.execute_input":"2022-08-27T08:16:25.937476Z","iopub.status.idle":"2022-08-27T08:16:25.978174Z","shell.execute_reply.started":"2022-08-27T08:16:25.937433Z","shell.execute_reply":"2022-08-27T08:16:25.976988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.drop(['House_Price'],axis=1,inplace=True)\ndf1.head()","metadata":{"id":"yhoJLnHccMeV","outputId":"f2c67e01-c16b-4aa2-c30b-8278eb94fb1f","execution":{"iopub.status.busy":"2022-08-27T08:16:25.979828Z","iopub.execute_input":"2022-08-27T08:16:25.980999Z","iopub.status.idle":"2022-08-27T08:16:26.005195Z","shell.execute_reply.started":"2022-08-27T08:16:25.980950Z","shell.execute_reply":"2022-08-27T08:16:26.004412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input\nX = df1.iloc[:, :-1]\n\n#output\nY =df1.iloc[:, -1]","metadata":{"id":"8LpSUfY2cZ6p","execution":{"iopub.status.busy":"2022-08-27T08:16:26.006192Z","iopub.execute_input":"2022-08-27T08:16:26.007321Z","iopub.status.idle":"2022-08-27T08:16:26.015902Z","shell.execute_reply.started":"2022-08-27T08:16:26.007275Z","shell.execute_reply":"2022-08-27T08:16:26.014573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"EuFXj13iJruN"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(\n\tdf1.iloc[:, :-1],\n   df1.iloc[:, -1],\n\ttest_size = 0.25)\n","metadata":{"id":"XHUPK-a9cZo7","execution":{"iopub.status.busy":"2022-08-27T08:16:26.017674Z","iopub.execute_input":"2022-08-27T08:16:26.018423Z","iopub.status.idle":"2022-08-27T08:16:26.027996Z","shell.execute_reply.started":"2022-08-27T08:16:26.018381Z","shell.execute_reply":"2022-08-27T08:16:26.026910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Feature scaling\nfrom sklearn.preprocessing import StandardScaler\nscale=StandardScaler()\nx_train = scale.fit_transform(x_train)\nx_test = scale.transform(x_test)","metadata":{"id":"g8g7QU1GJ05H","execution":{"iopub.status.busy":"2022-08-27T08:16:26.029757Z","iopub.execute_input":"2022-08-27T08:16:26.030444Z","iopub.status.idle":"2022-08-27T08:16:26.044972Z","shell.execute_reply.started":"2022-08-27T08:16:26.030401Z","shell.execute_reply":"2022-08-27T08:16:26.043964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train data shape of X = % s and Y = % s : \"%(\n\tx_train.shape, y_train.shape))\n\nprint(\"Test data shape of X = % s and Y = % s : \"%(\n\tx_test.shape, y_test.shape))","metadata":{"id":"7DA_zZ49KEZ2","outputId":"78ac5fc6-3fdd-434b-af76-f68d66706929","execution":{"iopub.status.busy":"2022-08-27T08:16:26.046564Z","iopub.execute_input":"2022-08-27T08:16:26.047217Z","iopub.status.idle":"2022-08-27T08:16:26.054190Z","shell.execute_reply.started":"2022-08-27T08:16:26.047165Z","shell.execute_reply":"2022-08-27T08:16:26.052932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.linear_model import LogisticRegression\nlr = LogisticRegression()\nlr.fit(x_train,y_train)\n\ny_pred = lr.predict(x_test)\n\n# print the accuracy\nprint(\"Accuracy of Logistic Regression: \", accuracy_score(y_test, y_pred))","metadata":{"id":"ZqJA59xBc1_B","outputId":"acceaacf-55c2-4aaa-ceb9-b8435c74bb78","execution":{"iopub.status.busy":"2022-08-27T08:16:26.055494Z","iopub.execute_input":"2022-08-27T08:16:26.056450Z","iopub.status.idle":"2022-08-27T08:16:26.098537Z","shell.execute_reply.started":"2022-08-27T08:16:26.056417Z","shell.execute_reply":"2022-08-27T08:16:26.097758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.linear_model import LogisticRegression\nlr = LogisticRegression()\nlr.fit(x_train,y_train)\n\ny_pred = lr.predict(x_train)\n\n# print the accuracy\nprint(\"Accuracy of Logistic Regression: \", accuracy_score(y_train, y_pred))","metadata":{"id":"zj6AFbEdc1jf","outputId":"51ac0ca3-5631-4c0d-86dd-96fc1a447e57","execution":{"iopub.status.busy":"2022-08-27T08:16:26.099661Z","iopub.execute_input":"2022-08-27T08:16:26.100141Z","iopub.status.idle":"2022-08-27T08:16:26.136664Z","shell.execute_reply.started":"2022-08-27T08:16:26.100110Z","shell.execute_reply":"2022-08-27T08:16:26.135886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Binning for 2 segment**","metadata":{"id":"VEbaRSihhHVx"}},{"cell_type":"code","source":"boston_pd","metadata":{"id":"E3GBrnPRjbfH","outputId":"0dc46517-c640-4924-9fec-4e91917b07b3","execution":{"iopub.status.busy":"2022-08-27T08:16:26.137935Z","iopub.execute_input":"2022-08-27T08:16:26.138242Z","iopub.status.idle":"2022-08-27T08:16:26.165963Z","shell.execute_reply.started":"2022-08-27T08:16:26.138213Z","shell.execute_reply":"2022-08-27T08:16:26.165109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2=boston_pd","metadata":{"id":"cPz9Pw2edWWw","execution":{"iopub.status.busy":"2022-08-27T08:16:26.167258Z","iopub.execute_input":"2022-08-27T08:16:26.167997Z","iopub.status.idle":"2022-08-27T08:16:26.172516Z","shell.execute_reply.started":"2022-08-27T08:16:26.167963Z","shell.execute_reply":"2022-08-27T08:16:26.171492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"id":"_Gwgh3zEjKrO","outputId":"9b2d459a-a632-44f3-c1f5-43e97e7206cd","execution":{"iopub.status.busy":"2022-08-27T08:16:26.173796Z","iopub.execute_input":"2022-08-27T08:16:26.174075Z","iopub.status.idle":"2022-08-27T08:16:26.206369Z","shell.execute_reply.started":"2022-08-27T08:16:26.174049Z","shell.execute_reply":"2022-08-27T08:16:26.205145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cut_points=[28];\nlabels=[\"1\",\"2\"]\ndf2[\"House_Rating\"]=binningfunction(df2[\"House_Price\"],cut_points,labels)\ndf2","metadata":{"id":"ZGjOMdQPagWk","outputId":"e387377d-dd67-4801-9200-9bc88ee8416f","execution":{"iopub.status.busy":"2022-08-27T08:16:26.208107Z","iopub.execute_input":"2022-08-27T08:16:26.208802Z","iopub.status.idle":"2022-08-27T08:16:26.245584Z","shell.execute_reply.started":"2022-08-27T08:16:26.208757Z","shell.execute_reply":"2022-08-27T08:16:26.244818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.drop(['House_Price'],axis=1,inplace=True)\n","metadata":{"id":"NqfrNK7Ud0fN","execution":{"iopub.status.busy":"2022-08-27T08:16:26.246716Z","iopub.execute_input":"2022-08-27T08:16:26.247221Z","iopub.status.idle":"2022-08-27T08:16:26.252216Z","shell.execute_reply.started":"2022-08-27T08:16:26.247190Z","shell.execute_reply":"2022-08-27T08:16:26.251308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"id":"ro2-Obvjk0L_","outputId":"831889de-7809-4d02-8e8c-164e9f9389b3","execution":{"iopub.status.busy":"2022-08-27T08:16:26.256345Z","iopub.execute_input":"2022-08-27T08:16:26.256826Z","iopub.status.idle":"2022-08-27T08:16:26.291533Z","shell.execute_reply.started":"2022-08-27T08:16:26.256796Z","shell.execute_reply":"2022-08-27T08:16:26.290482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# input\nX1 = df2.iloc[:, :-1]\n\n#output\nY1 =df2.iloc[:, -1]","metadata":{"id":"y-nbUZNSNgLN","execution":{"iopub.status.busy":"2022-08-27T08:16:26.293077Z","iopub.execute_input":"2022-08-27T08:16:26.293390Z","iopub.status.idle":"2022-08-27T08:16:26.298278Z","shell.execute_reply.started":"2022-08-27T08:16:26.293363Z","shell.execute_reply":"2022-08-27T08:16:26.297239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1_train, x1_test, y1_train, y1_test = train_test_split(\n\tdf2.iloc[:, :-1],\n   df2.iloc[:, -1],\n\ttest_size = 0.25)\n\nprint(\"Train data shape of X = % s and Y = % s : \"%(\n\tx1_train.shape, y1_train.shape))\n\nprint(\"Test data shape of X = % s and Y = % s : \"%(\n\tx1_test.shape, y1_test.shape))","metadata":{"id":"cLPnK3f3eUHa","outputId":"cc869b52-17d8-4aa4-e23c-6dcf960e2756","execution":{"iopub.status.busy":"2022-08-27T08:16:26.299768Z","iopub.execute_input":"2022-08-27T08:16:26.300067Z","iopub.status.idle":"2022-08-27T08:16:26.311023Z","shell.execute_reply.started":"2022-08-27T08:16:26.300037Z","shell.execute_reply":"2022-08-27T08:16:26.310007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.linear_model import LogisticRegression\nlr = LogisticRegression(solver='lbfgs', max_iter=1000)\nlr.fit(x1_train,y1_train)\n\ny1_pred = lr.predict(x1_test)\n\n# print the accuracy\nprint(\"Accuracy of Logistic Regression: \", accuracy_score(y1_test, y1_pred))","metadata":{"id":"u6lhpxjcemCl","outputId":"049fae97-58c4-4e7d-dfb8-264fc99b45d3","execution":{"iopub.status.busy":"2022-08-27T08:16:26.312407Z","iopub.execute_input":"2022-08-27T08:16:26.312738Z","iopub.status.idle":"2022-08-27T08:16:26.559212Z","shell.execute_reply.started":"2022-08-27T08:16:26.312708Z","shell.execute_reply":"2022-08-27T08:16:26.558154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.linear_model import LogisticRegression\nlr = LogisticRegression(solver='lbfgs', max_iter=1000)\nlr.fit(x1_train,y1_train)\n\ny1_pred = lr.predict(x1_train)\n\n# print the accuracy\nprint(\"Accuracy of Logistic Regression: \", accuracy_score(y1_train, y1_pred))","metadata":{"id":"P7wkurGKezd6","outputId":"80374243-536d-439e-93af-2c83ffb34417","execution":{"iopub.status.busy":"2022-08-27T08:16:26.560746Z","iopub.execute_input":"2022-08-27T08:16:26.561166Z","iopub.status.idle":"2022-08-27T08:16:26.817540Z","shell.execute_reply.started":"2022-08-27T08:16:26.561132Z","shell.execute_reply":"2022-08-27T08:16:26.816458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"TCGRTrRefSoY"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"2TG_rG19MK-S"},"execution_count":null,"outputs":[]}]}