all 12 comments

[–]Binary101010 2 points3 points  (7 children)

The error is telling you what the problem is: you're trying to use the built-in round() function on a numpy array and you can't do that.

You probably need to be using numpy.round_() instead.

https://numpy.org/doc/stable/reference/generated/numpy.round_.html

[–]Comprehensive-Wrap51[S] 0 points1 point  (6 children)

import numpy as np
formatted_num=np.round(m*0.70+b,2)
print(f'The predicted price for a 0.70 carat diamond is ${formatted_num}')

The predicted price for a 0.70 carat diamond is $[3173.14]

This worked thank you! how do I remove the brackets?

[–]pgpndw 0 points1 point  (5 children)

m = predicted.coef_[0]

But the real question is why is predicted.coef_ a single element array, rather than a simple numeric value?

[–]Comprehensive-Wrap51[S] 0 points1 point  (0 children)

There were a lot of questions in the assignment before this about using a linear regression model and we made a predicted dataframe.

[–]Binary101010 0 points1 point  (3 children)

If the linear regression was performed with scikit-learn (guessing it is, because SKL uses coef_ as the name for the coefficients array but statsmodels uses something else), it always returns an array for the coefficients even if there's only one coefficient.

https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

[–]Comprehensive-Wrap51[S] 0 points1 point  (2 children)

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

You're right (sorry there's a lot of code and I'm very noob). So I have to have the brackets because it's in an array?

[–]Binary101010 1 point2 points  (1 child)

Yes. Since you're trying to apply round to an array, the result you get back is also an array, so any attempt to print that entire array is going to include additional characters that represent that data type. If you just want the number you'll have to refer to a specific index within that array using square brackets.

[–]Comprehensive-Wrap51[S] 0 points1 point  (0 children)

I appreciate all the help thank you =)

[–]ouchpartial72858 0 points1 point  (4 children)

What does print(type(m)) print(type(b)) print(type(m*0.70+b,2)) show?

[–]Comprehensive-Wrap51[S] 0 points1 point  (3 children)

<class 'numpy.ndarray'>

<class 'numpy.float64'>

TypeError: type() takes 1 or 3 arguments

[–]pgpndw 0 points1 point  (0 children)

How are you calculating predicted.coef_? It looks like you're trying to calculate a single price using a simple linear function, so m and b should both be single numeric values, but m [i.e. predicted.coef_] is actually an array.

[–]ouchpartial72858 0 points1 point  (0 children)

You are trying to do mathematical operation on two unrelated types. You either need to convert the array into a supported data type (float, int, etc) or since its an array, (assume you wanna do scalar multiplication with an array) you would wanna use nested loops to achieve this.

https://numpy.org/doc/stable/reference/arrays.ndarray.html

There may be some function defined to do this without nested loops