Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# Week 9 - Exercise ### Notebook created by Jonathan Penava For this exercise you are going to try to try and predict the carat
# Week Exercise ### Notebook created by Jonathan Penava For this exercise you are going to try to try and predict the carat range of a diamond based on some of its properties. Start by importing the standard libraries and reading in the 'diamonds.csv file. Drop the 'Color', 'Clarity', and 'Unnamed: Columns. You will need to change the 'cut' values into int values. Referencing what we did in week create a new column for carat ranges with the values Drop the column 'carat' after you have created the carat ranges column Using KNearest Neighbours classify your carat ranges based on the other values in your dataframe. Print out an accuracy score and a classification report for your algorithm. Create a diagram of error rates for different values of K What value of K will give you a more accurate result? import pandas as pd from sklearn.modelselection import traintestsplit from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracyscore, classificationreport import matplotlib.pyplot as plt diamondsdf pdreadcsvdiamondscsv diamondsdfdropcolumnscolor 'clarity', 'Unnamed: inplaceTrue cutmapping Fair: 'Good': 'Very Good': 'Premium': 'Ideal': diamondsdfcut diamondsdfcutmapcutmapping caratbins caratlabels diamondsdfcaratrange' pdcutdiamondsdfcarat binscaratbins, labelscaratlabels diamondsdfdropcolumnscarat inplaceTrue X diamondsdfdropcolumnscaratrange' y diamondsdfcaratrange' Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate knn KNeighborsClassifiernneighbors # You can change the value of K knnfitXtrain, ytrain ypred knnpredictXtest accuracy accuracyscoreytest, ypred printfAccuracy: accuracy:f printclassificationreportytest, ypred errorrates kvalues range for k in kvalues: knn KNeighborsClassifiernneighborsk knnfitXtrain, ytrain ypred knnpredictXtest errorrate accuracyscoreytest, ypred errorrates.appenderrorrate pltplotkvalues, errorrates, markero pltxlabelK Value' pltylabelError Rate' plttitleError Rates for Different K Values' pltshow According to the above week exercise do the following Week Exercise Notebook created by Jonathan Penava B For this exercise you are going to modify your week exercise to be a web service. Have a sample URL that will return an example of JSON input. Have an evaluate URL that will return the carat range based on the input. Create a separate notebook Notes Part that will test out your URL's
# Week Exercise
### Notebook created by Jonathan Penava
For this exercise you are going to try to try and predict the carat range of a diamond based on some of its properties. Start by importing the standard libraries and reading in the 'diamonds.csv file.
Drop the 'Color', 'Clarity', and 'Unnamed: Columns.
You will need to change the 'cut' values into int values.
Referencing what we did in week create a new column for carat ranges with the values
Drop the column 'carat' after you have created the carat ranges column
Using KNearest Neighbours classify your carat ranges based on the other values in your dataframe. Print out an accuracy score and a classification report for your algorithm.
Create a diagram of error rates for different values of K What value of K will give you a more accurate result?
import pandas as pd
from sklearn.modelselection import traintestsplit
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracyscore, classificationreport
import matplotlib.pyplot as plt
diamondsdf pdreadcsvdiamondscsv
diamondsdfdropcolumnscolor 'clarity', 'Unnamed: inplaceTrue
cutmapping Fair: 'Good': 'Very Good': 'Premium': 'Ideal':
diamondsdfcut diamondsdfcutmapcutmapping
caratbins
caratlabels
diamondsdfcaratrange' pdcutdiamondsdfcarat binscaratbins, labelscaratlabels
diamondsdfdropcolumnscarat inplaceTrue
X diamondsdfdropcolumnscaratrange'
y diamondsdfcaratrange'
Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate
knn KNeighborsClassifiernneighbors # You can change the value of K
knnfitXtrain, ytrain
ypred knnpredictXtest
accuracy accuracyscoreytest, ypred
printfAccuracy: accuracy:f
printclassificationreportytest, ypred
errorrates
kvalues range
for k in kvalues:
knn KNeighborsClassifiernneighborsk
knnfitXtrain, ytrain
ypred knnpredictXtest
errorrate accuracyscoreytest, ypred
errorrates.appenderrorrate
pltplotkvalues, errorrates, markero
pltxlabelK Value'
pltylabelError Rate'
plttitleError Rates for Different K Values'
pltshow According to the above week exercise do the following
Week Exercise
Notebook created by Jonathan Penava
B
For this exercise you are going to modify your week exercise to be a web service.
Have a sample URL that will return an example of JSON input.
Have an evaluate URL that will return the carat range based on the input.
Create a separate notebook Notes Part that will test out your URL's
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