0
\$\begingroup\$

This is the task:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid') # Plot style

# Some 'magic' so that the notebook will reload external python modules;
# see
# http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2

Let's define a synthetic dataset and experiment on it. We will need to define a pandas dataframe with 3 columns using the 3 arrays given below.

 x1 = np.array([5, 6, 7, 7, 8, 9, 0, 1, 2, 4, 5, 6])
 x2 = np.array([2, 1, 3, 5, 10, 3, 4, 8, 6, 10, 9, 11])
 y = np.array([1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1])
 
 ##### YOUR CODE STARTS HERE##### 
    
    
    
 ##### YOUR CODE ENDS HERE ##### 

Plot the data in axes x1 and x2 , highlight two classes with different colors, name the axes.

def plot_data(data): 
  plt.figure(figsize=(8, 6))
  ##### YOUR CODE STARTS HERE #####



  ##### YOUR CODE ENDS HERE ##### 

plot_data(data)

And here is my solution:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid') # Plot style

# Some 'magic' so that the notebook will reload external python modules;
# see
# http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
x1 = np.array([5, 6, 7, 7, 8, 9, 0, 1, 2, 4, 5, 6])
x2 = np.array([2, 1, 3, 5, 10, 3, 4, 8, 6, 10, 9, 11])
y = np.array([1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1])

##### YOUR CODE STARTS HERE##### 

    d = {"x1": x1, "x2": x2, "y": y}
    data = pd.DataFrame(d)
    
##### YOUR CODE ENDS HERE ##### 
    def plot_data(data): 
      plt.figure(figsize=(8, 6))
      ##### YOUR CODE STARTS HERE #####
      negative = data[data['y'] < 0]
      positive = data[data['y'] > 0]
      plt.scatter(negative["x1"], negative["x2"], color="red")  
      plt.scatter(positive["x1"], positive['x2'])
      ##### YOUR CODE ENDS HERE ##### 
    
    plot_data(data)

What can I improve here?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.