Challenge

Project 1

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score,confusion_matrix"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"crash_data = pd.read_csv('heart.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(array([0, 1], dtype=int64), array([ 96, 207], dtype=int64))\n",
"['age', 'sex', 'cp', 'trtbps', 'chol', 'fbs', 'restecg', 'thalachh', 'exng', 'oldpeak', 'slp', 'caa', 'thall', 'output']\n"
]
}
],
"source": [
"print(np.unique(crash_data['sex'],return_counts=True))\n",
"\n",
"# Storing the column names\n",
"crash_data_columns_list = list(crash_data.columns)\n",
"\n",
"print(crash_data_columns_list)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['thalachh', 'cp', 'fbs', 'oldpeak', 'slp', 'trtbps', 'age', 'restecg', 'chol', 'output', 'thall', 'exng', 'caa']\n",
"[1 1 0 1 0 1 0 1 1 1 1 0 1 1 0 0 0 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 1 1 1 0 0\n",
" 1 0 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 1 0 1 0 1 1 1 1\n",
" 0 0 1 1 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 0 0 1 0 1 1 1 1 1 0 1 1 0 1 0 0 0 0\n",
" 1 0 1 1 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 0 0\n",
" 1 1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1\n",
" 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 0 1 1 1 0 1\n",
" 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0\n",
" 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1\n",
" 0 1 0 1 1 1 0]\n"
]
}
],
"source": [
"# Separating the unput names from species\n",
"features = list(set(crash_data_columns_list)- set(['sex']))\n",
"print(features)\n",
"\n",
"# Storing the output values in y\n",
"target = list(['sex'])\n",
"y= crash_data[target].values\n",
"\n",
"y= crash_data['sex'].values\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 7 12]\n",
" [14 58]]\n",
"0.7142857142857143\n",
"Misclassified samples: 26\n"
]
}
],
"source": [
"# Storing the values from input features\n",
"x = crash_data[features].values\n",
"\n",
"# Splitting the data into train and test\n",
"train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.3,random_state=0)\n",
"\n",
"# Data scaling\n",
"\n",
"scaler= StandardScaler()\n",
"\n",
"# Fit on training set only\n",
"scaler.fit(train_x)\n",
"\n",
"# Apply transform to both the training set and the test set.\n",
"train_x = scaler.transform(train_x)\n",
"\n",
"test_x = scaler.transform(test_x)\n",
"\n",
"# Make an instance of the model\n",
"logistic = LogisticRegression()\n",
"\n",
"# Fitting the values frof x and y\n",
"logistic.fit(train_x,train_y)\n",
"\n",
"\n",
"# Prediction from test data\n",
"prediction= logistic.predict(test_x)\n",
"\n",
"\n",
"# Confusion matrix\n",
"confusion_matrix = confusion_matrix(prediction,test_y)\n",
"print(confusion_matrix)\n",
"\n",
"# Calculating the accuracy\n",
"accuracy_score= accuracy_score(prediction,test_y)\n",
"print(accuracy_score)\n",
"\n",
"\n",
"# Printing the misclassified valuesfrom prediction\n",
"print('Misclassified samples: %d' % (test_y != prediction).sum())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.