Total area is zero in defuzzification - fuzzy-logic

my following code is getting the error: "AssertionError: Total area is zero in defuzzification!", followed by ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules.
I'm trying to understand what is wrong but I can't figure it out
its works when I give it a single input but when I use a loop its ends up with this error
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# range input and output
text = ctrl.Antecedent(np.arange(-1, 1.1, 0.1), 'text')
hashtag = ctrl.Antecedent(np.arange(-1, 1.1, 0.1), 'hashtag')
emoji = ctrl.Antecedent(np.arange(-1, 1.1, 0.1), 'emoji')
sentiment = ctrl.Consequent(np.arange(-1, 1.1, 0.1), 'sentiment')
# status input and output
text['negative'] = fuzz.trapmf(text.universe, [-1,-1, -0.5, -0.1])
text['neutral'] = fuzz.trimf(text.universe, [ -0.2,0, 0.2])
text['positive'] = fuzz.trapmf(text.universe, [0.1, 0.5,1, 1])
hashtag['negative'] = fuzz.trapmf(hashtag.universe, [-1,-1, -0.5, -0.1])
hashtag['neutral'] = fuzz.trimf(hashtag.universe, [ -0.2,0, 0.2])
hashtag['positive'] = fuzz.trapmf(hashtag.universe, [0.1, 0.5,1, 1])
emoji['negative'] = fuzz.trapmf(emoji.universe, [-1,-1, -0.5, -0.1])
emoji['neutral'] = fuzz.trimf(emoji.universe, [ -0.2,0, 0.2])
emoji['positive'] = fuzz.trapmf(emoji.universe, [0.1, 0.5,1, 1])
sentiment['strongly negative'] = fuzz.trimf(sentiment.universe, [-1,-1, -0.6])
sentiment['negative'] = fuzz.trimf(sentiment.universe, [-0.7,-0.5,-0.2])
sentiment['neutral'] = fuzz.trimf(sentiment.universe, [-0.3,0,0.3])
sentiment['positive'] = fuzz.trimf(sentiment.universe, [0.2,0.5,0.7])
sentiment['strongly positive'] = fuzz.trimf(sentiment.universe, [0.6,1,1])
rule1 = ctrl.Rule( text['positive'] & emoji['positive'] & hashtag['positive']| text['positive'] & emoji['positive'] & hashtag['neutral']
|text['positive'] & emoji['neutral'] & hashtag['positive'],
sentiment['strongly positive'])
rule2 = ctrl.Rule(text['positive'] & emoji['negative']& hashtag['positive'] |text['positive']& emoji['neutral']& hashtag['neutral'] |text['positive']& emoji['positive'] & hashtag['negative']
|text['neutral'] & emoji['positive']& hashtag['positive']|text['negative'] & emoji['positive']& hashtag['positive']|
text['neutral'] & emoji['positive']& hashtag['neutral']| text['neutral'] & emoji['neutral']&hashtag['positive'],
sentiment['positive'])
rule3 = ctrl.Rule( text['neutral'] & emoji['neutral']& hashtag['neutral']|text['positive']& emoji['negative'] & hashtag['negative'],
sentiment['neutral'])
rule4 = ctrl.Rule(text['negative'] & emoji['positive']& hashtag['negative']| text['negative'] & emoji['neutral']& hashtag['neutral'] | text['neutral'] & emoji['neutral']& hashtag['negative']
|text['negative'] & emoji['negative']& hashtag['positive']|text['neutral'] & emoji['negative']& hashtag['negative']
|text['neutral'] & emoji['negative']& hashtag['neutral'],
sentiment['negative'])
rule5 = ctrl.Rule( text['negative'] & emoji['negative']& hashtag['negative'] |
text['negative'] & emoji['neutral'] & hashtag['negative']|
text['negative'] & emoji['negative']& hashtag['neutral'],
sentiment['strongly negative'])
liste is a life of dictionary
for dic_tweets in liste:
for key in dic_tweets:
bot.input['text'] = dic_tweets['text']
bot.input['hashtag'] = dic_tweets['hashtag']
bot.input['emoji'] = dic_tweets['emoji']
bot.compute()
l.append(bot.output['sentiment'])
l
sentiment.view(sim=bot)
errors are :
AssertionError Traceback (most recent call last)
~\anaconda3\lib\site-packages\skfuzzy\control\controlsystem.py in defuzz(self)
585 try:
--> 586 return defuzz(ups_universe, output_mf,
587 self.var.defuzzify_method)
~\anaconda3\lib\site-packages\skfuzzy\defuzzify\defuzz.py in defuzz(x, mfx, mode)
247 zero_truth_degree = mfx.sum() == 0 # Approximation of total area
--> 248 assert not zero_truth_degree, 'Total area is zero in defuzzification!'
249
AssertionError: Total area is zero in defuzzification!
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-72-62f88feea746> in <module>
6 bot.input['hashtag'] = dic_tweets['hashtag']
7 bot.input['emoji'] = dic_tweets['emoji']
----> 8 bot.compute()
9 l.append(bot.output['sentiment'])
10
~\anaconda3\lib\site-packages\skfuzzy\control\controlsystem.py in compute(self)
371 for consequent in self.ctrl.consequents:
372 consequent.output[self] = \
--> 373 CrispValueCalculator(consequent, self).defuzz()
374 self.output[consequent.label] = consequent.output[self]
375
~\anaconda3\lib\site-packages\skfuzzy\control\controlsystem.py in defuzz(self)
587 self.var.defuzzify_method)
588 except AssertionError:
--> 589 raise ValueError("Crisp output cannot be calculated, likely "
590 "because the system is too sparse. Check to "
591 "make sure this set of input values will "
ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules.

Related

Is there a way to fit a normal curve to points?

As a small project I've made a program the throws nd dice an nt number of times. At each throw it sums the results from the dice and adds it to a list. At the end the data is rappresented with matplot.
import random
from collections import Counter
import matplotlib.pyplot as plt
nd = int(input("Insert number of dice: "))
nt = int(input("Insert number of throws: "))
print(nd, " dice thrown ", nt, " times")
print("Generating sums, please hold....")
c = 0
i = 0
sum = 0
sums = []
while nt >= i :
while nd >= c:
g = random.randint(1, 6)
sum = sum + g
c += 1
sums.append(sum)
i = i+1
c=0
sum = 0
print("Throw ", i, " of ", nt)
sums.sort()
max = max(sums)
min = min(sums)
print("||Maximum result: ", max, " ||Minimum result: ", min)
print("Now ordering results")
f = Counter(sums)
y = list(f.values())
x = list(f.keys())
print("Rappresenting results")
plt.plot(x, y)
plt.xlabel("Risultati")
plt.ylabel("Frequenza")
plt.title("Distribuzione delle somme")
plt.grid(True)
plt.tight_layout()
plt.show()
The resultant graph looks something like this:
I would like to know how to fit a gaussian curve to the points in order to make the graph clearer
The mean and the standard deviation of the sums are the parameters needed for the Gaussian normal. The pdf of a distribution has an area of 1. To scale it to the same size as the histogram, it needs to be multiplied with the number of input values (len(sums)).
Converting the code to work with numpy arrays, makes everything much faster:
import numpy as np
from collections import Counter
import matplotlib.pyplot as plt
from scipy.stats import norm
nd = 10000 # int(input("Insert number of dice: "))
nt = 10000 # int(input("Insert number of throws: "))
print(nd, "dice thrown", nt, "times")
print("Generating sums, please hold....")
sums = np.zeros(nt, dtype=np.int)
for i in range(nt):
sums[i] = np.sum(np.random.randint(1, 7, nd))
sums.sort()
xmax = sums.max()
xmin = sums.min()
print("||Maximum result: ", xmax, " ||Minimum result: ", xmin)
print("Now ordering results")
f = Counter(sums)
y = list(f.values())
x = list(f.keys())
print("Plotting results")
plt.plot(x, y)
mean = sums.mean()
std = sums.std()
xs = np.arange(xmin, xmax + 1)
plt.plot(xs, norm.pdf(xs, mean, std) * len(sums), color='red', alpha=0.7, lw=3)
plt.margins(x=0)
plt.xlim(xmin, xmax)
plt.ylim(ymin=0)
plt.tight_layout()
plt.show()
PS: Here is some code to add to the code of the question, using numpy only for calculating the mean and the standard deviation. (Note that as you use sum as a variable name, you get an error when you try to use Python's sum() function. Therefore, it is highly recommended to avoid naming variables such as sum and max.)
def f(x):
return norm.pdf(x, mean, std) * len(sums)
mean = np.mean(sums)
std = np.std(sums)
xs = range(xmin, xmax+1)
ys = [f(x) for x in xs]
plt.plot(xs, ys, color='red', lw=3)

Coreldraw VBA adding strings together in color assignment

Im sure im doing something stupid here but I cant get the following to work. I Have the following line of code
ActiveSelectionRange.ApplyCustomHatchFill ANG.value, LS.value, 0, 10, 0, LT.value, CreateRGBColor(255,255,255), Style:=OutlineStyles(TextBox5.value), DashDotLength:=(TextBox6.value), PenWidth:=(TextBox7.value), BackColor:=CreateRGBColor(255, 255, 255)
I am trying to replace the following part with variables
CreateRGBColor(255,255,255)
with the following
a = colbut.BackColor
R = a Mod 256
G = Int(a / 256) Mod 256
B = Int(a / 256 / 256) Mod 256
ActiveSelectionRange.ApplyCustomHatchFill ANG.value, LS.value, 0, 10, 0, LT.value, CreateRGBColor(" & R & ", " & G & ", " & B & ", " & "), Style:=OutlineStyles(TextBox5.value), DashDotLength:=(TextBox6.value), PenWidth:=(TextBox7.value), BackColor:=CreateRGBColor(255, 255, 255)
It keeps throwing the error "Wrong number of arguments or invalid property assignment"
I have tried all variations of adding the strings but to no avail
Any help is greatly appreciated
Mark

not possible to reshape

Hello when I run following code:
for record in training_data_list:
all_values = record.split(',')
y_inputs = (np.asfarray(all_values[0]))
ab = np.zeros(10)
ab[int(all_values[0])] = 1
print("ab= " + str(ab))
print("shape ab= " + str(ab.shape))
ac = np.expand_dims(ab, axis=0).T
print("ac = " + str(ac))
print("shape AC = " + str(ac.shape))
ac = ac.reshape((10,Y.shape[1]))
print("shape ac = " + str(ac.shape))
I run into following error:
ValueError: cannot reshape array of size 10 into shape (10,103)
The shape of ac before the reshape command is (10,1)
The shape of Y = (1,103)
So I want to have as shape for ac (10,103)
Second question, why is the error telling me cannot reshape array of size 10 while the size is (10,1)?

How continue normal line in dashed line chart (same line) in vb.net?

I want my normal line to continue in dashed line in char. I have chart from above:
Green line i want to continue on 2017/18 to be green and dashed but continue from the same line.
My code for this is that:
Dim OpenApu As New Series
Dim ClosedApu As New Series
Dim TargetWorkingAverage As New Series
OpenApu.Name = "Sum of APU units open"
ClosedApu.Name = "Sum of APU units closed"
TargetWorkingAverage.Name = "Target Working Average"
OpenApu.ChartType = SeriesChartType.Line
ClosedApu.ChartType = SeriesChartType.Line
TargetWorkingAverage.ChartType = SeriesChartType.Line
OpenApu.Color = System.Drawing.Color.Green
OpenApu.BorderWidth = 3
ClosedApu.Color = System.Drawing.Color.Blue
ClosedApu.BorderWidth = 3
TargetWorkingAverage.Color = System.Drawing.Color.Black
TargetWorkingAverage.BorderWidth = 3
TargetWorkingAverage.BorderDashStyle = ChartDashStyle.Dash
TargetWorkingAverage.ChartType = SeriesChartType.StepLine
' this i put my values from chart
ClosedApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, TotalCost2)
What i try, at final line to change style of line in dashed but change style for all line.
OpenApu.Color = System.Drawing.Color.Blue
OpenApu.BorderWidth = 3
OpenApu.BorderDashStyle = ChartDashStyle.Dash
OpenApu.ChartType = SeriesChartType.StepLine
ClosedApu.Color = System.Drawing.Color.Green
ClosedApu.BorderWidth = 3
ClosedApu.BorderDashStyle = ChartDashStyle.Dash
ClosedApu.ChartType = SeriesChartType.StepLine
OpenApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, 5000)
ClosedApu.Points.AddXY(DateTime.Now.Year & "/" & i + 1, 6000)
When try to change style in dahsed doesn't work, all my line is dashed but i want to continue un dahsed not all my line to be dashed.

