I've created a Python dask array and I'm trying to modify a slice of the array as follows:
import numpy as np
import dask.array as da
x = np.random.random((20000, 100, 100)) # Create numpy array
dx = da.from_array(x, chunks=(x.shape[0], 10, 10)) # Create dask array from numpy array
dx[:50, :, :] = 0 # Modify a slice of the dask array
Such an attempt to modify the dask array raises the exception:
TypeError: 'Array' object does not support item assignment
Is there a way to modify a dask array slice without raising an exception?
Currently dask.array does not support item assignment or any other mutation operation.
In the case above I recommend concatenating with zeros
In [1]: import dask.array as da
In [2]: dx = da.random.random((20000 - 50, 100, 100), chunks=(None, 10, 10))
In [3]: z = da.zeros((50, 100, 100), chunks=(50, 10, 10))
In [4]: dx2 = da.concatenate([z, dx], axis=0)
In [5]: dx2
Out[5]: dask.array<concate..., shape=(20000, 100, 100), dtype=float64, chunksize=(50, 10, 10)>
In [6]: (dx2 == 0)[0:100, 0, 0].compute()
Out[6]:
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True, True, True, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False, False], dtype=bool)
The da.where(condition, iftrue, iffalse) function can also be quite useful in working around cases where mutation is often desired.
Related
I would like to create a script in Dart that detects when in the OutputBool2 list there is a 'false' with a 'true' in the next index
I created this script, but it doesn't seem to work:
List OutputIndex2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
List OutpuBool2 = [false, false, false, false, false, false, false, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false];
for (int j in OutputIndex2.sublist(0, OutputList2.length - 1)) {
print(j);
if (OutputBool2[j] == false && OutputList2[j + 1] == true) {
print(FIND!!);
}
}
Can anyone explain to me what I'm doing wrong?
Thanks :)
I made this code:
List<bool> OutpuBool2 = [
false,
false,
false,
false,
false,
false,
false,
true,
false,
false,
false,
false,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false
];
for (var i = 0; i < OutpuBool2.length; i++) {
if (OutpuBool2[i] == false) {
if (OutpuBool2[i + 1] == true) {
print('There is a false with a true in the next index in index: $i');
}
}
}
and this is the result:
There is a false with a true in the next index in index: 6
There is a false with a true in the next index in index: 11
is that what you meant?
I want to print values of 'sm' inside loop
alist = [
{'price': '700', 'sizes': {'sm': True, 'md': False, 'lg': True, 'xl': True} },
{'price': '900', 'sizes': {'sm': False, 'md': True, 'lg': True, 'xl': True} }
]
for i in alist :
print(i.get('sizes'['sm']))
Revise print code.
alist = [
{'price': '700', 'sizes': {'sm': True, 'md': False, 'lg': True, 'xl': True} },
{'price': '900', 'sizes': {'sm': False, 'md': True, 'lg': True, 'xl': True} }
]
for i in alist :
print(i['sizes']['sm'])
#define ROWS 5
#define COLUMNS 5
int main(void)
{
bool *p = new bool[ROWS * COLUMNS] = {
{false, true, false, true, true},
{true, false, true, false, true},
{false, true, false, false, false},
{true, false, false, false, true},
{true, true, false, true, false}
};
}
// [Warning] extended initializer lists only available with std=c++11 or-std=gnu++11
// [Error] lvalue required as left operand of assignment
What is the problem here and how it can be resolved?
What you have trying to do is define a one-dimension array and initialized it like a two-dimension array, in that case, you should choose one of the following:
One-Dimention array:
bool bool_arr[ROWS * COLUMNS] = {
false, true, false, true, true,
true, false, true, false, true,
false, true, false, false, false,
true, false, false, false, true,
true, true, false, true, false
};
Two-Dimention array:
bool bool_mat[ROWS][COLUMNS] = {
{false, true, false, true, true},
{true, false, true, false, true},
{false, true, false, false, false},
{true, false, false, false, true},
{true, true, false, true, false}
};
I have two managed metadata fields in my list, they both can have multiple values, the first one is 'mm' and the second one has a danish symbol Ø - 'mm_Ø'.
I fetch the fields information using SharePoint REST API endpoint:
GET https://{tenant}.sharepoint.com/{site-path}/_api/Web/Lists(guid'{list-id}')/fields
In the response I see my fields definitions:
The 'mm' field is:
{
"odata.type": "SP.Taxonomy.TaxonomyField",
***
"CanBeDeleted": true,
"ClientSideComponentId": "00000000-0000-0000-0000-000000000000",
"ClientSideComponentProperties": null,
"ClientValidationFormula": null,
"ClientValidationMessage": null,
"CustomFormatter": null,
"DefaultFormula": null,
"DefaultValue": "",
"Description": "",
"Direction": "none",
"EnforceUniqueValues": false,
"EntityPropertyName": "mm",
"Filterable": true,
"FromBaseType": false,
"Group": "Custom Columns",
"Hidden": false,
"Id": "f863fab3-5611-4d81-82f9-299c42e6258c",
"Indexed": false,
"IndexStatus": 0,
"InternalName": "mm",
"JSLink": "SP.UI.Taxonomy.js|SP.UI.Rte.js(d)|SP.Taxonomy.js(d)|ScriptForWebTaggingUI.js(d)",
"PinnedToFiltersPane": false,
"ReadOnlyField": false,
"Required": false,
"SchemaXml": "***",
"Scope": ***,
"Sealed": false,
"ShowInFiltersPane": 0,
"Sortable": false,
"StaticName": "mm",
"Title": "mm",
"FieldTypeKind": 0,
"TypeAsString": "TaxonomyFieldTypeMulti",
"TypeDisplayName": "Managed Metadata",
"TypeShortDescription": "Managed Metadata",
"ValidationFormula": null,
"ValidationMessage": null,
"AllowMultipleValues": true,
"DependentLookupInternalNames": [],
"IsDependentLookup": false,
"IsRelationship": true,
"LookupField": "Term$Resources:core,Language;",
"LookupList": "{6eaf0d27-f293-4017-bac5-b3ef9b3fe079}",
"LookupWebId": "e0087d65-0dcb-41a5-bb7c-c4b2f94fed59",
"PrimaryFieldId": null,
"RelationshipDeleteBehavior": 0,
"UnlimitedLengthInDocumentLibrary": false,
"AnchorId": "00000000-0000-0000-0000-000000000000",
"CreateValuesInEditForm": false,
"IsAnchorValid": true,
"IsKeyword": false,
"IsPathRendered": false,
"IsTermSetValid": true,
"Open": true,
"SspId": "a1796552-9a87-4f1d-95d5-b4a3fc8f143d",
"TargetTemplate": null,
"TermSetId": "8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f",
"TextField": "93148f4a-853f-4851-a600-3f76b38d030a",
"UserCreated": false
}
And the 'mm_Ø' field:
{
"odata.type": "SP.Taxonomy.TaxonomyField",
***
"AutoIndexed": false,
"CanBeDeleted": true,
"ClientSideComponentId": "00000000-0000-0000-0000-000000000000",
"ClientSideComponentProperties": null,
"ClientValidationFormula": null,
"ClientValidationMessage": null,
"CustomFormatter": null,
"DefaultFormula": null,
"DefaultValue": "",
"Description": "",
"Direction": "none",
"EnforceUniqueValues": false,
"EntityPropertyName": "mm__x00d8_",
"Filterable": true,
"FromBaseType": false,
"Group": "Custom Columns",
"Hidden": false,
"Id": "8b6cb6c7-5bdf-4e87-95e0-8ad6db1bc60e",
"Indexed": false,
"IndexStatus": 0,
"InternalName": "mm__x00d8_",
"JSLink": "SP.UI.Taxonomy.js|SP.UI.Rte.js(d)|SP.Taxonomy.js(d)|ScriptForWebTaggingUI.js(d)",
"PinnedToFiltersPane": false,
"ReadOnlyField": false,
"Required": false,
"SchemaXml": "***",
"Scope": ***,
"Sealed": false,
"ShowInFiltersPane": 0,
"Sortable": false,
"StaticName": "mm__x00d8_",
"Title": "mm_Ø",
"FieldTypeKind": 0,
"TypeAsString": "TaxonomyFieldTypeMulti",
"TypeDisplayName": "Managed Metadata",
"TypeShortDescription": "Managed Metadata",
"ValidationFormula": null,
"ValidationMessage": null,
"AllowMultipleValues": true,
"DependentLookupInternalNames": [],
"IsDependentLookup": false,
"IsRelationship": true,
"LookupField": "Term$Resources:core,Language;",
"LookupList": "{6eaf0d27-f293-4017-bac5-b3ef9b3fe079}",
"LookupWebId": "e0087d65-0dcb-41a5-bb7c-c4b2f94fed59",
"PrimaryFieldId": null,
"RelationshipDeleteBehavior": 0,
"UnlimitedLengthInDocumentLibrary": false,
"AnchorId": "00000000-0000-0000-0000-000000000000",
"CreateValuesInEditForm": false,
"IsAnchorValid": true,
"IsKeyword": false,
"IsPathRendered": false,
"IsTermSetValid": true,
"Open": true,
"SspId": "a1796552-9a87-4f1d-95d5-b4a3fc8f143d",
"TargetTemplate": null,
"TermSetId": "8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f",
"TextField": "b658e553-725e-4e24-9a6a-c911b50d68a4",
"UserCreated": false
}
Now, I want to create an item in that list using REST endpoint:
POST https://{tenant}.sharepoint.com/{site-path}/_api/Web/Lists(guid'{list-id}')/items
First I try to create an item only filling the mm field
Body:
{
"Title": "Item1",
"f863fab356114d8182f9299c42e6258c": "taxonomy|24a5a5fa-7a1d-48d6-8a2d-7937429dfd71;taxonomy2|23901385-e142-458c-85f2-4b5ca5dbd812"
}
And it works. Now I want to do the same but filling the 'mm_Ø' field.
Body:
{
"Title": "ee",
"l910ce92c882443cb3134a7aeed731f7": "taxonomy|24a5a5fa-7a1d-48d6-8a2d-7937429dfd71;taxonomy2|23901385-e142-458c-85f2-4b5ca5dbd812"
}
The response I get:
{
"odata.error": {
"code": "-2130575340, Microsoft.SharePoint.SPException",
"message": {
"lang": "en-US",
"value": "One or more field types are not installed properly. Go to the list settings page to delete these fields."
}
}
}
Does it mean that having symbols other than English in the Managed Metadata causes SharePoint REST API to fail?
From what I can tell, if the meta data field title is too long (each non-standard English letter is 8 letters) then we are getting this error.
Couldn't find a solution except renaming to something shorter/no special characters.
Another solution is to use this new API:
https://[tenant].sharepoint.com/sites/test/_api/web/GetList(#a1)/AddValidateUpdateItemUsingPath()?#a1=%27%2Fsites%2Ftest%2FLists%2FMyList%27
=== update ===
Found a solution, might help you.
In our case, the hidden note field's title was created with wrong value.
it suppose to be "[meta data title]_0" but the special characters were cut off.
If I manually updated the hidden note field's title to "[meta data title]_0" the update works as expected.
It leads me to believe the API in the back end is relying on the field title instead of internal name/TextField property to map the value from the note field back to the meta data field.
I am trying to train a dataset with NLTK's Naive Bayes Classifier but my terminal keeps throwing this error
# Applying Naive Bayes
training_set = featursets[:2000]
testing_set = featursets[2000:]
classifier = nltk.NaiveBayesClassifier.train(training_set)
print "Naive bayes classifier accuracy % = ", (nltk.classify.accuracy(classifier, testing_set)*100)
classifier.show_informative_features(30)
And The error says:
AttributeError
Traceback (most recent call last)
<ipython-input-69-2a409562c9f8> in <module>()
2 training_set = featursets[:2000]
3 testing_set = featursets[2000:]
----> 4 classifier = nltk.NaiveBayesClassifier.train(featursets)
5 print "Naive bayes classifier accuracy % = "(nltk.classify.accuracy(classifier, testing_set)*100)
6 classifier.show_informative_features(30)
/home/satyaki/.local/lib/python2.7/site-packages/nltk/classify/naivebayes.pyc in train(cls, labeled_featuresets, estimator)
194 for featureset, label in labeled_featuresets:
195 label_freqdist[label] += 1
--> 196 for fname, fval in featureset.items():
197 # Increment freq(fval|label, fname)
198 feature_freqdist[label, fname][fval] += 1
AttributeError: 'list' object has no attribute 'items'
But I'm not sure what went wrong here. Any help, guys?
Make your feature values as a dictionary.
Source : reference link
Snippet from train data[0] :
({'able': True,
'absurdly': True,
'alone': True,
'american': True,
'americans': True,
'anyone': True,
'appearance': True,
'applauding': True,
'atrocious': True,
'audience': True,
'audiences': True,
'aykroyd': True,
'bacall': True,
'band': True,
'banter': True,
'bicker': True,
'bits': True,
'brothers': True,
'chief': True,
'clude': True,
'comedy': True,
'commander': True,
'counted': True,
'crap': True,
'current': True,
'dan': True,
'dialogue': True,
'discriminating': True,
'dorothy': True,
'drowned': True,
'elvis': True,
'especially': True,
'even': True,
'ex': True,
'exchange': True,
'fellow': True,
'film': True,
'fine': True,
'first': True,
'fit': True,
'forget': True,
'former': True,
'funny': True,
'garner': True,
'gay': True,
'get': True,
'gets': True,
'going': True,
'grumpy': True,
'heard': True,
'heaven': True,
'help': True,
'holiday': True,
'honestly': True,
'immediately': True,
'impersonator': True,
'including': True,
'ing': True,
'ish': True,
'jack': True,
'james': True,
'john': True,
'joke': True,
'judas': True,
'lady': True,
'lauren': True,
'lemmon': True,
'macarena': True,
'march': True,
'marching': True,
'men': True,
'merely': True,
'mine': True,
'moment': True,
'movie': True,
'musical': True,
'non': True,
'number': True,
'offensively': True,
'old': True,
'older': True,
'one': True,
'overbearing': True,
'penis': True,
'perfect': True,
'performing': True,
'raw': True,
'references': True,
'resist': True,
'rest': True,
'ritual': True,
'road': True,
'room': True,
'scores': True,
'seeing': True,
'silence': True,
'single': True,
'slot': True,
'sold': True,
'star': True,
'submit': True,
'supporting': True,
'sure': True,
'talkin': True,
'tarheels': True,
'yup': True},
'negative')