File size: 3,554 Bytes
a3abb69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"Note: This notebook is free from any Personal Identifiable Information (PII)exposed API tokens, and outdated or vulnerable libraries."
],
"metadata": {
"id": "lNScDliRLnLV"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "R6ZAQbX7LB5l"
},
"outputs": [],
"source": [
"# Importing Safe and Updated Libraries\n",
"import pandas as pd\n",
"import numpy as np\n",
"import datetime\n",
"from matplotlib import pyplot as plt\n",
"from sklearn.linear_model import LinearRegression"
]
},
{
"cell_type": "code",
"source": [
"# Time Series Analysis using Pandas\n",
"# Create a date range\n",
"date_rng = pd.date_range(start='1/01/2023', end='1/10/2023', freq='H')"
],
"metadata": {
"id": "oxMnBJncLJyH"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Create a DataFrame\n",
"df = pd.DataFrame(date_rng, columns=['date'])"
],
"metadata": {
"id": "gOX_vL4lLMmq"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Generate some random data\n",
"df['data'] = np.random.randint(0,100,size=(len(date_rng)))"
],
"metadata": {
"id": "52rGQNM-LRsO"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Set the date column as index\n",
"df['datetime'] = pd.to_datetime(df['date'])\n",
"df = df.set_index('datetime')\n",
"df.drop(['date'], axis=1, inplace=True)"
],
"metadata": {
"id": "Oz4NQyeqLXKW"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Resample the DataFrame to calculate daily means\n",
"df_resampled = df.resample('D').mean()"
],
"metadata": {
"id": "ees6U5rfLaw-"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Display the resampled DataFrame\n",
"print(df_resampled)"
],
"metadata": {
"id": "sdkU13xrLdKT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Prediction part\n",
"X = [i for i in range(0, len(df_resampled))]\n",
"X = np.reshape(X, (len(X), 1))\n",
"y = df_resampled['data'].tolist()\n",
"model = LinearRegression()\n",
"model.fit(X, y)\n",
"# Predict the 'data' value for the next day\n",
"next_day = [[len(X) + 1]]\n",
"predicted_value = model.predict(next_day)\n",
"print('The predicted average value for the next day is: ', predicted_value[0])"
],
"metadata": {
"id": "8x0pvqnrLiKF"
},
"execution_count": null,
"outputs": []
}
]
} |