diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0102ef9..5840bda 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Import your libraries:\n" + "import pandas as pd\n", + "from sklearn.linear_model import LinearRegression\n" ] }, { @@ -37,11 +38,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn import datasets\n", + "\n", + "diabetes=datasets.load_diabetes()" ] }, { @@ -53,11 +56,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'diabetesDataset' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdiabetesDataset\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'diabetesDataset' is not defined" + ] + } + ], "source": [ - "# Your code here:\n" + "diabetesDataset.keys()" ] }, { @@ -73,13 +88,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "scrolled": false }, - "outputs": [], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'.. _diabetes_dataset:\\n\\nDiabetes dataset\\n----------------\\n\\nTen baseline variables, age, sex, body mass index, average blood\\npressure, and six blood serum measurements were obtained for each of n =\\n442 diabetes patients, as well as the response of interest, a\\nquantitative measure of disease progression one year after baseline.\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 442\\n\\n :Number of Attributes: First 10 columns are numeric predictive values\\n\\n :Target: Column 11 is a quantitative measure of disease progression one year after baseline\\n\\n :Attribute Information:\\n - age age in years\\n - sex\\n - bmi body mass index\\n - bp average blood pressure\\n - s1 tc, T-Cells (a type of white blood cells)\\n - s2 ldl, low-density lipoproteins\\n - s3 hdl, high-density lipoproteins\\n - s4 tch, thyroid stimulating hormone\\n - s5 ltg, lamotrigine\\n - s6 glu, blood sugar level\\n\\nNote: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\\n\\nSource URL:\\nhttps://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\\n\\nFor more information see:\\nBradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\\n(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)'" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], "source": [ - "# Your code here:\n" + "diabetes.DESCR\n" ] }, { @@ -97,11 +123,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "7" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ], "source": [ - "# Enter your answer here:\n" + "len(diabetesDataset.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "'numpy.ndarray' object is not callable", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcorrcoef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdiabetes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'data'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdiabetes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'target'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'numpy.ndarray' object is not callable" + ] + } + ], + "source": [ + "import numpy as np\n", + "np.corrcoef(diabetes['data'], diabetes['target'](10))" ] }, { @@ -115,11 +174,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(442, 10)" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ], "source": [ - "# Your code here:\n" + "diabetes.data.shape\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(442,)" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "diabetes.target.shape" ] }, { @@ -156,11 +246,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn import linear_model\n" ] }, { @@ -172,11 +262,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "diabetes_model=LinearRegression()\n" ] }, { @@ -190,11 +280,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn.model_selection import train_test_split as tts \n", + "X=diabetes.data\n", + "y=diabetes.target\n", + "diabetes_data_train, diabetes_data_test, diabetes_target_train, diabetes_target_test = tts(X, y, test_size=.2)" ] }, { @@ -206,11 +299,86 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ], + "source": [ + "linreg=LinearRegression()\n", + "\n", + "linreg.fit(diabetes_data_train, diabetes_target_train)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.5543949051717935 0.2774931945160769\n" + ] + } + ], + "source": [ + "train_score=linreg.score(diabetes_data_train, diabetes_target_train) # R2\n", + "test_score=linreg.score(diabetes_data_test, diabetes_target_test)\n", + "\n", + "print (train_score, test_score)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 41.95923724, -249.55201413, 568.45047278, 345.0558147 ,\n", + " -1141.3310399 , 714.25721517, 335.99944096, 347.42634856,\n", + " 781.00034584, 105.82760756])" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], "source": [ - "# Your code here:\n" + "linreg.coef_" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "152.76257183439594" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "linreg.intercept_\n" ] }, { @@ -231,11 +399,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.5543949051717935 0.2774931945160769\n" + ] + } + ], + "source": [ + "train_score=linreg.score(diabetes_data_train, diabetes_target_train) # R2\n", + "test_score=linreg.score(diabetes_data_test, diabetes_target_test)\n", + "\n", + "print (train_score, test_score)\n" ] }, { @@ -250,9 +429,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Your code here:\n" - ] + "source": [] }, { "cell_type": "markdown", @@ -263,7 +440,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -302,7 +479,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -326,7 +503,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -351,11 +528,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto=pd.read_csv('/Users/mikel/Documents/week6/lab_supervised_sklearn/auto-mpg.csv')\n" ] }, { @@ -367,11 +544,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " mpg cylinders displacement horse_power weight acceleration \\\n", + "0 18.0 8 307.0 130.0 3504 12.0 \n", + "1 15.0 8 350.0 165.0 3693 11.5 \n", + "2 18.0 8 318.0 150.0 3436 11.0 \n", + "3 16.0 8 304.0 150.0 3433 12.0 \n", + "4 17.0 8 302.0 140.0 3449 10.5 \n", + "\n", + " model_year car_name \n", + "0 70 \\t\"chevrolet chevelle malibu\" \n", + "1 70 \\t\"buick skylark 320\" \n", + "2 70 \\t\"plymouth satellite\" \n", + "3 70 \\t\"amc rebel sst\" \n", + "4 70 \\t\"ford torino\" " + ], + "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
mpgcylindersdisplacementhorse_powerweightaccelerationmodel_yearcar_name
018.08307.0130.0350412.070\\t\"chevrolet chevelle malibu\"
115.08350.0165.0369311.570\\t\"buick skylark 320\"
218.08318.0150.0343611.070\\t\"plymouth satellite\"
316.08304.0150.0343312.070\\t\"amc rebel sst\"
417.08302.0140.0344910.570\\t\"ford torino\"
\n
" + }, + "metadata": {}, + "execution_count": 18 + } + ], + "source": [ + "auto.head()\n" ] }, { @@ -383,11 +584,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "mpg float64\n", + "cylinders int64\n", + "displacement float64\n", + "horse_power float64\n", + "weight int64\n", + "acceleration float64\n", + "model_year int64\n", + "car_name object\n", + "dtype: object" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ], + "source": [ + "auto.dtypes\n" ] }, { @@ -399,11 +619,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "82 70\n" + ] + } + ], "source": [ - "# Your code here:\n" + "print(auto.model_year.max(), auto.model_year.min())\n" ] }, { @@ -415,11 +643,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "horse_power 6\n", + "dtype: int64" + ] + }, + "metadata": {}, + "execution_count": 21 + } + ], + "source": [ + "nan_cols=auto.isna().sum()\n", + "\n", + "nan_cols[nan_cols>0]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto.horse_power=auto.horse_power.fillna(auto.horse_power.mean())" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "Series([], dtype: int64)" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ], + "source": [ + "nan_cols=auto.isna().sum()\n", + "\n", + "nan_cols[nan_cols>0]\n" ] }, { @@ -431,11 +705,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4 204\n", + "8 103\n", + "6 84\n", + "3 4\n", + "5 3\n", + "Name: cylinders, dtype: int64" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ], + "source": [ + "auto.cylinders.value_counts()\n" ] }, { @@ -451,11 +741,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto.drop(columns='car_name', inplace=True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " mpg cylinders displacement horse_power weight acceleration \\\n", + "0 18.0 8 307.0 130.0 3504 12.0 \n", + "1 15.0 8 350.0 165.0 3693 11.5 \n", + "2 18.0 8 318.0 150.0 3436 11.0 \n", + "3 16.0 8 304.0 150.0 3433 12.0 \n", + "4 17.0 8 302.0 140.0 3449 10.5 \n", + "\n", + " model_year \n", + "0 70 \n", + "1 70 \n", + "2 70 \n", + "3 70 \n", + "4 70 " + ], + "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
mpgcylindersdisplacementhorse_powerweightaccelerationmodel_year
018.08307.0130.0350412.070
115.08350.0165.0369311.570
218.08318.0150.0343611.070
316.08304.0150.0343312.070
417.08302.0140.0344910.570
\n
" + }, + "metadata": {}, + "execution_count": 26 + } + ], + "source": [ + "auto.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from sklearn.model_selection import train_test_split\n", + "x_columns = [col for col in auto.columns.values if col != \"mpg\"]\n", + "X= auto[x_columns]\n", + "y= auto[\"mpg\"]\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" ] }, { @@ -469,11 +806,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ], + "source": [ + "from sklearn import linear_model\n", + "linreg=LinearRegression()\n", + "\n", + "linreg.fit(X_train, y_train)\n" ] }, { @@ -502,11 +853,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.8038965489657075 0.8244069426990217\n" + ] + } + ], + "source": [ + "train_score=linreg.score(X_train, y_train) # R2\n", + "test_score=linreg.score(X_test, y_test)\n", + "\n", + "print (train_score, test_score)\n" ] }, { @@ -712,9 +1074,8 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" + "name": "python385jvsc74a57bd0e134e05457d34029b6460cd73bbf1ed73f339b5b6d98c95be70b69eba114fe95", + "display_name": "Python 3.8.5 64-bit ('base': conda)" }, "language_info": { "codemirror_mode": { @@ -726,9 +1087,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file