diff --git a/machine_learning/Multi-Armed Bandits .py b/machine_learning/Multi-Armed Bandits .py new file mode 100644 index 000000000000..416c7d711384 --- /dev/null +++ b/machine_learning/Multi-Armed Bandits .py @@ -0,0 +1,241 @@ +import numpy as np +import matplotlib.pyplot as plt +from abc import ABC, abstractmethod + + +class BanditAlgorithm(ABC): + """Base class for bandit algorithms""" + + def __init__(self, n_arms): + self.n_arms = n_arms + self.reset() + + def reset(self): + self.counts = np.zeros(self.n_arms) + self.rewards = np.zeros(self.n_arms) + self.t = 0 + + @abstractmethod + def select_arm(self): + pass + + def update(self, arm, reward): + self.t += 1 + self.counts[arm] += 1 + self.rewards[arm] += reward + + +class EpsilonGreedy(BanditAlgorithm): + """Epsilon-Greedy Algorithm""" + + def __init__(self, n_arms, epsilon=0.1): + super().__init__(n_arms) + self.epsilon = epsilon + + def select_arm(self): + if np.random.random() < self.epsilon: + # Explore: random arm + return np.random.randint(self.n_arms) + else: + # Exploit: best arm so far + avg_rewards = np.divide( + self.rewards, + self.counts, + out=np.zeros_like(self.rewards), + where=self.counts != 0, + ) + return np.argmax(avg_rewards) + + +class UCB(BanditAlgorithm): + """Upper Confidence Bound Algorithm""" + + def __init__(self, n_arms, c=2.0): + super().__init__(n_arms) + self.c = c + + def select_arm(self): + # If any arm hasn't been tried, try it + if 0 in self.counts: + return np.where(self.counts == 0)[0][0] + + # Calculate UCB values + avg_rewards = self.rewards / self.counts + confidence = self.c * np.sqrt(np.log(self.t) / self.counts) + ucb_values = avg_rewards + confidence + + return np.argmax(ucb_values) + + +class ThompsonSampling(BanditAlgorithm): + """Thompson Sampling (Beta-Bernoulli)""" + + def __init__(self, n_arms): + super().__init__(n_arms) + self.alpha = np.ones(n_arms) # Prior successes + self.beta = np.ones(n_arms) # Prior failures + + def select_arm(self): + # Sample from Beta distribution for each arm + samples = np.random.beta(self.alpha, self.beta) + return np.argmax(samples) + + def update(self, arm, reward): + super().update(arm, reward) + # Update Beta parameters + if reward > 0: + self.alpha[arm] += 1 + else: + self.beta[arm] += 1 + + +class GradientBandit(BanditAlgorithm): + """Gradient Bandit Algorithm""" + + def __init__(self, n_arms, alpha=0.1): + super().__init__(n_arms) + self.alpha = alpha + self.preferences = np.zeros(n_arms) + self.avg_reward = 0 + + def select_arm(self): + # Softmax to get probabilities + exp_prefs = np.exp(self.preferences - np.max(self.preferences)) + probs = exp_prefs / np.sum(exp_prefs) + return np.random.choice(self.n_arms, p=probs) + + def update(self, arm, reward): + super().update(arm, reward) + + # Update average reward + self.avg_reward += (reward - self.avg_reward) / self.t + + # Get action probabilities + exp_prefs = np.exp(self.preferences - np.max(self.preferences)) + probs = exp_prefs / np.sum(exp_prefs) + + # Update preferences + for a in range(self.n_arms): + if a == arm: + self.preferences[a] += ( + self.alpha * (reward - self.avg_reward) * (1 - probs[a]) + ) + else: + self.preferences[a] -= ( + self.alpha * (reward - self.avg_reward) * probs[a] + ) + + +# Testbed for comparing algorithms +class BanditTestbed: + """Environment for testing bandit algorithms""" + + def __init__(self, n_arms=10, true_rewards=None): + self.n_arms = n_arms + if true_rewards is None: + self.true_rewards = np.random.normal(0, 1, n_arms) + else: + self.true_rewards = true_rewards + self.optimal_arm = np.argmax(self.true_rewards) + + def get_reward(self, arm): + """Get noisy reward for pulling an arm""" + return np.random.normal(self.true_rewards[arm], 1) + + def run_experiment(self, algorithm, n_steps=1000): + """Run bandit algorithm for n_steps""" + algorithm.reset() + rewards = [] + optimal_actions = [] + + for _ in range(n_steps): + arm = algorithm.select_arm() + reward = self.get_reward(arm) + algorithm.update(arm, reward) + + rewards.append(reward) + optimal_actions.append(1 if arm == self.optimal_arm else 0) + + return np.array(rewards), np.array(optimal_actions) + + +# Example usage and comparison +def compare_algorithms(): + """Compare different bandit algorithms""" + + # Create testbed + testbed = BanditTestbed(n_arms=10) + + # Initialize algorithms + algorithms = { + "ε-greedy (0.1)": EpsilonGreedy(10, epsilon=0.1), + "ε-greedy (0.01)": EpsilonGreedy(10, epsilon=0.01), + "UCB (c=2)": UCB(10, c=2), + "Thompson Sampling": ThompsonSampling(10), + "Gradient Bandit": GradientBandit(10, alpha=0.1), + } + + n_steps = 2000 + n_runs = 100 + + results = {} + + for name, algorithm in algorithms.items(): + print(f"Running {name}...") + avg_rewards = np.zeros(n_steps) + optimal_actions = np.zeros(n_steps) + + for run in range(n_runs): + rewards, optimal = testbed.run_experiment(algorithm, n_steps) + avg_rewards += rewards + optimal_actions += optimal + + avg_rewards /= n_runs + optimal_actions /= n_runs + + results[name] = {"rewards": avg_rewards, "optimal_actions": optimal_actions} + + # Plot results + plt.figure(figsize=(15, 5)) + + # Average reward over time + plt.subplot(1, 2, 1) + for name, result in results.items(): + plt.plot(np.cumsum(result["rewards"]) / np.arange(1, n_steps + 1), label=name) + plt.xlabel("Steps") + plt.ylabel("Average Reward") + plt.title("Average Reward vs Steps") + plt.legend() + plt.grid(True) + + # Percentage of optimal actions + plt.subplot(1, 2, 2) + for name, result in results.items(): + plt.plot( + np.cumsum(result["optimal_actions"]) / np.arange(1, n_steps + 1) * 100, + label=name, + ) + plt.xlabel("Steps") + plt.ylabel("% Optimal Action") + plt.title("Optimal Action Selection vs Steps") + plt.legend() + plt.grid(True) + + plt.tight_layout() + plt.show() + + return results + + +# Run the comparison +if __name__ == "__main__": + results = compare_algorithms() + + # Print final performance + print("\nFinal Performance (last 100 steps):") + for name, result in results.items(): + avg_reward = np.mean(result["rewards"][-100:]) + optimal_pct = np.mean(result["optimal_actions"][-100:]) * 100 + print( + f"{name:20s}: Avg Reward = {avg_reward:.3f}, Optimal = {optimal_pct:.1f}%" + ) diff --git a/machine_learning/kernel_svm.ipynb b/machine_learning/kernel_svm.ipynb new file mode 100644 index 000000000000..5f91ec14c08f --- /dev/null +++ b/machine_learning/kernel_svm.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Copy of kernel_svm.ipynb","provenance":[{"file_id":"1U2p46TcDjQyYx80tQdZkiANbGTjEv8Po","timestamp":1660747043874}],"collapsed_sections":[],"toc_visible":true,"machine_shape":"hm"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"0MRC0e0KhQ0S"},"source":["# Kernel SVM"]},{"cell_type":"markdown","metadata":{"id":"LWd1UlMnhT2s"},"source":["## Importing the libraries"]},{"cell_type":"code","metadata":{"id":"YvGPUQaHhXfL","executionInfo":{"status":"ok","timestamp":1660747109613,"user_tz":-330,"elapsed":562,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","import pandas as pd"],"execution_count":2,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"K1VMqkGvhc3-"},"source":["## Importing the dataset"]},{"cell_type":"code","metadata":{"id":"M52QDmyzhh9s","executionInfo":{"status":"error","timestamp":1660747110167,"user_tz":-330,"elapsed":15,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}},"outputId":"8888c572-e012-4e97-8043-757e6a2c6b04","colab":{"base_uri":"https://localhost:8080/","height":363}},"source":["dataset = pd.read_csv('Social_Network_Ads.csv')\n","X = dataset.iloc[:, :-1].values\n","y = dataset.iloc[:, -1].values"],"execution_count":3,"outputs":[{"output_type":"error","ename":"FileNotFoundError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mFileNotFoundError\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[0mdataset\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Social_Network_Ads.csv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdataset\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/util/_decorators.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 309\u001b[0m \u001b[0mstacklevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstacklevel\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 310\u001b[0m )\n\u001b[0;32m--> 311\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 312\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 313\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/readers.py\u001b[0m in \u001b[0;36mread_csv\u001b[0;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)\u001b[0m\n\u001b[1;32m 584\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkwds_defaults\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 585\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 586\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_read\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 587\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 588\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/readers.py\u001b[0m in \u001b[0;36m_read\u001b[0;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[1;32m 480\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 481\u001b[0m \u001b[0;31m# Create the parser.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 482\u001b[0;31m \u001b[0mparser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTextFileReader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 483\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 484\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mchunksize\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0miterator\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/readers.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[1;32m 809\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"has_index_names\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"has_index_names\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 810\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 811\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mengine\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 812\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 813\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/readers.py\u001b[0m in \u001b[0;36m_make_engine\u001b[0;34m(self, engine)\u001b[0m\n\u001b[1;32m 1038\u001b[0m )\n\u001b[1;32m 1039\u001b[0m \u001b[0;31m# error: Too many arguments for \"ParserBase\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1040\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmapping\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mengine\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[call-arg]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1041\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1042\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_failover_to_python\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\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[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/c_parser_wrapper.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, src, **kwds)\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;31m# open handles\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 51\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_open_handles\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 52\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhandles\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/base_parser.py\u001b[0m in \u001b[0;36m_open_handles\u001b[0;34m(self, src, kwds)\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[0mmemory_map\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"memory_map\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\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[1;32m 228\u001b[0m \u001b[0mstorage_options\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"storage_options\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\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[0;32m--> 229\u001b[0;31m \u001b[0merrors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"encoding_errors\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"strict\"\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[1;32m 230\u001b[0m )\n\u001b[1;32m 231\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.7/dist-packages/pandas/io/common.py\u001b[0m in \u001b[0;36mget_handle\u001b[0;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[1;32m 705\u001b[0m \u001b[0mencoding\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mioargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencoding\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 706\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 707\u001b[0;31m \u001b[0mnewline\u001b[0m\u001b[0;34m=\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[1;32m 708\u001b[0m )\n\u001b[1;32m 709\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'Social_Network_Ads.csv'"]}]},{"cell_type":"markdown","metadata":{"id":"YvxIPVyMhmKp"},"source":["## Splitting the dataset into the Training set and Test set"]},{"cell_type":"code","metadata":{"id":"AVzJWAXIhxoC","executionInfo":{"status":"aborted","timestamp":1660747110169,"user_tz":-330,"elapsed":12,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from sklearn.model_selection import train_test_split\n","X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"P3nS3-6r1i2B","executionInfo":{"status":"aborted","timestamp":1660747110170,"user_tz":-330,"elapsed":13,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(X_train)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"8dpDLojm1mVG","executionInfo":{"status":"aborted","timestamp":1660747110171,"user_tz":-330,"elapsed":14,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(y_train)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"qbb7i0DH1qui","executionInfo":{"status":"aborted","timestamp":1660747110171,"user_tz":-330,"elapsed":13,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(X_test)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"kj1hnFAR1s5w","executionInfo":{"status":"aborted","timestamp":1660747110172,"user_tz":-330,"elapsed":14,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(y_test)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"kW3c7UYih0hT"},"source":["## Feature Scaling"]},{"cell_type":"code","metadata":{"id":"9fQlDPKCh8sc","executionInfo":{"status":"aborted","timestamp":1660747110173,"user_tz":-330,"elapsed":15,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from sklearn.preprocessing import StandardScaler\n","sc = StandardScaler()\n","X_train = sc.fit_transform(X_train)\n","X_test = sc.transform(X_test)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"syrnD1Op2BSR","executionInfo":{"status":"aborted","timestamp":1660747110173,"user_tz":-330,"elapsed":14,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(X_train)"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"JUd6iBRp2C3L","executionInfo":{"status":"aborted","timestamp":1660747110174,"user_tz":-330,"elapsed":15,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(X_test)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"bb6jCOCQiAmP"},"source":["## Training the Kernel SVM model on the Training set"]},{"cell_type":"code","metadata":{"id":"e0pFVAmciHQs","executionInfo":{"status":"aborted","timestamp":1660747110175,"user_tz":-330,"elapsed":16,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from sklearn.svm import SVC\n","classifier = SVC(kernel = 'rbf', random_state = 0)\n","classifier.fit(X_train, y_train)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"yyxW5b395mR2"},"source":["## Predicting a new result"]},{"cell_type":"code","metadata":{"id":"f8YOXsQy58rP","executionInfo":{"status":"aborted","timestamp":1660747110176,"user_tz":-330,"elapsed":17,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["print(classifier.predict(sc.transform([[30,87000]])))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"vKYVQH-l5NpE"},"source":["## Predicting the Test set results"]},{"cell_type":"code","metadata":{"id":"p6VMTb2O4hwM","executionInfo":{"status":"aborted","timestamp":1660747110177,"user_tz":-330,"elapsed":18,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["y_pred = classifier.predict(X_test)\n","print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"h4Hwj34ziWQW"},"source":["## Making the Confusion Matrix"]},{"cell_type":"code","metadata":{"id":"D6bpZwUiiXic","executionInfo":{"status":"aborted","timestamp":1660747110179,"user_tz":-330,"elapsed":20,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from sklearn.metrics import confusion_matrix, accuracy_score\n","cm = confusion_matrix(y_test, y_pred)\n","print(cm)\n","accuracy_score(y_test, y_pred)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"6OMC_P0diaoD"},"source":["## Visualising the Training set results"]},{"cell_type":"code","metadata":{"id":"_NOjKvZRid5l","executionInfo":{"status":"aborted","timestamp":1660747110181,"user_tz":-330,"elapsed":21,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from matplotlib.colors import ListedColormap\n","X_set, y_set = sc.inverse_transform(X_train), y_train\n","X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),\n"," np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))\n","plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),\n"," alpha = 0.75, cmap = ListedColormap(('red', 'green')))\n","plt.xlim(X1.min(), X1.max())\n","plt.ylim(X2.min(), X2.max())\n","for i, j in enumerate(np.unique(y_set)):\n"," plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)\n","plt.title('Kernel SVM (Training set)')\n","plt.xlabel('Age')\n","plt.ylabel('Estimated Salary')\n","plt.legend()\n","plt.show()"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"SZ-j28aPihZx"},"source":["## Visualising the Test set results"]},{"cell_type":"code","metadata":{"id":"qeTjz2vDilAC","executionInfo":{"status":"aborted","timestamp":1660747110182,"user_tz":-330,"elapsed":22,"user":{"displayName":"Alien Editz","userId":"03181622927197882991"}}},"source":["from matplotlib.colors import ListedColormap\n","X_set, y_set = sc.inverse_transform(X_test), y_test\n","X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),\n"," np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))\n","plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),\n"," alpha = 0.75, cmap = ListedColormap(('red', 'green')))\n","plt.xlim(X1.min(), X1.max())\n","plt.ylim(X2.min(), X2.max())\n","for i, j in enumerate(np.unique(y_set)):\n"," plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)\n","plt.title('Kernel SVM (Test set)')\n","plt.xlabel('Age')\n","plt.ylabel('Estimated Salary')\n","plt.legend()\n","plt.show()"],"execution_count":null,"outputs":[]}]} \ No newline at end of file