cancel
Showing results for 
Search instead for 
Did you mean: 

SyntaxError: invalid syntax

Hi, my code works perfectly in Spyder butwhen I paste here in PowerBI it gives below error and need support, thanks

Error Message:
Python script error.

import os
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

from scipy.ndimage import gaussian_filter

os.chdir(r'\Users\mishaq\OneDrive - Nokia\Machine Learning\DataSets')

default_bin_size = 2
default_smoothing_alpha = 0.1
default_use_average = False
input_data = 'For link curve RSRP vs Tput.csv'

df = pd.read_csv(input_data)

bin_size = default_bin_size
smoothing_alpha = default_smoothing_alpha
use_average = default_use_average
low_threshold = 25
plt.rcParams['font.family'] = "Arial"

x_label = 'NR_UE_RSRP_0'
y_label = 'Physical_Throughput_DL_5GNR'
selector = 'Logic'

########################################################################

df = df[[x_label, y_label, selector]]
df.dropna(how='any', axis=0, inplace=True)

# Bin the data
x = df[x_label]
y = df[y_label] 

min_x = min(x)
max_x = max(x)
min_y = min(y)
max_y = max(y)
    
bins = np.arange(min_x, max_x + bin_size, bin_size)
x_binned = pd.cut(x, bins)

df['x_bin'] = x_binned

#output_file = input_data.split('.')[0]

fig = plt.figure(figsize=(8,5))
for logic in df[selector].unique():
    df_ = df[df[selector] == logic]

    # now obtain the aggregate
    if use_average:
        df_summarized = df_.groupby('x_bin').agg(lambda x: x.mean(skipna=True)).reset_index()
        print('INFORMATION: Binning by the average function.')
    else:
        df_summarized = df_.groupby('x_bin').agg(lambda x: x.max(skipna=True)).reset_index()
        print('INFORMATION: Binning by the max function.')
    
    # if n is small, kill the sample with NaN since the statistic is insufficient.
    df_summarized['N'] = df.groupby('x_bin').agg('count').reset_index()[x_label]
    df_summarized.loc[df_summarized['N'] < low_threshold, y_label] = np.nan
    df_summarized['x_fit'] = df_summarized['x_bin'].apply(lambda x: x.mid)
    
    df_summarized.drop('x_bin', axis=1, inplace=True)

    df_ = df_summarized[df_summarized[selector] == logic]
    df_true = df[df[selector] == logic]
    #df_summarized.dropna(inplace=True)
    df_.fillna(method='ffill', inplace=True)
    y_smooth = df_[y_label].ewm(alpha=smoothing_alpha).mean()
    df_['y_fitted'] = gaussian_filter(y_smooth, sigma=1)

    plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
    plt.plot(df_['x_fit'], df_['y_fitted'], label=f'Best fit {logic}')

plt.grid(True, ls='dashed')
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend()
plt.tight_layout()
#plt.savefig(f'{output_file}.pdf', format='pdf')
plt.show()
plt.close(fig)


File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax


Stack Trace:
Microsoft.PowerBI.ExploreServiceCommon.ScriptHandlerException: Python script error.
File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax
---> Microsoft.PowerBI.Scripting.Python.Exceptions.PythonScriptRuntimeException: Python script error.
File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax

mishaq_0-1617817677712.png

 

 

Status: New
Comments
v-chuncz-msft
Community Support

@mishaq 

 

Take a good look at Create visuals with Python and try the latest version.