Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_wine In [2]: # In this assignment, we use the wine data set to
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_wine
In [2]:
# In this assignment, we use the wine data set to apply PCA in a classification problem. # Check the page modules to read an example that uses PCA. X = load_wine() X.data.shape, X.target.shape
Out[2]:
((178, 13), (178,))
In [3]:
# The data set contains data on three types of wines from the same Italian region. # The classes (i.e. the target) are wines 1, 2, and 3. # The features are 13 chemical properties that can be relatively easily measured. # Uncomment the following line to read more about the data set. # print(X.DESCR)
In [4]:
# We extract the training set. X_train = X.data y_train = X.target + 1 X_train.shape, y_train.shape
Out[4]:
((178, 13), (178,))
In [5]:
# question 1. # Apply PCA on the training set, and store the result in X_train_pca. X_train_pca.shape
Out[5]:
(178, 13)
In [6]:
# question 2. # Plot the first two columns in X_train_pca, i.e. the x axis is column 0, and the y axis is column 1, # and show the three wines in different colors and symbols. # Use the function plt.plot(x_value, y_value, parameters, label) 3 times, # where parameters are 'yo', 'bx', and 'g+', for wines 1, 2, and 3 respectively. plt.legend(loc="upper left")
Out[6]:
In [7]:
# We just bought 4 new bottles of Italian wine, however we do not know their wine type, # and we want to use data science to find the answer. # We measure all 13 chemical properties for each bootle, and obtain the following data. # [[ 14, 2, 2, 16, 127, 3, 3, 0, 2, 6, 1, 4, 1065] # [ 13, 2, 3, 19, 101, 3, 3, 0, 3, 6, 1, 3, 1185] # [ 14, 2, 2, 17, 113, 4, 3, 0, 2, 8, 1, 3, 1480] # [ 14, 2, 3, 20, 98, 2, 3, 0, 2, 6, 1, 3, 1180]]
In [8]:
# question 3. # Using the PCA previously trained on X_train, transform the data of the 4 new wine bottles, and plot the results along with the training data set. # For the new wine, use the following call to the function plot. # plt.plot(x_value, y_value, 'rs', label='new wine') plt.legend(loc="upper left")
Out[8]:
In [9]:
# question 4. # Answer in a comment. Using the previous plot, what is the predicted class of the four new wine bottles?
In [ ]:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started