Question

PYTHON

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

Our goal is to create a linear regression model to estimate values of ln_price using ln_carat as the only feature. We will now prepare the feature and label arrays.

Our goal is to create a linear regression model to estimate values of In_price using In_carat as the only feature. We will noWe will now calculate the r-squared score for the model on both the training set and the test set. Calculate and print the tr

"carat"   "cut" "color"   "clarity"   "depth"   "table"   "price"   "x"   "y"   "z"
"1" 0.23   "Ideal" "E" "SI2" 61.5 55 326   3.95   3.98   2.43
"2" 0.21   "Premium" "E" "SI1" 59.8 61 326   3.89   3.84   2.31
"3"   0.23   "Good" "E" "VS1" 56.9 65 327   4.05   4.07   2.31
"4"   0.29   "Premium" "I" "VS2" 62.4 58 334   4.2 4.23   2.63
"5"   0.31   "Good" "J" "SI2" 63.3 58 335   4.34   4.35   2.75
"6"   0.24   "Very Good"   "J" "VVS2"   62.8 57 336   3.94   3.96   2.48
"7"   0.24   "Very Good"   "I" "VVS1"   62.3 57 336   3.95   3.98   2.47
"8"   0.26   "Very Good"   "H" "SI1" 61.9 55 337   4.07   4.11   2.53
"9"   0.22 "Fair" "E" "VS2" 65.1 61 337   3.87 3.78   2.49
"10"   0.23   "Very Good"   "H"   "VS1" 59.4 61 338   4 4.05 2.39

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have written comments for the code. Please go through them

The data points given are less so for splitting i have used 0.2 percentage please change that 0.1 while you are using the script i have included the line as comment just uncomment it and use it when you have enough data. I have mentioned in the code as well please go through that.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

df = pd.read_csv('data.csv') # Read as csv file 

X2 = np.log(df['carat'].to_numpy().reshape(df.shape[0],1)) # converting carat to ln_carat and numpy  2d array
y = np.log(df['price'].to_numpy()) # converting price to ln_price and numpy 1d array

# I have used 0.2 test size here because there are only 10 data points are given and to calculate r-squared error 
# we need atleast two data points in test data set. So please delete this line and use the commented line when you have enough data
X_train_2, X_test_2, y_train_2, y_test_2 = train_test_split(X2, y, test_size=0.2, random_state = 1)
# X_train_2, X_test_2, y_train_2, y_test_2 = train_test_split(X2, y, test_size=0.1, random_state = 1) 

print("Training Features Shape: {}".format(X_train_2.shape))
print("Test Features Shape:     {}".format(X_test_2.shape))

dia_mod = LinearRegression() # Initalizing linear regression Model
dia_mod.fit(X_train_2, y_train_2) # fitting data with linear regression model.

print("Intercept:    {}".format(dia_mod.intercept_))
print("Coefficients: {}".format(dia_mod.coef_))


print('Training r-Squared: {}'.format(dia_mod.score(X_train_2,y_train_2)))
print('Testing r-Squared:  {}'.format(dia_mod.score(X_test_2,y_test_2)))

test_pred_2 = dia_mod.predict(X_test_2)
print("Observed Prices:  {}".format(np.exp(y_test_2))) # converting natural log values using exponential exp(log_base_e(2)) = 2
print("Estimated Prices: {}".format(np.exp(test_pred_2).round())) # converting natural log values using exponential exp(log_base_e(2)) = 2

diamonds_new = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] # given new list
diamonds_new = np.array(diamonds_new).reshape(len(diamonds_new),1) # conver to 2d column array 

new_pred_2 = dia_mod.predict(diamonds_new) # predicting using model
new_pred_3 = np.exp(new_pred_2) # converting natural log to normal values using np.exp
print("Esitmated Prices: {}".format(new_pred_3.round())) # round to nearest whole number/ prices.

Output Snippets:

-3]: import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression

: print(Training Features Shape: {}.format(X_train_2. shape)) print(Test Features Shape: {}.format(X_test_2. shape)) Trai

1: dia_mod LinearRegression () # Initalizing Linear regression Model : dia_mod. fit(X_train_2, y_train_2) # fitting data with

- : diamonds_new [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] # given new list diamonds_new = np.array(diamonds_new).reshape(len (diamonds_Please drop a comment for any further doubts.

Add a comment
Know the answer?
Add Answer to:
PYTHON import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is my code: import numpy as np import pandas as pd import sys from keras.models...

    This is my code: import numpy as np import pandas as pd import sys from keras.models import Sequential from keras.layers import Dense from sklearn.preprocessing import StandardScaler from keras.layers.normalization import BatchNormalization from keras.layers import Dropout file_full=pd.read_csv("/Users/anwer/Desktop/copy/FULL.csv") file_bottom=pd.read_csv("/Users/anwer/Desktop/copy/bottom.csv") train=[] train_targets=[] test=[] test_targets=[] p=[] q=[]    # We will generate train data using 50% of full data and 50% of bottom data. #is train target for labeling ? yes for train data train_df = file_full[:len(file_full)//2] labels=[ 0 for i in range(len(file_full)//2)] train_df=train_df.append(file_bottom[:len(file_bottom)//2]) for...

  • Python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt....

    python 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 abscissa = np.arange(20) 5 plt.gca().set_prop_cycle( ’ color ’ , [ ’ red ’ , ’ green ’ , ’ blue ’ , ’ black ’ ]) 6 7 class MyLine: 8 9 def __init__(self, * args, ** options): 10 #TO DO: IMPLEMENT FUNCTION 11 pass 12 13 def draw(self): 14 plt.plot(abscissa,self.line(abscissa)) 15 16 def get_line(self): 17 return "y = {0:.2f}x + {1:.2f}".format(self.slope, self.intercept) 18 19 def __str__(self):...

  • Before you start For this homework, we will need to import some libraries. You need to...

    Before you start For this homework, we will need to import some libraries. You need to execute the following cell only once; you don't need to copy this in every cell you run. In [ ]: import pandas import numpy from urllib.request import urlretrieve from matplotlib import pyplot %matplotlib inline ​ #This library is needed for testing from IPython.display import set_matplotlib_close set_matplotlib_close(False) Introduction In this homework, you will work with data from the World Bank. The subject of study is...

  • package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find...

    package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * *    * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

  • What are the major areas of change from the old design to the new design? What...

    What are the major areas of change from the old design to the new design? What do you think the major concerns will be of employees and managers in the new design? Use the star model to identify the transitions at each point of the star. Case Study 4: Reorganizing the Finance Department: Managing Change and Transitions Read the finance department case and consider the challenges you might anticipate during this reorganization. Develop a transition plan that addresses the following...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • I need Summary of this Paper i dont need long summary i need What methodology they used , what is the purpose of this p...

    I need Summary of this Paper i dont need long summary i need What methodology they used , what is the purpose of this paper and some conclusions and contributes of this paper. I need this for my Finishing Project so i need this ASAP please ( IN 1-2-3 HOURS PLEASE !!!) Budgetary Policy and Economic Growth Errol D'Souza The share of capital expenditures in government expenditures has been slipping and the tax reforms have not yet improved the income...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT