spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.31k stars 1.61k forks source link

for cycle & if conditions return EOF error #11446

Closed solocazzimiei closed 4 years ago

solocazzimiei commented 4 years ago

I'm newer user of both python and Spyder, but since the begining in original conf of Spyder3 (now also Spyder4) from Anaconda, I noted such EOF error while stepping any for cycle or if statement !! It's crazy.. you can't do almost anything.... Any solutions ??? thks

for param_seasonal in seasonal_pdq: File "", line 1 for param_seasonal in seasonal_pdq: ^ SyntaxError: unexpected EOF while parsing

try: File "", line 1 try: ^ SyntaxError: unexpected EOF while parsing

dalthviz commented 4 years ago

Hi @solocazzimiei could you provided a full code example of this happening and/or a .GIF showing the way you get the error (you can use something like LICECap for that)?

In general, please fill up all the info that the issue template describes (this is important to have a better understanding of the context where the issue is happening and replicate it if valid).

Furthermore, such a SyntaxError normally means an error in the code rather than a error in Spyder itself so probably a quick google search of the error could help to understand it better (if no automatic error report dialog pops up from Spyder or if Spyder isn't crashing of course :) ).

ccordoba12 commented 4 years ago

@solocazzimiei, it seems you're evaluating a for loop line by line using F9 or Run > Run selection or current line. Is that the case?

solocazzimiei commented 4 years ago

Thanks for feedback. Yes ccordoba12, F9 line by line debug. No dalthviz any LICECap This is the listate I'm evaluating/adapting, but please consider same kind of error occur also on my own code on every : "if,else, elsif, try" statement, "for" loop and also on "def" function...its seams error happen when the debugger (line by line F9) find a jump call...practically I can work only without any conditional statements... python 3.7.6 Qt 5.9.7 Spyder 4.0.1 Spyder kernel 1.8.1

import warnings import itertools import numpy as np import matplotlib.pyplot as plt warnings.filterwarnings("ignore") plt.style.use('fivethirtyeight') import pandas as pd import statsmodels.api as sm import matplotlib matplotlib.rcParams['axes.labelsize'] = 14 matplotlib.rcParams['xtick.labelsize'] = 12 matplotlib.rcParams['ytick.labelsize'] = 12 matplotlib.rcParams['text.color'] = 'k'

There are several categories in the Superstore sales data, we start from time series analysis and forecasting for furniture sales.

df = pd.read_excel("D:\Documenti\Develope\AI\Mio Tensorflow\Superstore.xls") furniture = df.loc[df['Category'] == 'Furniture']

We have a good 4-year furniture sales data.

furniture['Order Date'].min(), furniture['Order Date'].max()

cols = ['Row ID', 'Order ID', 'Ship Date', 'Ship Mode', 'Customer ID', 'Customer Name', 'Segment', 'Country', 'City', 'State', 'Postal Code', 'Region', 'Product ID', 'Category', 'Sub-Category', 'Product Name', 'Quantity', 'Discount', 'Profit'] furniture.drop(cols, axis=1, inplace=True) furniture = furniture.sort_values('Order Date') furniture.isnull().sum()

furniture = furniture.groupby('Order Date')['Sales'].sum().reset_index()

Indexing with Time Series Data

furniture = furniture.set_index('Order Date') furniture.index y = furniture['Sales'].resample('MS').mean()

Have a quick peek 2017 furniture sales data.

y['2017':]

y.plot(figsize=(15, 6)) plt.show()

from pylab import rcParams rcParams['figure.figsize'] = 18, 8 decomposition = sm.tsa.seasonal_decompose(y, model='additive') fig = decomposition.plot() plt.show()

p = d = q = range(0, 2) pdq = list(itertools.product(p, d, q)) seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p, d, q))] print('Examples of parameter combinations for Seasonal ARIMA...') print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[1])) print('SARIMAX: {} x {}'.format(pdq[1], seasonal_pdq[2])) print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[3])) print('SARIMAX: {} x {}'.format(pdq[2], seasonal_pdq[4]))

