Add files via upload

This commit is contained in:
fenago authored and GitHub committed 2022-07-25 11:07:52 -07:00
1 parent eda8fb9fc0
commit ecc653cf2c
1 file changed
+1
+1
View File
@@ -0,0 +1 @@
{"metadata":{"orig_nbformat":4,"kernelspec":{"display_name":"Python 3 (ipykernel)","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.10"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"code","source":"#Import your Libraries\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nfrom sklearn.linear_model import LinearRegression\nimport sklearn.metrics as metrics\n%matplotlib inline","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"2dac3d66-b2d4-49df-8540-82d05eeff307"},{"cell_type":"code","source":"# %%timeit -n 1\n# Load your data -- start with data.csv... then Life Expectancy - and then anyone you choose\n# Replace with your dataset... for instance - if it is on github -use: https://raw.githubusercontent.com/fenago/introml/main/Life%20Expectancy%20Data.csv\ndf = pd.read_csv('data.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"c21f8a1a-a9d5-4277-868f-bef4e51785a9"},{"cell_type":"code","source":"len(df)","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"823b6b3a-c279-4b3e-9b75-75ab01f8ed0b"},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"dfdc07fd-82d6-411c-bf4e-4012315538e1"},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"4f5bc2f4-a159-4a24-b02d-cf234b7daefa"},{"cell_type":"code","source":"df.nunique()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"960afc07-cb45-4851-aa9b-6a564d943273"},{"cell_type":"code","source":"df.corr()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"17fa6add-bdac-4d33-8b96-e102f3d13bc5"},{"cell_type":"code","source":"# Basic Data Cleaning\ndf.columns = df.columns.str.lower().str.replace(' ', '_') # A\n \nstring_columns = list(df.dtypes[df.dtypes == 'object'].index) # B\n \nfor col in string_columns:\n df[col] = df[col].str.lower().str.replace(' ', '_') # C","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"03c95958-f524-486f-84dd-62eccffe2985"},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"5694febc-4570-4e82-a40a-4e347e468eb6"},{"cell_type":"code","source":"df.head().T","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"cfd18cea-dbe5-4ac0-864c-922704a8f46a"},{"cell_type":"code","source":"# Replace with your target variable --- df.YOUR_TARGET_VARIABLE \n# Also replace your X label\nplt.figure(figsize=(6, 4))\n\nsns.histplot(df.life_expectancy_, bins=40, color='black', alpha=1)\nplt.ylabel('Frequency')\nplt.xlabel('life_expectancy_')\nplt.title('Distribution of prices')\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"227d8604-3160-4070-a1b8-b7c2e71ce771"},{"cell_type":"code","source":"# This may not be needed for your dataset but explore with different features\nplt.figure(figsize=(6, 4))\n\nsns.histplot(df.msrp[df.msrp < 100000], bins=40, color='black', alpha=1)\nplt.ylabel('Frequency')\nplt.xlabel('Price')\nplt.title('Distribution of prices')\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"bfc72552-0bc0-4da4-b4d9-b43d4ca28bbf"},{"cell_type":"code","source":"# This may not be needed for your dataset but explore with different features\n\nlog_price = np.log1p(df.msrp)\n\nplt.figure(figsize=(6, 4))\n\nsns.histplot(log_price, bins=40, color='black', alpha=1)\nplt.ylabel('Frequency')\nplt.xlabel('Log(Price + 1)')\nplt.title('Distribution of prices after log tranformation')\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"dfe5a4c0-af19-44f8-a155-a6f36ca2ea2d"},{"cell_type":"code","source":"# Check for nulls --- you do NOT want nulls when you train\ndf.isnull().sum()","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"4ad70d6b-e458-4bb7-be22-f550a07aed9f"},{"cell_type":"code","source":"df.head().T","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"a8a3347d-5141-4b92-bb07-3dc2dbc1bf3a"},{"cell_type":"code","source":"#delete columns --- this may or may NOT be needed. As before - skip if you don't need it\n# You will encounter times where you will want to delete columns. This is how you do that.\ndf = df.drop(['x5_latitude', 'x6_longitude', 'x1_transaction_date'], axis=1)\ndf","metadata":{"trusted":true},"execution_count":null,"outputs":[],"id":"2871e2ab-8677-43b4-bba1-36fe991438ab"},{"cell_type":"code","source":"'''\n# Split the data into test, train, validation sets... 60/20/20\nfrom sklearn.model_selection import train_test_split\n# This gives the 80/20 train test split\ndf_train_full, df_test = train_test_split(df, test_size=0.2, random_state=1Line truncated