Looping through pandas data frame and creating new column value

I'm trying to loop through a csv file which I converted into a pandas data frame.
I need to loop through each line and check the latitude and longitude data I have (2 separate columns) and append a code (0,1 or 2) to the the same line depending on whether the lat, long data falls within a certain range.
I'm somewhat new to python and would love any help ya'll might have.
It's throwing off quite a few errors at me.
book = 'yellow_tripdata_2014-04.csv'
write_book = 'yellow_04.csv'
yank_max_long = -73.921630300
yank_min_long = -73.931169700
yank_max_lat = 40.832823000
yank_min_lat = 40.825582000
mets_max_long = 40.760523000
mets_min_long = 40.753277000
mets_max_lat = -73.841035400
mets_min_lat = -73.850564600
df = pd.read_csv(book)
##To check for Yankee Stadium Lat's and Long's, if within gps units then Stadium_Code = 1 , if mets then Stadium_Code=2
df['Stadium_Code'] = 0
for i, row in df.iterrows():
if yank_min_lat <= float(row['dropoff_latitude']) <= yank_max_lat and yank_min_long <=float(row('dropoff_longitude')) <=yank_max_long:
row['Stadium_Code'] == 1
elif mets_min_lat <= float(row['dropoff_latitude']) <= mets_max_lat and mets_min_long <=float(row('dropoff_longitude')) <=mets_max_long:
row['Stadium_Code'] == 2
I tried using the .loc command but ran into this error message:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-33-9a9166772646> in <module>()
----> 1 yank_mask = (df['dropoff_latitude'] > yank_min_lat) & (df['dropoff_latitude'] <= yank_max_lat) & (df['dropoff_longitude'] > yank_min_long) & (df['dropoff_longitude'] <= yank_max_long)
2
3 mets_mask = (df['dropoff_latitude'] > mets_min_lat) & (df['dropoff_latitude'] <= mets_max_lat) & (df['dropoff_longitude'] > mets_min_long) & (df['dropoff_longitude'] <= mets_max_long)
4
5 df.loc[yank_mask, 'Stadium_Code'] = 1
/Users/benjaminprice/anaconda/lib/python3.4/site-packages/pandas/core/frame.py in __getitem__(self, key)
1795 return self._getitem_multilevel(key)
1796 else:
-> 1797 return self._getitem_column(key)
1798
1799 def _getitem_column(self, key):
/Users/benjaminprice/anaconda/lib/python3.4/site-packages/pandas/core/frame.py in _getitem_column(self, key)
1802 # get column
1803 if self.columns.is_unique:
-> 1804 return self._get_item_cache(key)
1805
1806 # duplicate columns & possible reduce dimensionaility
/Users/benjaminprice/anaconda/lib/python3.4/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
1082 res = cache.get(item)
1083 if res is None:
-> 1084 values = self._data.get(item)
1085 res = self._box_item_values(item, values)
1086 cache[item] = res
/Users/benjaminprice/anaconda/lib/python3.4/site-packages/pandas/core/internals.py in get(self, item, fastpath)
2849
2850 if not isnull(item):
-> 2851 loc = self.items.get_loc(item)
2852 else:
2853 indexer = np.arange(len(self.items))[isnull(self.items)]
/Users/benjaminprice/anaconda/lib/python3.4/site-packages/pandas/core/index.py in get_loc(self, key, method)
1570 """
1571 if method is None:
-> 1572 return self._engine.get_loc(_values_from_object(key))
1573
1574 indexer = self.get_indexer([key], method=method)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)()
KeyError: 'dropoff_latitude'
I'm usually not too bad at figuring out what these error codes mean, but this one threw me off.
Firstly it's pretty wasteful to iterate row-wise when there are vectorised solutions available that will operate on the whole df at once.
I'd create a boolean mask of your 2 conditions and pass these to .loc to mask the rows that meet the criteria and set these to the values.
Here the masks use the bitwise operators & to and the conditions and parentheses are used around each condition due to operator precedence.
So the following should work:
yank_mask = (df['dropoff_latitude'] > yank_min_lat) & (df['dropoff_latitude'] <= yank_max_lat) & (df['dropoff_longitude'] > yank_min_long) & (df['dropoff_longitude'] <= yank_max_long)
mets_mask = (df['dropoff_latitude'] > mets_min_lat) & (df['dropoff_latitude'] <= mets_max_lat) & (df['dropoff_longitude'] > mets_min_long) & (df['dropoff_longitude'] <= mets_max_long)
df.loc[yank_mask, 'Stadium_Code'] = 1
df.loc[mets_mask, 'Stadium_Code'] = 2
If not already done so I'd read the docs as will aid you in understanding how the above works