{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.00513Z","iopub.execute_input":"2023-05-19T15:32:55.005607Z","iopub.status.idle":"2023-05-19T15:32:55.012626Z","shell.execute_reply.started":"2023-05-19T15:32:55.005565Z","shell.execute_reply":"2023-05-19T15:32:55.01113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/customer/customer.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.015506Z","iopub.execute_input":"2023-05-19T15:32:55.016027Z","iopub.status.idle":"2023-05-19T15:32:55.040972Z","shell.execute_reply.started":"2023-05-19T15:32:55.015973Z","shell.execute_reply":"2023-05-19T15:32:55.039888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.043579Z","iopub.execute_input":"2023-05-19T15:32:55.044828Z","iopub.status.idle":"2023-05-19T15:32:55.061582Z","shell.execute_reply.started":"2023-05-19T15:32:55.044768Z","shell.execute_reply":"2023-05-19T15:32:55.060371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['age'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.063234Z","iopub.execute_input":"2023-05-19T15:32:55.064317Z","iopub.status.idle":"2023-05-19T15:32:55.083037Z","shell.execute_reply.started":"2023-05-19T15:32:55.064125Z","shell.execute_reply":"2023-05-19T15:32:55.080955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['gender'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.087134Z","iopub.execute_input":"2023-05-19T15:32:55.087961Z","iopub.status.idle":"2023-05-19T15:32:55.102406Z","shell.execute_reply.started":"2023-05-19T15:32:55.087917Z","shell.execute_reply":"2023-05-19T15:32:55.098478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['review'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.104451Z","iopub.execute_input":"2023-05-19T15:32:55.106088Z","iopub.status.idle":"2023-05-19T15:32:55.121725Z","shell.execute_reply.started":"2023-05-19T15:32:55.106011Z","shell.execute_reply":"2023-05-19T15:32:55.119468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['education'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.123894Z","iopub.execute_input":"2023-05-19T15:32:55.12518Z","iopub.status.idle":"2023-05-19T15:32:55.137275Z","shell.execute_reply.started":"2023-05-19T15:32:55.12512Z","shell.execute_reply":"2023-05-19T15:32:55.13597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['purchased'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.138975Z","iopub.execute_input":"2023-05-19T15:32:55.13977Z","iopub.status.idle":"2023-05-19T15:32:55.153271Z","shell.execute_reply.started":"2023-05-19T15:32:55.139717Z","shell.execute_reply":"2023-05-19T15:32:55.151959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:32:55.155187Z","iopub.execute_input":"2023-05-19T15:32:55.156637Z","iopub.status.idle":"2023-05-19T15:32:55.167706Z","shell.execute_reply.started":"2023-05-19T15:32:55.156577Z","shell.execute_reply":"2023-05-19T15:32:55.166173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,0:4]\ny=df['purchased']","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:38:14.766053Z","iopub.execute_input":"2023-05-19T15:38:14.767305Z","iopub.status.idle":"2023-05-19T15:38:14.774974Z","shell.execute_reply.started":"2023-05-19T15:38:14.767203Z","shell.execute_reply":"2023-05-19T15:38:14.773874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:39:15.459981Z","iopub.execute_input":"2023-05-19T15:39:15.460497Z","iopub.status.idle":"2023-05-19T15:39:15.466832Z","shell.execute_reply.started":"2023-05-19T15:39:15.460454Z","shell.execute_reply":"2023-05-19T15:39:15.465166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:40:56.99681Z","iopub.execute_input":"2023-05-19T15:40:56.997264Z","iopub.status.idle":"2023-05-19T15:40:57.005962Z","shell.execute_reply.started":"2023-05-19T15:40:56.997224Z","shell.execute_reply":"2023-05-19T15:40:57.004512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:41:07.049569Z","iopub.execute_input":"2023-05-19T15:41:07.050717Z","iopub.status.idle":"2023-05-19T15:41:07.065959Z","shell.execute_reply.started":"2023-05-19T15:41:07.050634Z","shell.execute_reply":"2023-05-19T15:41:07.064343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:41:07.686033Z","iopub.execute_input":"2023-05-19T15:41:07.687253Z","iopub.status.idle":"2023-05-19T15:41:07.694775Z","shell.execute_reply.started":"2023-05-19T15:41:07.687176Z","shell.execute_reply":"2023-05-19T15:41:07.692921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oe = OrdinalEncoder()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:56:29.288436Z","iopub.execute_input":"2023-05-19T15:56:29.288892Z","iopub.status.idle":"2023-05-19T15:56:29.294838Z","shell.execute_reply.started":"2023-05-19T15:56:29.288854Z","shell.execute_reply":"2023-05-19T15:56:29.293363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the encoder using x_train\noe.fit(x_train)\n\n# Transform x_train using the fitted encoder\nx_train_encoded = oe.transform(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:56:47.193485Z","iopub.execute_input":"2023-05-19T15:56:47.19402Z","iopub.status.idle":"2023-05-19T15:56:47.204413Z","shell.execute_reply.started":"2023-05-19T15:56:47.193976Z","shell.execute_reply":"2023-05-19T15:56:47.202844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories = [['Poor','Average','Good'], ['School','UG','PG']]\noe = OrdinalEncoder(categories=categories)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T16:00:13.220294Z","iopub.execute_input":"2023-05-19T16:00:13.221679Z","iopub.status.idle":"2023-05-19T16:00:13.228641Z","shell.execute_reply.started":"2023-05-19T16:00:13.221621Z","shell.execute_reply":"2023-05-19T16:00:13.22674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oe.categories_","metadata":{"execution":{"iopub.status.busy":"2023-05-19T16:00:26.575704Z","iopub.execute_input":"2023-05-19T16:00:26.576246Z","iopub.status.idle":"2023-05-19T16:00:26.601566Z","shell.execute_reply.started":"2023-05-19T16:00:26.576199Z","shell.execute_reply":"2023-05-19T16:00:26.599386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:47:49.886814Z","iopub.execute_input":"2023-05-19T15:47:49.887363Z","iopub.status.idle":"2023-05-19T15:47:49.894068Z","shell.execute_reply.started":"2023-05-19T15:47:49.887301Z","shell.execute_reply":"2023-05-19T15:47:49.892249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le=LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:48:01.130208Z","iopub.execute_input":"2023-05-19T15:48:01.130756Z","iopub.status.idle":"2023-05-19T15:48:01.137952Z","shell.execute_reply.started":"2023-05-19T15:48:01.130706Z","shell.execute_reply":"2023-05-19T15:48:01.13614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_le = le.fit_transform(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:49:10.206516Z","iopub.execute_input":"2023-05-19T15:49:10.207867Z","iopub.status.idle":"2023-05-19T15:49:10.214083Z","shell.execute_reply.started":"2023-05-19T15:49:10.207813Z","shell.execute_reply":"2023-05-19T15:49:10.212547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = le.transform(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:49:44.726206Z","iopub.execute_input":"2023-05-19T15:49:44.727794Z","iopub.status.idle":"2023-05-19T15:49:44.733519Z","shell.execute_reply.started":"2023-05-19T15:49:44.727734Z","shell.execute_reply":"2023-05-19T15:49:44.731985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le.classes_","metadata":{"execution":{"iopub.status.busy":"2023-05-19T15:55:07.105824Z","iopub.execute_input":"2023-05-19T15:55:07.106279Z","iopub.status.idle":"2023-05-19T15:55:07.115517Z","shell.execute_reply.started":"2023-05-19T15:55:07.106241Z","shell.execute_reply":"2023-05-19T15:55:07.113876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:31:40.22973Z","iopub.execute_input":"2023-05-27T18:31:40.230124Z","iopub.status.idle":"2023-05-27T18:31:40.236679Z","shell.execute_reply.started":"2023-05-27T18:31:40.230086Z","shell.execute_reply":"2023-05-27T18:31:40.235646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and preprocess the data\ndata = pd.read_csv(\"/kaggle/input/nifty-indices-dataset/NIFTY 50.csv\")  # Replace 'stock_data.csv' with your actual file name\nclosing_prices = data['Close'].values.reshape(-1, 1)\nopening_prices = data['Open'].values.reshape(-1, 1)\n\nscaler = MinMaxScaler()\nscaled_closing_prices = scaler.fit_transform(closing_prices)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:32:14.915548Z","iopub.execute_input":"2023-05-27T18:32:14.915921Z","iopub.status.idle":"2023-05-27T18:32:14.932668Z","shell.execute_reply.started":"2023-05-27T18:32:14.915887Z","shell.execute_reply":"2023-05-27T18:32:14.931609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and testing sets\ntrain_size = int(len(scaled_closing_prices) * 0.8)\ntrain_closing_prices = scaled_closing_prices[:train_size]\ntrain_opening_prices = opening_prices[:train_size]\ntest_closing_prices = scaled_closing_prices[train_size:]\ntest_opening_prices = opening_prices[train_size:]","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:32:28.068951Z","iopub.execute_input":"2023-05-27T18:32:28.069393Z","iopub.status.idle":"2023-05-27T18:32:28.075357Z","shell.execute_reply.started":"2023-05-27T18:32:28.069354Z","shell.execute_reply":"2023-05-27T18:32:28.074284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the training data\ndef prepare_data(closing_prices, opening_prices, time_steps):\n    X, y = [], []\n    for i in range(len(closing_prices) - time_steps - 1):\n        X.append(np.hstack((closing_prices[i:(i + time_steps), 0], opening_prices[i:(i + time_steps), 0])))\n        y.append(closing_prices[i + time_steps, 0])\n    return np.array(X), np.array(y)\n\ntime_steps = 10  # Adjust the number of time steps as per your preference\nX_train, y_train = prepare_data(train_closing_prices, train_opening_prices, time_steps)\nX_test, y_test = prepare_data(test_closing_prices, test_opening_prices, time_steps)\n\n# Build the LSTM model\nmodel = Sequential()\nmodel.add(LSTM(units=50, return_sequences=True, input_shape=(time_steps, 2)))\nmodel.add(LSTM(units=50))\nmodel.add(Dense(units=1))","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:33:27.429387Z","iopub.execute_input":"2023-05-27T18:33:27.429818Z","iopub.status.idle":"2023-05-27T18:33:28.181643Z","shell.execute_reply.started":"2023-05-27T18:33:27.429779Z","shell.execute_reply":"2023-05-27T18:33:28.179472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the LSTM model\nmodel = Sequential()\nmodel.add(LSTM(units=50, return_sequences=True, input_shape=(time_steps, 2)))\nmodel.add(LSTM(units=50))\nmodel.add(Dense(units=1))","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:34:01.848958Z","iopub.execute_input":"2023-05-27T18:34:01.849365Z","iopub.status.idle":"2023-05-27T18:34:02.318955Z","shell.execute_reply.started":"2023-05-27T18:34:01.849328Z","shell.execute_reply":"2023-05-27T18:34:02.318066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense\nimport matplotlib.pyplot as plt\n\n# Load and preprocess the data\ndata = pd.read_csv('/kaggle/input/nifty-indices-dataset/NIFTY 50.csv')  # Replace 'stock_data.csv' with your actual file name\nclosing_prices = data['Close'].values.reshape(-1, 1)\nopening_prices = data['Open'].values.reshape(-1, 1)\n\nscaler = MinMaxScaler()\nscaled_closing_prices = scaler.fit_transform(closing_prices)\n\n# Split the data into training and testing sets\ntrain_size = int(len(scaled_closing_prices) * 0.8)\ntrain_closing_prices = scaled_closing_prices[:train_size]\ntrain_opening_prices = opening_prices[:train_size]\ntest_closing_prices = scaled_closing_prices[train_size:]\ntest_opening_prices = opening_prices[train_size:]\n\n# Prepare the training data\ndef prepare_data(closing_prices, opening_prices, time_steps):\n    X, y = [], []\n    for i in range(len(closing_prices) - time_steps):\n        X.append(np.concatenate((closing_prices[i:(i + time_steps)], opening_prices[i:(i + time_steps)]), axis=1))\n        y.append(closing_prices[i + time_steps])\n    return np.array(X), np.array(y)\n\ntime_steps = 200  # Adjust the number of time steps as per your preference\nX_train, y_train = prepare_data(train_closing_prices, train_opening_prices, time_steps)\nX_test, y_test = prepare_data(test_closing_prices, test_opening_prices, time_steps)\n\n# Build the LSTM model\nmodel = Sequential()\nmodel.add(LSTM(units=50, return_sequences=True, input_shape=(time_steps, 2)))\nmodel.add(LSTM(units=50))\nmodel.add(Dense(units=1))\n\n# Compile and train the model\nmodel.compile(optimizer='adam', loss='mean_squared_error')\nmodel.fit(X_train, y_train, epochs=50, batch_size=32)\n\n# Make predictions\ntrain_predictions = model.predict(X_train)\ntest_predictions = model.predict(X_test)\n\n# Scale the predictions back to the original range\ntrain_predictions = scaler.inverse_transform(train_predictions)\ntest_predictions = scaler.inverse_transform(test_predictions)\n\n# Evaluate the model\ntrain_rmse = np.sqrt(np.mean((scaler.inverse_transform(y_train.reshape(-1, 1)) - train_predictions) ** 2))\ntest_rmse = np.sqrt(np.mean((scaler.inverse_transform(y_test.reshape(-1, 1)) - test_predictions) ** 2))\nprint('Train RMSE:', train_rmse)\nprint('Test RMSE:', test_rmse)\n\n# Visualize the results\nplt.plot(data['Close'].values, label='Actual Closing Prices')\nplt.plot(np.concatenate([train_predictions, test_predictions]), label='Predicted Closing Prices')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T19:16:17.02138Z","iopub.execute_input":"2023-05-27T19:16:17.022354Z","iopub.status.idle":"2023-05-27T19:16:19.868045Z","shell.execute_reply.started":"2023-05-27T19:16:17.022287Z","shell.execute_reply":"2023-05-27T19:16:19.865418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\ntrain_rmse = np.sqrt(np.mean((scaler.inverse_transform(y_train.reshape(-1, 1)) - train_predictions) ** 2))\ntest_rmse = np.sqrt(np.mean((scaler.inverse_transform(y_test.reshape(-1, 1)) - test_predictions) ** 2))\nprint('Train RMSE:', train_rmse)\nprint('Test RMSE:', test_rmse)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:43:08.334609Z","iopub.execute_input":"2023-05-27T18:43:08.33502Z","iopub.status.idle":"2023-05-27T18:43:08.345533Z","shell.execute_reply.started":"2023-05-27T18:43:08.334984Z","shell.execute_reply":"2023-05-27T18:43:08.343392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_percentage_error = (train_rmse / np.mean(scaler.inverse_transform(y_train.reshape(-1, 1)))) * 100\ntest_percentage_error = (test_rmse / np.mean(scaler.inverse_transform(y_test.reshape(-1, 1)))) * 100\n\nprint('Train Percentage Error:', train_percentage_error)\nprint('Test Percentage Error:', test_percentage_error)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:44:29.994136Z","iopub.execute_input":"2023-05-27T18:44:29.994663Z","iopub.status.idle":"2023-05-27T18:44:30.00609Z","shell.execute_reply.started":"2023-05-27T18:44:29.994588Z","shell.execute_reply":"2023-05-27T18:44:30.003708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\n\n# Load the stock price data\ndata = pd.read_csv('/kaggle/input/nifty-indices-dataset/NIFTY 50.csv')  # Replace 'stock_data.csv' with your actual file name\n\n# Split the data into features (X) and target variable (y)\nX = data[['Open']]\ny = data['Close']\n\n# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Build the Random Forest regression model\nmodel = RandomForestRegressor(n_estimators=100, random_state=42)\n\n# Train the model\nmodel.fit(X_train, y_train)\n\n# Make predictions on the training and testing sets\ntrain_predictions = model.predict(X_train)\ntest_predictions = model.predict(X_test)\n\n# Calculate the root mean squared error (RMSE)\ntrain_rmse = mean_squared_error(y_train, train_predictions, squared=False)\ntest_rmse = mean_squared_error(y_test, test_predictions, squared=False)\n\nprint('Train RMSE:', train_rmse)\nprint('Test RMSE:', test_rmse)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:46:23.748869Z","iopub.execute_input":"2023-05-27T18:46:23.749296Z","iopub.status.idle":"2023-05-27T18:46:24.702392Z","shell.execute_reply.started":"2023-05-27T18:46:23.749252Z","shell.execute_reply":"2023-05-27T18:46:24.700673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Create a figure and axis object\nfig, ax = plt.subplots(figsize=(10, 6), dpi=80)\n\n# Visualize the results\nax.plot(data['Close'].values, label='Actual Closing Prices', linewidth=2, marker='o')\nax.plot(np.concatenate([train_predictions, test_predictions]), label='Predicted Closing Prices', linewidth=2, marker='o')\n\n# Set the plot title and labels\nax.set_title('Actual vs. Predicted Closing Prices', fontsize=16)\nax.set_xlabel('Time', fontsize=12)\nax.set_ylabel('Closing Price', fontsize=12)\n\n# Show the legend\nax.legend(fontsize=12)\n\n# Adjust the layout to prevent labels from being cut off\nfig.tight_layout()\n\n# Display the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:52:10.0195Z","iopub.execute_input":"2023-05-27T18:52:10.019914Z","iopub.status.idle":"2023-05-27T18:52:10.432109Z","shell.execute_reply.started":"2023-05-27T18:52:10.019874Z","shell.execute_reply":"2023-05-27T18:52:10.430883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform necessary imports\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\n\n# Load and preprocess the data\ndata = pd.read_csv('/kaggle/input/nifty-indices-dataset/NIFTY 50.csv')  # Replace 'your_data.csv' with your actual data file\n\n# Split the data into features (X) and target (y)\nX = data[['Open']].values\ny = data['Close'].values\n\n# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Scale the features using MinMaxScaler\nscaler = MinMaxScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)\n\n# Create and train the model\nmodel = RandomForestRegressor(n_estimators=100, random_state=42)\nmodel.fit(X_train, y_train)\n\n# Make predictions on the training and testing data\ntrain_predictions = model.predict(X_train)\ntest_predictions = model.predict(X_test)\n\n# Calculate RMSE\ntrain_rmse = np.sqrt(mean_squared_error(y_train, train_predictions))\ntest_rmse = np.sqrt(mean_squared_error(y_test, test_predictions))\n\nprint(\"Train RMSE:\", train_rmse)\nprint(\"Test RMSE:\", test_rmse)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T18:54:44.024532Z","iopub.execute_input":"2023-05-27T18:54:44.024977Z","iopub.status.idle":"2023-05-27T18:54:44.529416Z","shell.execute_reply.started":"2023-05-27T18:54:44.024935Z","shell.execute_reply":"2023-05-27T18:54:44.527873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.models import Sequential\nfrom keras.layers import LSTM, Dense\n\n# Load and preprocess the data\ndata = pd.read_csv('/kaggle/input/nifty-indices-dataset/NIFTY 50.csv')  # Replace 'your_data.csv' with your actual data file\n\n# Split the data into features (X) and target (y)\nX = data[['Open']].values\ny = data['Close'].values\n\n# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Scale the features using MinMaxScaler\nscaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\n\n# Train linear regression model\nlinear_model = LinearRegression()\nlinear_model.fit(X_train_scaled, y_train)\n\n# Predict using linear regression\nlinear_train_predictions = linear_model.predict(X_train_scaled)\nlinear_test_predictions = linear_model.predict(X_test_scaled)\n\n# Train deep learning model (LSTM)\nmodel = Sequential()\nmodel.add(LSTM(128, input_shape=(1, 1)))\nmodel.add(Dense(1))\nmodel.compile(optimizer='adam', loss='mean_squared_error')\nmodel.fit(X_train_scaled.reshape((-1, 1, 1)), y_train, epochs=50, batch_size=32)\n\n# Predict using LSTM\nlstm_train_predictions = model.predict(X_train_scaled.reshape((-1, 1, 1))).flatten()\nlstm_test_predictions = model.predict(X_test_scaled.reshape((-1, 1, 1))).flatten()\n\n# Combine predictions\ncombined_train_predictions = (linear_train_predictions + lstm_train_predictions) / 2\ncombined_test_predictions = (linear_test_predictions + lstm_test_predictions) / 2\n\n# Calculate RMSE\ntrain_rmse = np.sqrt(mean_squared_error(y_train, combined_train_predictions))\ntest_rmse = np.sqrt(mean_squared_error(y_test, combined_test_predictions))\n\nprint(\"Train RMSE:\", train_rmse)\nprint(\"Test RMSE:\", test_rmse)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T19:26:10.972876Z","iopub.execute_input":"2023-05-27T19:26:10.973275Z","iopub.status.idle":"2023-05-27T19:26:49.401832Z","shell.execute_reply.started":"2023-05-27T19:26:10.97324Z","shell.execute_reply":"2023-05-27T19:26:49.399909Z"},"trusted":true},"execution_count":null,"outputs":[]}]}