Question

In: Computer Science

This is a machine learning question. Please use python and google colab format. PLEASE USE BASIC...

This is a machine learning question. Please use python and google colab format. PLEASE USE BASIC MACHINE LEARNING CODES.

Using the Kaggle diamonds dataset, construct a KNN estimator to predict diamond prices. Choose an appropriate K value and predict the price of a diamond with the following parameters: "carat' : 0.32, 'cut' : Ideal, 'color' : E, 'clarity' : IF, 'depth' : 60.7, 'table' : 58.0, 'x' : 4.46, 'y' : 4.48,  'z': 2.71".

Please change the cut, color and clarity to numbers (Eg: cut: 'Fair' : 1, 'Good' : 2, 'Very Good' : 3, 'Premium' : 4, 'Ideal' : 5) etc. I need to predict the price of the specific diamond stated above. Thank you!

Solutions

Expert Solution

Please find the google colab code below:

import pandas as pd

data = pd.read_csv("diamonds.csv")   # read data from the csv file
data.head()   # print the first 5 lines to check if the data has been imported correctly 

data.drop('Unnamed: 0', inplace=True, axis=1)   # drop the serial number column
data.head()

# get the unique values in each categorical column
print(data["cut"].unique())
print(data["color"].unique())
print(data["clarity"].unique())

# replace the categorical values with the corresponding numerical value
data['cut'].replace(to_replace=['Ideal', 'Premium', 'Good', 'Very Good', 'Fair'], value=[5, 4, 3, 2, 1], inplace=True)
data['color'].replace(to_replace=['E', 'I', 'J', 'H', 'F', 'G', 'D'], value=[7, 6, 5, 4, 3, 2, 1], inplace=True)
data['clarity'].replace(to_replace=['SI2', 'SI1', 'VS1', 'VS2', 'VVS2', 'VVS1', 'I1', 'IF'], value=[8, 7, 6, 5, 4, 3, 2, 1], inplace=True)

# see if all the data has been correctly replaced
print(data["cut"].unique())
print(data["color"].unique())
print(data["clarity"].unique())

data.head()

# store the price column in y and the rest of the features in X
y = data['price']
X = data.drop("price", axis=1)

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier()    # define the KNeighborsClassifier model
knn.fit(X, y)   # fit the data

# create the test dataframe
test_data = {'carat' : [0.32], 'cut' : [5], 'color' : [7], 'clarity' : [1], 'depth' : [60.7], 'table' : [58.0], 'x' : [4.46], 'y' : [4.48],  'z': [2.71]}
to_test = pd.DataFrame (test_data, columns = ['carat', 'cut', 'color', 'clarity', 'depth', 'table', 'x', 'y', 'z'])
to_test

print("Price: ", knn.predict(to_test)[0])   # print the predicted price

Since .ipynb file can't be attached, the python code has been pasted. The final screenshot is as follows:

If you have any doubts please let me know in the comments. (Also upvote/thumbsup if you can!)


Related Solutions

URGENT PLZ - BASIC PYTHON Use the following format for each of your files in the...
URGENT PLZ - BASIC PYTHON Use the following format for each of your files in the header. # Name: # Date: # Description: You programs should be FULLY COMMENTED. Mr. Zapanta has locked himself out of his laptop and completely forgotten his password (the password is “cardinalcarter123” or “CardinalCarter123”). Write a program that generates a password entry. The program will display an appropriate message if Mr. Zapanta enters the wrong password. The program will allow a maximum of 5 tries...
This is A PYTHON Question. Developing a machine learning system for a given dataset. Dataset Diabetes...
This is A PYTHON Question. Developing a machine learning system for a given dataset. Dataset Diabetes dataset - https://www.kaggle.com/uciml/pima-indians-diabetes-database Algorithms Either one of the following: 1. K-Nearest Neighbors 2. Support Vector Machines 3. Neural Networks Notes: Explain your choice of algorithms and analyze the models developed. Show what patterns/insights can be extracted from your chosen dataset and the selected algorithms.
Use Ati Active learning Template Basic concept to demonstrate Coping Use ATI Active learning template Basic...
Use Ati Active learning Template Basic concept to demonstrate Coping Use ATI Active learning template Basic concept to demonstrate self concept and sexuality
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
Please use IRAC format to answer this question. Bob is tired of working as an attorney...
Please use IRAC format to answer this question. Bob is tired of working as an attorney and decides to open a pet shop. Although Bob knows nothing about dogs, he decides to specialize in retrievers. Bob calls up Dog Breeder and orders 10 Black Labrador retrievers, 10 Chocolate Labrador retrievers, and 10 Yellow Labrador retrievers. All dogs are to be pure bred and male. Breeder mistakenly sends Bob 5 male and 5 female retrievers of each category. Bob does not...
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
On Python a) Use format() method to print an integer value entered by the user and...
On Python a) Use format() method to print an integer value entered by the user and its cube root with two decimal places. b) Print the same values as part (a) using format() function with keyword arguments and labels number and cubeRoot as in: format(number=n,cubeRoot=cr) c) Switch the order of keyword arguments and show that this has no effect on the output.
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could only include alphabetic letters and space. There are 26 alphabetic letters. Consider space the 27th letter. The user then enters a shift code that should be an integer between -26 and 26. The application will show the encoded/decoded message based on the shift code entered. If you encode a message, each letter in the message will be moved forward through the alphabet according to...
Please use IRAC format to answer this question. John and his wife needed their house painted....
Please use IRAC format to answer this question. John and his wife needed their house painted. John wanted to hired Tom to paint the house. Tom estimated it would take 40 hours of labor at $50.00 per hour and 20 gallons of premium paint at $20.00 per gallon. John agreed to pay the labor charge but decided he would buy his own premium paint. John and Tom entered into a valid written contract for the painting of the house: one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT