Files
tunmnlu/task_2/others-answer/DVA-CSE6242-Spring2021-main/hw4-ML/hw4-skeleton/Q3/Untitled.ipynb
louiscklaw 9035c1312b update,
2025-02-01 02:09:32 +08:00

251 lines
5.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5600f60e",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import time\n",
"import gc\n",
"import random\n",
"from sklearn.model_selection import cross_val_score, GridSearchCV, cross_validate, train_test_split\n",
"from sklearn.metrics import accuracy_score, classification_report\n",
"from sklearn.svm import SVC\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.preprocessing import StandardScaler, normalize\n",
"from sklearn.decomposition import PCA\n",
"from sklearn.impute import SimpleImputer\n",
"\n",
"df = pd.read_csv(\"data/pima-indians-diabetes.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba9ed7e8",
"metadata": {},
"outputs": [],
"source": [
"x_data = df.filter(regex='x\\d')\n",
"y_data = df.filter(regex='y')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4e150ff2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>y</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>763</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>764</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>765</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>766</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>767</th>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>768 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" y\n",
"0 1\n",
"1 0\n",
"2 1\n",
"3 0\n",
"4 1\n",
".. ..\n",
"763 0\n",
"764 0\n",
"765 0\n",
"766 1\n",
"767 0\n",
"\n",
"[768 rows x 1 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e2870990",
"metadata": {},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, train_size=0.7,test_size=0.3, random_state= 614, shuffle = True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9b3459f7",
"metadata": {},
"outputs": [],
"source": [
"linear_regression_classifier = LinearRegression().fit(x_train, y_train)\n",
"y_predict_train = linear_regression_classifier.predict(x_train)\n",
"y_predict_test = linear_regression_classifier.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d57b40fb",
"metadata": {},
"outputs": [],
"source": [
"y_predict_train = [i.round() for i in y_predict_train]\n",
"train_accuracy = accuracy_score(y_train,y_predict_train)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "af7edfc2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7821229050279329"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_accuracy"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "4209f55e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7821229050279329"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_accuracy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "682449cc",
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}