for param in pdq: for param_seasonal in seasonal_pdq: try: mod = sm.tsa.statespace.SARIMAX(y,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False) results = mod.fit() print('ARIMA{}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic)) except: continue

mod = sm.tsa.statespace.SARIMAX(y,order=(1, 1, 1),seasonal_order=(1, 1, 0, 12),enforce_stationarity=False,enforce_invertibility=False) results = mod.fit() print(results.summary().tables[1])

results.plot_diagnostics(figsize=(16, 8)) plt.show()

pred = results.get_prediction(start=pd.to_datetime('2017-01-01'), dynamic=False) pred_ci = pred.conf_int() ax = y['2014':].plot(label='observed') pred.predicted_mean.plot(ax=ax, label='One-step ahead Forecast', alpha=.7, figsize=(14, 7)) ax.fill_between(pred_ci.index, pred_ci.iloc[:, 0], pred_ci.iloc[:, 1], color='k', alpha=.2) ax.set_xlabel('Date') ax.set_ylabel('Furniture Sales') plt.legend() plt.show()

y_forecasted = pred.predicted_mean y_truth = y['2017-01-01':] mse = ((y_forecasted - y_truth) ** 2).mean() print('The Mean Squared Error of our forecasts is {}'.format(round(mse, 2)))

The Mean Squared Error of our forecasts is 22993.58

print('The Root Mean Squared Error of our forecasts is {}'.format(round(np.sqrt(mse), 2)))

pred_uc = results.get_forecast(steps=100) pred_ci = pred_uc.conf_int() ax = y.plot(label='observed', figsize=(14, 7)) pred_uc.predicted_mean.plot(ax=ax, label='Forecast') ax.fill_between(pred_ci.index, pred_ci.iloc[:, 0], pred_ci.iloc[:, 1], color='k', alpha=.25) ax.set_xlabel('Date') ax.set_ylabel('Furniture Sales') plt.legend() plt.show()

furniture = df.loc[df['Category'] == 'Furniture'] office = df.loc[df['Category'] == 'Office Supplies'] furniture.shape, office.shape

cols = ['Row ID', 'Order ID', 'Ship Date', 'Ship Mode', 'Customer ID', 'Customer Name', 'Segment', 'Country', 'City', 'State', 'Postal Code', 'Region', 'Product ID', 'Category', 'Sub-Category', 'Product Name', 'Quantity', 'Discount', 'Profit'] furniture.drop(cols, axis=1, inplace=True) office.drop(cols, axis=1, inplace=True) furniture = furniture.sort_values('Order Date') office = office.sort_values('Order Date') furniture = furniture.groupby('Order Date')['Sales'].sum().reset_index() office = office.groupby('Order Date')['Sales'].sum().reset_index() furniture = furniture.set_index('Order Date') office = office.set_index('Order Date') y_furniture = furniture['Sales'].resample('MS').mean() y_office = office['Sales'].resample('MS').mean() furniture = pd.DataFrame({'Order Date':y_furniture.index, 'Sales':y_furniture.values}) office = pd.DataFrame({'Order Date': y_office.index, 'Sales': y_office.values}) store = furniture.merge(office, how='inner', on='Order Date') store.rename(columns={'Sales_x': 'furniture_sales', 'Sales_y': 'office_sales'}, inplace=True) store.head()

plt.figure(figsize=(20, 8)) plt.plot(store['Order Date'], store['furniture_sales'], 'b-', label = 'furniture') plt.plot(store['Order Date'], store['office_sales'], 'r-', label = 'office supplies') plt.xlabel('Date'); plt.ylabel('Sales'); plt.title('Sales of Furniture and Office Supplies') plt.legend();

first_date = store.ix[np.min(list(np.where(store['office_sales'] > store['furniture_sales'])[0])), 'Order Date'] print("Office supplies first time produced higher sales than furniture is {}.".format(first_date.date()))

from fbprophet import Prophet furniture = furniture.rename(columns={'Order Date': 'ds', 'Sales': 'y'}) furniture_model = Prophet(interval_width=0.95) furniture_model.fit(furniture) office = office.rename(columns={'Order Date': 'ds', 'Sales': 'y'}) office_model = Prophet(interval_width=0.95) office_model.fit(office) furniture_forecast = furniture_model.make_future_dataframe(periods=36, freq='MS') furniture_forecast = furniture_model.predict(furniture_forecast) office_forecast = office_model.make_future_dataframe(periods=36, freq='MS') office_forecast = office_model.predict(office_forecast) plt.figure(figsize=(18, 6)) furniture_model.plot(furniture_forecast, xlabel = 'Date', ylabel = 'Sales') plt.title('Furniture Sales');

plt.figure(figsize=(18, 6)) office_model.plot(office_forecast, xlabel = 'Date', ylabel = 'Sales') plt.title('Office Supplies Sales');

furniturenames = ['furniture%s' % column for column in furniture_forecast.columns] officenames = ['office%s' % column for column in office_forecast.columns] merge_furniture_forecast = furniture_forecast.copy() merge_office_forecast = office_forecast.copy() merge_furniture_forecast.columns = furniture_names merge_office_forecast.columns = office_names forecast = pd.merge(merge_furniture_forecast, merge_office_forecast, how = 'inner', left_on = 'furniture_ds', right_on = 'office_ds') forecast = forecast.rename(columns={'furniture_ds': 'Date'}).drop('office_ds', axis=1) forecast.head()

plt.figure(figsize=(10, 7)) plt.plot(forecast['Date'], forecast['furniture_trend'], 'b-') plt.plot(forecast['Date'], forecast['office_trend'], 'r-') plt.legend(); plt.xlabel('Date'); plt.ylabel('Sales') plt.title('Furniture vs. Office Supplies Sales Trend');

plt.figure(figsize=(10, 7)) plt.plot(forecast['Date'], forecast['furniture_yhat'], 'b-') plt.plot(forecast['Date'], forecast['office_yhat'], 'r-') plt.legend(); plt.xlabel('Date'); plt.ylabel('Sales') plt.title('Furniture vs. Office Supplies Estimate');

furniture_model.plot_components(furniture_forecast);

office_model.plot_components(office_forecast);

bcolsen commented 4 years ago

F9 isn't a line by line debugging. It just copies the current line or selection to ipython console and executes it whether the current line or selection is valid code or not. Proper line by line debugging is done using ctrl + F5

It would be nice if F9 was a bit smarter but that is covered in #4431. Closing as a duplicate