Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
nabitti
Regular Visitor

Polynomial Regression using Python

I'm following this tutorial in youtube (https://www.youtube.com/watch?v=CPuDfovUuTs) to create a polynomial regresssion in Power BI. I have the same exact script but I'm getting this error:

 

Error Message:

Þγτћŏл §сґĩρţ ęѓгŏř.

Traceback (most recent call last):

  File "PythonScriptWrapper.PY", line 41, in <module>

    y_pred_p = model.predict(poly_feat.fit_transform(param_val))

  File "C:\Users\Nabila\miniconda3\envs\std_env\lib\site-packages\sklearn\base.py", line 690, in fit_transform

    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\Nabila\miniconda3\envs\std_env\lib\site-packages\sklearn\preprocessing\_data.py", line 1513, in fit

    X, accept_sparse=True).shape

  File "C:\Users\Nabila\miniconda3\envs\std_env\lib\site-packages\sklearn\base.py", line 420, in _validate_data

    X = check_array(X, **check_params)

  File "C:\Users\Nabila\miniconda3\envs\std_env\lib\site-packages\sklearn\utils\validation.py", line 73, in inner_f

    return f(**kwargs)

  File "C:\Users\Nabila\miniconda3\envs\std_env\lib\site-packages\sklearn\utils\validation.py", line 617, in check_array

    "if it contains a single sample.".format(array))

ValueError: Expected 2D array, got scalar array instead:

array=5.

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

 

 

Note: I'm using Python with Miniconda so the file path I have specified in Power BI is C\Nabila\miniconda3\envs\std_env

and I have all the packages numpy, sklearn, pandas and matplotlib installed. It's worthy to emntion that I created the environment using Python 3.6 following this tutorial (https://medium.com/@pbabbott/how-to-connect-power-bi-and-python-on-windows-10-b131a36f4b44)

 

I tried many things but cant get it working , please help

3 REPLIES 3
v-deddai1-msft
Community Support
Community Support

Hi @nabitti ,

 

Please refer to the following links that have similar issues to yours:

 

https://stackoverflow.com/questions/54296377/valueerror-expected-2d-array-got-scalar-array-instead

https://datascience.stackexchange.com/questions/53048/expected-2d-array-got-scalar-array-instead

https://datascience.stackexchange.com/questions/52902/valueerror-expected-2d-array-got-scalar-array-...

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

Hello  @v-deddai1-msft , thank you for your reply.

I followed the links you mentioned and modified my script but now I have this other error that I do not know how to correct.
This is my script:

 


# dataset = pandas.DataFrame(Hour of Workday, Avg Quality, Valor Parámetro 2)
# dataset = dataset.drop_duplicates()
 
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

#split the dataset
X = dataset.iloc[:,0:1].values
X =X.reshape(-1,1)
y = dataset.iloc[:,1].values
y = y.reshape (-1,1)
z = dataset.iloc[2,2]

#fit the polynomial regressor to the dataset
poly_feat = PolynomialFeatures(degree=4)
X_poly = poly_feat.fit_transform(X)
model = LinearRegression()
model.fit(X_poly,y)

#chosen parameter
param_val = z

#predict quality based on the chosen hour
y_pred_p = model.predict(poly_feat.fit_transform([[param_val]]))

#Plot a range of predictions and actuals
X_line = np.arange(min(X), max(X), 0.01)
X_line=X_line.reshape((len(X_line), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_line,model.predict(poly_feat.fit_transofrm(X_line)),color='green')
plt.scatter(param_val, y_pred_p, color='green',marker='D')
plt.title('Quality Over Time', fontsize=24)
plt.xlabel('Hour of shift', fontsize=20)
plt.ylabel('Quality', fontsize=20)

#format and annotate graph
ax=plt.gca()
ax.grid(which='major', axis='both', linestyle='--')
ax.annotate('Polynomial Reg: {}'.format(np.round(y_pred_p, 2)),xy=(param_val, y_pred_p), xytext=(-90,-50), textcoords='offset points', size=15)

plt.yticks(fontsize=16)
plt.xticks(fontsize=16)
plt.show()

I have two colums that I dragged in 'values' to use for the scrpit and created a z parameter. Now the error I'm getting is this:
 

Error Message:
Þγτћŏл §сґĩρţ ęѓгŏř.
Traceback (most recent call last):
File "PythonScriptWrapper.PY", line 50, in <module>
plt.plot(X_line,model.predict(poly_feat.fit_transofrm(X_line)),color='green')
AttributeError: 'PolynomialFeatures' object has no attribute 'fit_transofrm'

 

I dont know how to correct it. Hope someone can help. Thank you so much.

Hi @nabitti ,

 

Your issue is more related to python. I suggest you open a thread in python forumn in SO. People there will help you well. Please also refer to the similar post: https://stackoverflow.com/questions/56201362/polynomialfeatures-object-has-no-attribute-predict

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors