问:
Converting the Select Case with To and Is to c# problems
I am convertng vb.net to c# 2010 as my job, and none of the automatic tools I have can succeed completely.   In special example, this case:

```<code>'searchString is a string paramter from a long method
Select Case searchString
   Case "paid"
      'Do something long here
   Case "oaaaaa" To "ozzzzzz", "maaaaaa" To "mzzzzzz"
      'Do other long code
   Case Else
      'other long code
End Select
</code>```

I am mostly java developer before this, so not great with c# and none with vb.net.  I do not understand the "oaaaa to ...." part and this part is not converting.  Can you please point me to right place to find the c# version of this?

答:
There isn't a direct equivalent in C# but you can easily achieve the same semantics (with more readable code!) using the following:</p>

```<code>if(searchString == "paid") {
    // do something here
}
else if(
    searchString.IsInRange("oaaaaa", "ozzzzzz") ||
    searchString.IsInRange("maaaaa", "mzzzzzz")
) {
    // do other long code
}
else {
    // other long code
}

public static class StringExtensions {
    public static bool IsInRange(this string s, string lower, string upper) {
    if(String.Compare(lower, upper) > 0) {
        throw new InvalidOperationException();
    }
    return String.Compare(s, lower) >= 0 && String.Compare(s, upper) <= 0
}
</code>```

Body:
I am convertng vb.net to c# 2010 as my job, and none of the automatic tools I have can succeed completely.   In special example, this case:

'searchString is a string paramter from a long method
Select Case searchString
   Case "paid"
      'Do something long here
   Case "oaaaaa" To "ozzzzzz", "maaaaaa" To "mzzzzzz"
      'Do other long code
   Case Else
      'other long code
End Select

I am mostly java developer before this, so not great with c# and none with vb.net.  I do not understand the "oaaaa to ...." part and this part is not converting.  Can you please point me to right place to find the c# version of this?

Body:
There isn't a direct equivalent in C# but you can easily achieve the same semantics (with more readable code!) using the following:

if(searchString == "paid") {
    // do something here
}
else if(
    searchString.IsInRange("oaaaaa", "ozzzzzz") ||
    searchString.IsInRange("maaaaa", "mzzzzzz")
) {
    // do other long code
}
else {
    // other long code
}

public static class StringExtensions {
    public static bool IsInRange(this string s, string lower, string upper) {
    if(String.Compare(lower, upper) > 0) {
        throw new InvalidOperationException();
    }
    return String.Compare(s, lower) >= 0 && String.Compare(s, upper) <= 0
}


来源: stackexchange

问:
Unable to send cross-domain request in `Firefox` extension
I am trying to access cross-domain data by using jsonp or XMLHttpRequest with GET method. My Code:

XMLHttpRequest

```<code>var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/ajax.php?code=BSE", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.responseText);
    }
xhr.send();
</code>```

JsonP

```<code>$.ajax({
     type: 'GET',
     url: "http://example.com/ajax.php?code=BSE",
     dataType: "jsonp",
     jsonpCallback: "jsonp_callback",
     crossDomain: true,
     success: function(res){
         console.log(res);
     }
});
</code>```

Both methods having same behavior. Whenever i am sending request its just keep loading (even i am not sure its sending request or not) and do nothing.

And my php code:

PHP Code:

```<code>header('content-type: application/json; charset=utf-8');
$dts=array('value'=>'123');
echo $_GET['jsonp_callback'] . '('.json_encode($dts).')';
</code>```

答:
Try using new XDomainRequest() in your xhr request. XDomainRequest is an implementation of HTTP access control (CORS).

```<code>var createCORSRequest = function(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Most browsers.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // IE8 & IE9
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
};

var url = 'http://example.com/ajax.php?code=BSE';
var method = 'GET';
var xhr = createCORSRequest(method, url);

xhr.onload = function() {
  // Success code goes here.
};

xhr.onerror = function() {
  // Error code goes here.
};

xhr.setRequestHeader('content-type', 'application/json; charset=utf-8');
xhr.send();
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I am trying to access cross-domain data by using jsonp or XMLHttpRequest with GET method. My Code:

XMLHttpRequest

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/ajax.php?code=BSE", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.responseText);
    }
xhr.send();

JsonP

$.ajax({
     type: 'GET',
     url: "http://example.com/ajax.php?code=BSE",
     dataType: "jsonp",
     jsonpCallback: "jsonp_callback",
     crossDomain: true,
     success: function(res){
         console.log(res);
     }
});

Both methods having same behavior. Whenever i am sending request its just keep loading (even i am not sure its sending request or not) and do nothing.

And my php code:

PHP Code:

header('content-type: application/json; charset=utf-8');
$dts=array('value'=>'123');
echo $_GET['jsonp_callback'] . '('.json_encode($dts).')';

Body:
Try using new XDomainRequest() in your xhr request. XDomainRequest is an implementation of HTTP access control (CORS).

var createCORSRequest = function(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Most browsers.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // IE8 & IE9
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
};

var url = 'http://example.com/ajax.php?code=BSE';
var method = 'GET';
var xhr = createCORSRequest(method, url);

xhr.onload = function() {
  // Success code goes here.
};

xhr.onerror = function() {
  // Error code goes here.
};

xhr.setRequestHeader('content-type', 'application/json; charset=utf-8');
xhr.send();


问:
Parameterised Test Execution in JUnit Jupiter(JUnit5)
Is there a way to have parameterized tests in JUnit Jupiter (Junit5)?

```<code>@RunWith(Parameterized.class)
</code>```

Background:

I went through How to write junit tests for interfaces? and wanted to implement test cases as answered here using JUnit5. But couldn't find the same class/es in the Jupiter package.

Attempts:

Looking further for substitutes I found that Extensions would be replacing the @RunWith as stated in Migrating tips

@RunWith no longer exists; superseded by @ExtendWith.

I tried defining a CustomExtension as provided in the sample  here - MockitoExtension but couldn't succeed in getting to use the instances of classes Parameterized.

Looking forward to suggestions on how to test interfaces with parameterized instances of classes implementing it in JUnit5.

答:
JUnit 5 M4 was just released and now supports parameterized tests.

Here's the "Hello World" example:

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
    assertNotNull(argument);
}
```

Please refer to the User Guide for the full documentation and additional examples.

Body:
Is there a way to have parameterized tests in JUnit Jupiter (Junit5)?

@RunWith(Parameterized.class)

Background:

I went through How to write junit tests for interfaces? and wanted to implement test cases as answered here using JUnit5. But couldn't find the same class/es in the Jupiter package.

Attempts:

Looking further for substitutes I found that Extensions would be replacing the @RunWith as stated in Migrating tips

@RunWith no longer exists; superseded by @ExtendWith.

I tried defining a CustomExtension as provided in the sample  here - MockitoExtension but couldn't succeed in getting to use the instances of classes Parameterized.

Looking forward to suggestions on how to test interfaces with parameterized instances of classes implementing it in JUnit5.

Body:
JUnit 5 M4 was just released and now supports parameterized tests.

Here's the "Hello World" example:

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
    assertNotNull(argument);
}

Please refer to the User Guide for the full documentation and additional examples.


问:
Getting the error "java.lang.UnsupportedOperationException: empty.maxBy" while executing PySpark locally
I am building a model using RandomForestCLassifier. This is my code,

```<code>conf = SparkConf()
conf.setAppName('spark-nltk')
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
m=sc.textFile("Question_Type_Classification_testing_purpose/data/TREC_10.txt").map(lambda s: s.split(" ",1))
df= m.toDF()
</code>```

The dataframe created has two default column names "_1" and "_2". Column "_1" has labels and column "_2" has the training data, which are plain text sentences.
I am performing following steps to create a model,

```<code>tokenizer = Tokenizer(inputCol="_2", outputCol="words")
tok= tokenizer.transform(df)
hashingTF = HashingTF(inputCol="words", outputCol="raw_features")
h=hashingTF.transform(tok)
indexer = StringIndexer(inputCol='_1', outputCol="idxlabel").fit(df)
idx=indexer.transform(h)
lr = RandomForestClassifier(labelCol="idxlabel").setFeaturesCol("raw_features")
model=lr.fit(idx)
</code>```

I know I can use Pipeline() method instead to do transform() every time, but I need to use my custom POS tagger also and there are some issues in persisting pipeline with a custom transformer. So I coined down to using the standard libraries. I issue the following command to submit my spark job,

```<code>spark-submit --driver-memory 5g Question_Type_Classification_testing_purpose/spark-nltk.py
</code>```

As I am running the job locally, I set my executor memory to 5g as the worker runs in the driver when spark runs in local mode.

```<code>17/04/01 02:59:19 INFO Executor: Running task 1.0 in stage 15.0 (TID 25)
17/04/01 02:59:19 INFO Executor: Running task 0.0 in stage 15.0 (TID 24)
17/04/01 02:59:19 INFO BlockManager: Found block rdd_38_1 locally
17/04/01 02:59:19 INFO BlockManager: Found block rdd_38_0 locally
17/04/01 02:59:19 INFO Executor: Finished task 1.0 in stage 15.0 (TID 25). 2432 bytes result sent to driver
17/04/01 02:59:19 INFO Executor: Finished task 0.0 in stage 15.0 (TID 24). 2432 bytes result sent to driver
17/04/01 02:59:19 INFO TaskSetManager: Finished task 1.0 in stage 15.0 (TID 25) in 390 ms on localhost (executor driver) (1/2)
17/04/01 02:59:19 INFO TaskSetManager: Finished task 0.0 in stage 15.0 (TID 24) in 390 ms on localhost (executor driver) (2/2)
17/04/01 02:59:19 INFO TaskSchedulerImpl: Removed TaskSet 15.0, whose tasks have all completed, from pool
17/04/01 02:59:19 INFO DAGScheduler: ShuffleMapStage 15 (mapPartitions at RandomForest.scala:534) finished in 0.390 s
17/04/01 02:59:19 INFO DAGScheduler: looking for newly runnable stages
17/04/01 02:59:19 INFO DAGScheduler: running: Set()
17/04/01 02:59:19 INFO DAGScheduler: waiting: Set(ResultStage 16)
17/04/01 02:59:19 INFO DAGScheduler: failed: Set()
17/04/01 02:59:19 INFO DAGScheduler: Submitting ResultStage 16 (MapPartitionsRDD[47] at map at RandomForest.scala:553), which has no missing parents
17/04/01 02:59:19 INFO MemoryStore: Block broadcast_20 stored as values in memory (estimated size 2.6 MB, free 1728.4 MB)
17/04/01 02:59:19 INFO MemoryStore: Block broadcast_20_piece0 stored as bytes in memory (estimated size 57.0 KB, free 1728.4 MB)
17/04/01 02:59:19 INFO BlockManagerInfo: Added broadcast_20_piece0 in memory on 192.168.56.1:55850 (size: 57.0 KB, free: 1757.9 MB)
17/04/01 02:59:19 INFO SparkContext: Created broadcast 20 from broadcast at DAGScheduler.scala:996
17/04/01 02:59:19 INFO DAGScheduler: Submitting 2 missing tasks from ResultStage 16 (MapPartitionsRDD[47] at map at RandomForest.scala:553)
17/04/01 02:59:19 INFO TaskSchedulerImpl: Adding task set 16.0 with 2 tasks
17/04/01 02:59:19 INFO TaskSetManager: Starting task 0.0 in stage 16.0 (TID 26, localhost, executor driver, partition 0, ANY, 5848 bytes)
17/04/01 02:59:19 INFO TaskSetManager: Starting task 1.0 in stage 16.0 (TID 27, localhost, executor driver, partition 1, ANY, 5848 bytes)
17/04/01 02:59:19 INFO Executor: Running task 0.0 in stage 16.0 (TID 26)
17/04/01 02:59:19 INFO Executor: Running task 1.0 in stage 16.0 (TID 27)
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Getting 2 non-empty blocks out of 2 blocks
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Started 0 remote fetches in 0 ms
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Getting 2 non-empty blocks out of 2 blocks
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Started 0 remote fetches in 0 ms
17/04/01 02:59:19 INFO Executor: Finished task 1.0 in stage 16.0 (TID 27). 14434 bytes result sent to driver
17/04/01 02:59:19 INFO TaskSetManager: Finished task 1.0 in stage 16.0 (TID 27) in 78 ms on localhost (executor driver) (1/2)
17/04/01 02:59:19 ERROR Executor: Exception in task 0.0 in stage 16.0 (TID 26)
java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
17/04/01 02:59:19 WARN TaskSetManager: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver): java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

17/04/01 02:59:19 ERROR TaskSetManager: Task 0 in stage 16.0 failed 1 times; aborting job
17/04/01 02:59:19 INFO TaskSchedulerImpl: Removed TaskSet 16.0, whose tasks have all completed, from pool
17/04/01 02:59:19 INFO TaskSchedulerImpl: Cancelling stage 16
17/04/01 02:59:19 INFO DAGScheduler: ResultStage 16 (collectAsMap at RandomForest.scala:563) failed in 0.328 s due to Job aborted due to stage failure: Task 0 in stage 16.0 failed 1 times, m
failure: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver): java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:
17/04/01 02:59:19 INFO DAGScheduler: Job 11 failed: collectAsMap at RandomForest.scala:563, took 1.057042 s
Traceback (most recent call last):
  File "C:/SPARK2.0/bin/Question_Type_Classification_testing_purpose/spark-nltk2.py", line 178, in <module>
    model=lr.fit(idx)
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\base.py", line 64, in fit
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\wrapper.py", line 236, in _fit
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\wrapper.py", line 233, in _fit_java
  File "C:\SPARK2.0\python\lib\py4j-0.10.4-src.zip\py4j\java_gateway.py", line 1133, in __call__
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\sql\utils.py", line 63, in deco
  File "C:\SPARK2.0\python\lib\py4j-0.10.4-src.zip\py4j\protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o86.fit.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 16.0 failed 1 times, most recent failure: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver
ng.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1435)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1423)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1422)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1422)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at scala.Option.foreach(Option.scala:257)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1650)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1605)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1594)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:628)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1918)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1931)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1944)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1958)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:934)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$collectAsMap$1.apply(PairRDDFunctions.scala:748)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$collectAsMap$1.apply(PairRDDFunctions.scala:747)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.PairRDDFunctions.collectAsMap(PairRDDFunctions.scala:747)
    at org.apache.spark.ml.tree.impl.RandomForest$.findBestSplits(RandomForest.scala:563)
    at org.apache.spark.ml.tree.impl.RandomForest$.run(RandomForest.scala:198)
    at org.apache.spark.ml.classification.RandomForestClassifier.train(RandomForestClassifier.scala:137)
    at org.apache.spark.ml.classification.RandomForestClassifier.train(RandomForestClassifier.scala:45)
    at org.apache.spark.ml.Predictor.fit(Predictor.scala:96)
    at org.apache.spark.ml.Predictor.fit(Predictor.scala:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more

17/04/01 02:59:20 INFO SparkContext: Invoking stop() from shutdown hook
17/04/01 02:59:20 INFO SparkUI: Stopped Spark web UI at http://192.168.56.1:4040
</code>```

My available physical RAM out of total 8G is 6G when my spark job is submitted, so I set the driver memory to 5g. Initially, I gave 4g but I got the "OutOfMemory" error, so I changed it to 5. My dataset size is quite small. It consists of 900 records, in the form of plain text sentences and the file size is 50Kb.

What can be the possible reason for this error? I tried reducing and increasing the data size, but nothing happened. Can anyone please let me know what am I doing wrong? Do I need to set any other conf variable? Is it due to RF by any chance? Any help is really appreciated. I am using PySpark 2.1 and Python 2.7 on Windows machine with 4 Cores.

答:
I am not sure what's wrong with RandomForest, but the above code works if I use the DecisionTreesClassifier. Maybe due to as RF is an ensemble approach, it might be trying to do some operation with the dataset that is causing the error. But, with DT, it's working fine.

For now, this is the only solution that I can think of. Please do let me know if someone figures it out what is the appropriate solution to the above problem.

Body:
I am building a model using RandomForestCLassifier. This is my code,

conf = SparkConf()
conf.setAppName('spark-nltk')
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
m=sc.textFile("Question_Type_Classification_testing_purpose/data/TREC_10.txt").map(lambda s: s.split(" ",1))
df= m.toDF()

The dataframe created has two default column names "_1" and "_2". Column "_1" has labels and column "_2" has the training data, which are plain text sentences.
I am performing following steps to create a model,

tokenizer = Tokenizer(inputCol="_2", outputCol="words")
tok= tokenizer.transform(df)
hashingTF = HashingTF(inputCol="words", outputCol="raw_features")
h=hashingTF.transform(tok)
indexer = StringIndexer(inputCol='_1', outputCol="idxlabel").fit(df)
idx=indexer.transform(h)
lr = RandomForestClassifier(labelCol="idxlabel").setFeaturesCol("raw_features")
model=lr.fit(idx)

I know I can use Pipeline() method instead to do transform() every time, but I need to use my custom POS tagger also and there are some issues in persisting pipeline with a custom transformer. So I coined down to using the standard libraries. I issue the following command to submit my spark job,

spark-submit --driver-memory 5g Question_Type_Classification_testing_purpose/spark-nltk.py

As I am running the job locally, I set my executor memory to 5g as the worker runs in the driver when spark runs in local mode.

17/04/01 02:59:19 INFO Executor: Running task 1.0 in stage 15.0 (TID 25)
17/04/01 02:59:19 INFO Executor: Running task 0.0 in stage 15.0 (TID 24)
17/04/01 02:59:19 INFO BlockManager: Found block rdd_38_1 locally
17/04/01 02:59:19 INFO BlockManager: Found block rdd_38_0 locally
17/04/01 02:59:19 INFO Executor: Finished task 1.0 in stage 15.0 (TID 25). 2432 bytes result sent to driver
17/04/01 02:59:19 INFO Executor: Finished task 0.0 in stage 15.0 (TID 24). 2432 bytes result sent to driver
17/04/01 02:59:19 INFO TaskSetManager: Finished task 1.0 in stage 15.0 (TID 25) in 390 ms on localhost (executor driver) (1/2)
17/04/01 02:59:19 INFO TaskSetManager: Finished task 0.0 in stage 15.0 (TID 24) in 390 ms on localhost (executor driver) (2/2)
17/04/01 02:59:19 INFO TaskSchedulerImpl: Removed TaskSet 15.0, whose tasks have all completed, from pool
17/04/01 02:59:19 INFO DAGScheduler: ShuffleMapStage 15 (mapPartitions at RandomForest.scala:534) finished in 0.390 s
17/04/01 02:59:19 INFO DAGScheduler: looking for newly runnable stages
17/04/01 02:59:19 INFO DAGScheduler: running: Set()
17/04/01 02:59:19 INFO DAGScheduler: waiting: Set(ResultStage 16)
17/04/01 02:59:19 INFO DAGScheduler: failed: Set()
17/04/01 02:59:19 INFO DAGScheduler: Submitting ResultStage 16 (MapPartitionsRDD[47] at map at RandomForest.scala:553), which has no missing parents
17/04/01 02:59:19 INFO MemoryStore: Block broadcast_20 stored as values in memory (estimated size 2.6 MB, free 1728.4 MB)
17/04/01 02:59:19 INFO MemoryStore: Block broadcast_20_piece0 stored as bytes in memory (estimated size 57.0 KB, free 1728.4 MB)
17/04/01 02:59:19 INFO BlockManagerInfo: Added broadcast_20_piece0 in memory on 192.168.56.1:55850 (size: 57.0 KB, free: 1757.9 MB)
17/04/01 02:59:19 INFO SparkContext: Created broadcast 20 from broadcast at DAGScheduler.scala:996
17/04/01 02:59:19 INFO DAGScheduler: Submitting 2 missing tasks from ResultStage 16 (MapPartitionsRDD[47] at map at RandomForest.scala:553)
17/04/01 02:59:19 INFO TaskSchedulerImpl: Adding task set 16.0 with 2 tasks
17/04/01 02:59:19 INFO TaskSetManager: Starting task 0.0 in stage 16.0 (TID 26, localhost, executor driver, partition 0, ANY, 5848 bytes)
17/04/01 02:59:19 INFO TaskSetManager: Starting task 1.0 in stage 16.0 (TID 27, localhost, executor driver, partition 1, ANY, 5848 bytes)
17/04/01 02:59:19 INFO Executor: Running task 0.0 in stage 16.0 (TID 26)
17/04/01 02:59:19 INFO Executor: Running task 1.0 in stage 16.0 (TID 27)
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Getting 2 non-empty blocks out of 2 blocks
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Started 0 remote fetches in 0 ms
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Getting 2 non-empty blocks out of 2 blocks
17/04/01 02:59:19 INFO ShuffleBlockFetcherIterator: Started 0 remote fetches in 0 ms
17/04/01 02:59:19 INFO Executor: Finished task 1.0 in stage 16.0 (TID 27). 14434 bytes result sent to driver
17/04/01 02:59:19 INFO TaskSetManager: Finished task 1.0 in stage 16.0 (TID 27) in 78 ms on localhost (executor driver) (1/2)
17/04/01 02:59:19 ERROR Executor: Exception in task 0.0 in stage 16.0 (TID 26)
java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
17/04/01 02:59:19 WARN TaskSetManager: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver): java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

17/04/01 02:59:19 ERROR TaskSetManager: Task 0 in stage 16.0 failed 1 times; aborting job
17/04/01 02:59:19 INFO TaskSchedulerImpl: Removed TaskSet 16.0, whose tasks have all completed, from pool
17/04/01 02:59:19 INFO TaskSchedulerImpl: Cancelling stage 16
17/04/01 02:59:19 INFO DAGScheduler: ResultStage 16 (collectAsMap at RandomForest.scala:563) failed in 0.328 s due to Job aborted due to stage failure: Task 0 in stage 16.0 failed 1 times, m
failure: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver): java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:
17/04/01 02:59:19 INFO DAGScheduler: Job 11 failed: collectAsMap at RandomForest.scala:563, took 1.057042 s
Traceback (most recent call last):
  File "C:/SPARK2.0/bin/Question_Type_Classification_testing_purpose/spark-nltk2.py", line 178, in <module>
    model=lr.fit(idx)
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\base.py", line 64, in fit
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\wrapper.py", line 236, in _fit
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\ml\wrapper.py", line 233, in _fit_java
  File "C:\SPARK2.0\python\lib\py4j-0.10.4-src.zip\py4j\java_gateway.py", line 1133, in __call__
  File "C:\SPARK2.0\python\lib\pyspark.zip\pyspark\sql\utils.py", line 63, in deco
  File "C:\SPARK2.0\python\lib\py4j-0.10.4-src.zip\py4j\protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o86.fit.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 16.0 failed 1 times, most recent failure: Lost task 0.0 in stage 16.0 (TID 26, localhost, executor driver
ng.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1435)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1423)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1422)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1422)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:802)
    at scala.Option.foreach(Option.scala:257)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:802)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1650)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1605)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1594)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:628)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1918)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1931)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1944)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1958)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:934)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$collectAsMap$1.apply(PairRDDFunctions.scala:748)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$collectAsMap$1.apply(PairRDDFunctions.scala:747)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:362)
    at org.apache.spark.rdd.PairRDDFunctions.collectAsMap(PairRDDFunctions.scala:747)
    at org.apache.spark.ml.tree.impl.RandomForest$.findBestSplits(RandomForest.scala:563)
    at org.apache.spark.ml.tree.impl.RandomForest$.run(RandomForest.scala:198)
    at org.apache.spark.ml.classification.RandomForestClassifier.train(RandomForestClassifier.scala:137)
    at org.apache.spark.ml.classification.RandomForestClassifier.train(RandomForestClassifier.scala:45)
    at org.apache.spark.ml.Predictor.fit(Predictor.scala:96)
    at org.apache.spark.ml.Predictor.fit(Predictor.scala:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.UnsupportedOperationException: empty.maxBy
    at scala.collection.TraversableOnce$class.maxBy(TraversableOnce.scala:236)
    at scala.collection.SeqViewLike$AbstractTransformed.maxBy(SeqViewLike.scala:37)
    at org.apache.spark.ml.tree.impl.RandomForest$.binsToBestSplit(RandomForest.scala:831)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:561)
    at org.apache.spark.ml.tree.impl.RandomForest$$anonfun$14.apply(RandomForest.scala:553)
    at scala.collection.Iterator$$anon$11.next(Iterator.scala:409)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at scala.collection.AbstractIterator.to(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1336)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at scala.collection.AbstractIterator.toArray(Iterator.scala:1336)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:935)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1944)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:99)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more

17/04/01 02:59:20 INFO SparkContext: Invoking stop() from shutdown hook
17/04/01 02:59:20 INFO SparkUI: Stopped Spark web UI at http://192.168.56.1:4040

My available physical RAM out of total 8G is 6G when my spark job is submitted, so I set the driver memory to 5g. Initially, I gave 4g but I got the "OutOfMemory" error, so I changed it to 5. My dataset size is quite small. It consists of 900 records, in the form of plain text sentences and the file size is 50Kb.

What can be the possible reason for this error? I tried reducing and increasing the data size, but nothing happened. Can anyone please let me know what am I doing wrong? Do I need to set any other conf variable? Is it due to RF by any chance? Any help is really appreciated. I am using PySpark 2.1 and Python 2.7 on Windows machine with 4 Cores.


问:
How create asimetric cone in matlab?
I need to create a surface generate by rotation on y axis of shape formed by two curves. I already have the equation for this two curves. Here is the shape

I already create the surface but for simetric cone formed by one of this curve, here is my script:

```<code>h=20;
rw=1;
hw=5;
L=25;
r=linspace(rw,L,50);
theta=linspace(0,2*pi,25);
[r,theta] = meshgrid(r,theta);
xx=r.*cos(theta);
yy=r.*sin(theta);
z=sqrt(log(r/rw)*(h^2-hw^2)/log(L/rw)+hw^2); %Fuction of curve
surf(xx,yy,z)
</code>```

Here is my result

In the other curve is the same fuction (z) but change r,h,L

Thanks for your help,

答:
you should use parameter grids instead of scalars:

```<code>% parameters
h=[20 25];
rw=1;
hw=5;
L=[25 30];
nr = 50;
nt = 25;
theta=linspace(0,2*pi,nt);
rmax = linspace(L(1),L(2),floor((nt+1)/2));
% generating **grids** for all parameters
l = [linspace(L(1),L(2),floor((nt+1)/2)) linspace(L(2),L(1),floor((nt)/2))];
hh = [linspace(h(1),h(2),floor((nt+1)/2)) linspace(h(2),h(1),floor((nt)/2))];
[~,thetaGrid] = meshgrid(1:nr,theta);
[~,lGrid] = meshgrid(1:nr,l);
[~,hGrid] = meshgrid(1:nr,hh);
rGrid = zeros(size(thetaGrid));
for ii = 1:floor((nt+1)/2)
    rGrid(ii,:) = linspace(rw,rmax(ii),nr);
end
rGrid(ceil((nt+1)/2):end,:) = rGrid(floor((nt+1)/2):-1:1,:);
% set coordinate grids
xx=rGrid.*cos(thetaGrid);
yy=rGrid.*sin(thetaGrid);
z=sqrt(log(rGrid./rw).*(hGrid.^2-hw^2)./log(lGrid./rw)+hw.^2); %Fuction of curve
% plot
surf(xx,yy,z)
</code>```

Title: How create asimetric cone in matlab?

Body:
I need to create a surface generate by rotation on y axis of shape formed by two curves. I already have the equation for this two curves. Here is the shape

I already create the surface but for simetric cone formed by one of this curve, here is my script:

h=20;
rw=1;
hw=5;
L=25;
r=linspace(rw,L,50);
theta=linspace(0,2*pi,25);
[r,theta] = meshgrid(r,theta);
xx=r.*cos(theta);
yy=r.*sin(theta);
z=sqrt(log(r/rw)*(h^2-hw^2)/log(L/rw)+hw^2); %Fuction of curve
surf(xx,yy,z)

Here is my result

In the other curve is the same fuction (z) but change r,h,L

Thanks for your help,

Body:
you should use parameter grids instead of scalars:

% parameters
h=[20 25];
rw=1;
hw=5;
L=[25 30];
nr = 50;
nt = 25;
theta=linspace(0,2*pi,nt);
rmax = linspace(L(1),L(2),floor((nt+1)/2));
% generating **grids** for all parameters
l = [linspace(L(1),L(2),floor((nt+1)/2)) linspace(L(2),L(1),floor((nt)/2))];
hh = [linspace(h(1),h(2),floor((nt+1)/2)) linspace(h(2),h(1),floor((nt)/2))];
[~,thetaGrid] = meshgrid(1:nr,theta);
[~,lGrid] = meshgrid(1:nr,l);
[~,hGrid] = meshgrid(1:nr,hh);
rGrid = zeros(size(thetaGrid));
for ii = 1:floor((nt+1)/2)
    rGrid(ii,:) = linspace(rw,rmax(ii),nr);
end
rGrid(ceil((nt+1)/2):end,:) = rGrid(floor((nt+1)/2):-1:1,:);
% set coordinate grids
xx=rGrid.*cos(thetaGrid);
yy=rGrid.*sin(thetaGrid);
z=sqrt(log(rGrid./rw).*(hGrid.^2-hw^2)./log(lGrid./rw)+hw.^2); %Fuction of curve
% plot
surf(xx,yy,z)


问:
Ruby on Rails running on a vps slice
What are the major gotchas and tricks when running Rails on a small slice (256MB)?  What is the best server setup for cramped quarters?  Passenger, Nginx, Mongrel?  What is the best background task processor in this kind of environment?  What do you need to watch out for? I'd love to hear the stories of people who have done this and bumped into some of the virtual walls.</p>

答: On my 256 slice I run nginx in front of a pair of thin servers. Whatever you decide to go with make sure you're using god or monit to keep your stack in check.</p>

OwnerDisplayName: srboisvert

ContentLicense: CC BY-SA 2.5

Title: Ruby on Rails running on a vps slice

OwnerDisplayName: Mike Breen


问:
Graphical open source text editor for large text files (> 200 MBytes)
Is there an open source alternative (similar to ultraedit) to handle files with filesize >200 MBytes?</p>

答: JujuEdit is the correct software for this. It won't load the whole file into memory so it's lightning fast when opening even 1 GB+ files. Note: It's not open source (it's freeware) and it is not in active development.</p>

OwnerDisplayName: Dirk

LastEditorDisplayName: Dirk


问:
How to filter by category in Magento 2's API?
In Magento 2's REST API, there is an option to search a product using various search criteria. As you know, an example is given below:

```<code>http://magentohost/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name& searchCriteria[filter_groups][0][filters][0][value]=%macbook%& searchCriteria[filter_groups][0][filters][0][condition_type]=like
</code>```

But I have not found an option to search by category.

How can we do that?

答:
To search by category, it is simple. You have to just pass category_id as a field. Have a look at below example:

```<code>http://magentohost/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id& searchCriteria[filterGroups][0][filters][0][value]=4& searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at& searchCriteria[sortOrders][0][direction]=DESC& searchCriteria[pageSize]=10& searchCriteria[currentPage]=1
</code>```

ContentLicense: CC BY-SA 4.0

Body:
In Magento 2's REST API, there is an option to search a product using various search criteria. As you know, an example is given below:

http://magentohost/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name& searchCriteria[filter_groups][0][filters][0][value]=%macbook%& searchCriteria[filter_groups][0][filters][0][condition_type]=like

But I have not found an option to search by category.

How can we do that?

Body:
To search by category, it is simple. You have to just pass category_id as a field. Have a look at below example:

http://magentohost/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=category_id& searchCriteria[filterGroups][0][filters][0][value]=4& searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[sortOrders][0][field]=created_at& searchCriteria[sortOrders][0][direction]=DESC& searchCriteria[pageSize]=10& searchCriteria[currentPage]=1


问:
Drools :- multiple(thousands) .drl files , each having multiple rules , execution of each file should be controlled as required
I'm new to drools rule engine. In my project, I have multiple .drl files of drools to run. But readknowledgebase() takes time to run rules. So, I want to learn the best way of loading all rule files (multiple .drl files) once, and then run the rules of each .drl file one after another as required, instead of running all rules of all .drl files each time I make a call to knowledgebase().

答:
multiple way we approach this problm
1.use whole,part concept
2. use onDemand concept
3.use Kie stateless session + runtime drools table loading</p>


问:
C++ pointer/for-loop confusion
I'm trying to write a program that reads values from cin using a pointer, and then outputs the values alongside their position in the array. I can't figure out why printNumbers1 works but printNumbers2 doesn't. Here is the program (relevant code near the bottom):

```<code>#include <iostream>

using namespace std;

int *readNumbers(int);
void printNumbers1(int*);
void printNumbers2(int*);

int main()
{
    int *numbers = readNumbers(5);
    printNumbers1(numbers);
    printNumbers2(numbers);
    return 0;
}

int *readNumbers(int n)
{
    int a[n];
    int *numbers;
    numbers = &a[0];
    for (int i=0; i<n; i++)
    {
        cin >> *(numbers+i);
    }

    return numbers;
}

void printNumbers1(int *numbers)
{
    cout << 0 << ' ' << *(numbers) << endl
         << 1 << ' ' << *(numbers+1) << endl
         << 2 << ' ' << *(numbers+2) << endl
         << 3 << ' ' << *(numbers+3) << endl
         << 4 << ' ' << *(numbers+4) << endl;
}

void printNumbers2(int *numbers)
{
    for (int i=0; i<5; i++)
    {
        cout << i << ' ' << *(numbers+i) << endl;
    }
}
</code>```

When I run the program, it works as intended for printNumbers1 but outputs a combination of seemingly random numbers and 0s for printNumbers2. I feel like the two printNumbers functions should should function identically, but they don't. What am I missing?

答:
This happens because of a combination of two things:

C++ does not allow variable-length arrays - this is a popular extension, but the declaration int a[n] is not standard.

You cannot return a pointer to local variable from a function - pointer numbers inside readNumbers points to a, a local variable. You can use this pointer inside the function, but outside of the function it becomes invalid, because a goes out of scope.

Using an out-of-scope variable causes undefined behavior. This Q&A provides a very good explanation of what is happening, and why it may look like the program is working fine.

If you want to use built-in pointers, remove int a[n], and change the declaration of numbers as follows:

```<code>int *numbers = new int[n];
</code>```

You also need to add

```<code>delete[] numbers;
</code>```

before return 0 line to avoid memory leaks.

I am assuming that you wrote this code as part of a learning exercise. In general, though, a better approach in C++ is to use std::vector<int>, which hides pointer operations from your code, and deals with resource management for you.

Title: C++ pointer/for-loop confusion

Body:
I'm trying to write a program that reads values from cin using a pointer, and then outputs the values alongside their position in the array. I can't figure out why printNumbers1 works but printNumbers2 doesn't. Here is the program (relevant code near the bottom):

#include <iostream>

using namespace std;

int *readNumbers(int);
void printNumbers1(int*);
void printNumbers2(int*);

int main()
{
    int *numbers = readNumbers(5);
    printNumbers1(numbers);
    printNumbers2(numbers);
    return 0;
}

int *readNumbers(int n)
{
    int a[n];
    int *numbers;
    numbers = &a[0];
    for (int i=0; i<n; i++)
    {
        cin >> *(numbers+i);
    }

    return numbers;
}

void printNumbers1(int *numbers)
{
    cout << 0 << ' ' << *(numbers) << endl
         << 1 << ' ' << *(numbers+1) << endl
         << 2 << ' ' << *(numbers+2) << endl
         << 3 << ' ' << *(numbers+3) << endl
         << 4 << ' ' << *(numbers+4) << endl;
}

void printNumbers2(int *numbers)
{
    for (int i=0; i<5; i++)
    {
        cout << i << ' ' << *(numbers+i) << endl;
    }
}

When I run the program, it works as intended for printNumbers1 but outputs a combination of seemingly random numbers and 0s for printNumbers2. I feel like the two printNumbers functions should should function identically, but they don't. What am I missing?

Body:
This happens because of a combination of two things:

C++ does not allow variable-length arrays - this is a popular extension, but the declaration int a[n] is not standard.

You cannot return a pointer to local variable from a function - pointer numbers inside readNumbers points to a, a local variable. You can use this pointer inside the function, but outside of the function it becomes invalid, because a goes out of scope.

Using an out-of-scope variable causes undefined behavior. This Q&A provides a very good explanation of what is happening, and why it may look like the program is working fine.

If you want to use built-in pointers, remove int a[n], and change the declaration of numbers as follows:

int *numbers = new int[n];

You also need to add

delete[] numbers;

before return 0 line to avoid memory leaks.

I am assuming that you wrote this code as part of a learning exercise. In general, though, a better approach in C++ is to use std::vector<int>, which hides pointer operations from your code, and deals with resource management for you.


问:
How to make links active with jquery in rails
I'm trying get some active links in my navbar and failing big time, what im doing wrong?

HTML:

```<code><div class="navbar-links text-center hidden-xs hidden-sm">
  <%= link_to "HOME", "#one", class: "navbar-links link-home" %>
  <%= link_to "FEATURES", "#features-container", class: "navbar-link link-home" %>
  <%= link_to "TEMPLATES", "#templates", class: "navbar-links link-home" %>
  <%= link_to "CONTACT", "#map", class: "navbar-links link-home" %>
</div>
</code>```

CSS:

```<code>.active-link {
  color: #C99D9A;
  transition: all 0.3s ease;
  opacity: 80%;
  border-bottom: 3px solid #C99D9A;
}
</code>```

JQUERY:

```<code>$('.navbar-links a').on('click', function(){
  $('this').removeClass('.active-link');
  $(this).addClass('.active-link');
});
</code>```

答:
$('.menu_ul li').click(function(){
  $(this).addClass('active').siblings().removeClass('active');
  //$(this).addClass('.active-link');
});```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.menu_ul{
  list-style-type: none;
}
.active{
  background: red;
}</code>```

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

Home

About

Blog

Contact

```

Body:
I'm trying get some active links in my navbar and failing big time, what im doing wrong?

HTML:

  <%= link_to "HOME", "#one", class: "navbar-links link-home" %>
  <%= link_to "FEATURES", "#features-container", class: "navbar-link link-home" %>
  <%= link_to "TEMPLATES", "#templates", class: "navbar-links link-home" %>
  <%= link_to "CONTACT", "#map", class: "navbar-links link-home" %>

CSS:

.active-link {
  color: #C99D9A;
  transition: all 0.3s ease;
  opacity: 80%;
  border-bottom: 3px solid #C99D9A;
}

JQUERY:

$('.navbar-links a').on('click', function(){
  $('this').removeClass('.active-link');
  $(this).addClass('.active-link');
});

Body:
$('.menu_ul li').click(function(){
  $(this).addClass('active').siblings().removeClass('active');
  //$(this).addClass('.active-link');
});

.menu_ul{
  list-style-type: none;
}
.active{
  background: red;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="navbar-links text-center">
<ul class="menu_ul">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>


答:
readNumbers returns a pointer to a variable with automatic storage duration.

The behaviour on dereferencing that pointer is undefined.

Use a std::vector instead. Rely on return value optimisation to obviate any superfluous value copies being taken.

Title: C++ pointer/for-loop confusion


问:
Oracle database not recognizing type when using procedure
I declared my types in the package header:

file_2.sql

```<code>CREATE OR REPLACE PACKAGE MY_UTILS
IS
TYPE VECTOR IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
TYPE MATRIX IS TABLE OF VECTOR INDEX BY PLS_INTEGER;
PROCEDURE PRINT_MATRIX(p_matrix IN MATRIX);
END MY_UTILS;
</code>```

My print matrix procedure

```<code>PROCEDURE PRINT_MATRIX(p_matrix IN MATRIX)  IS
BEGIN
DBMS_OUTPUT.PUT_LINE('printing matrix..');
END PRINT_MATRIX;
</code>```

The PRINT_MATRIX procedure does a simple DBMS_OUTPUT.PUT_LINE to print the matrix; When I try to use my MY_UTILS.PRINT_MATRIX(v_matrix) I get the error:

PLS-00306: wrong number or types of arguments in call to
  'PRINT_MATRIX'

In my other .sql file I also declare the MATRIX type the same as in the package.

file_1.sql

```<code>set serveroutput ON;
DECLARE
TYPE VECTOR IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
TYPE MATRIX IS TABLE OF VECTOR INDEX BY PLS_INTEGER;
v_matrix MATRIX;
BEGIN
--populate matrix, printing it works with 2 FOR LOOPS
MY_UTILS.PRINT_MATRIX(v_matrix); -- error
END;
</code>```

答:
Oracle sees your variable as a different type to the formal argument. To you they look identical, buy to Oracle they are two different types.

From the documentation:

A collection type defined in a package specification is incompatible with an identically defined local or standalone collection type.

So you have to change your variable declaration to use the type from the package:

```<code>DECLARE
  v_matrix MY_UTILS.MATRIX;
BEGIN
  --populate matrix, printing it works with 2 FOR LOOPS
  MY_UTILS.PRINT_MATRIX(v_matrix); -- error
END;
</code>```

Body:
I declared my types in the package header:

file_2.sql

CREATE OR REPLACE PACKAGE MY_UTILS
IS
TYPE VECTOR IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
TYPE MATRIX IS TABLE OF VECTOR INDEX BY PLS_INTEGER;
PROCEDURE PRINT_MATRIX(p_matrix IN MATRIX);
END MY_UTILS;

My print matrix procedure

PROCEDURE PRINT_MATRIX(p_matrix IN MATRIX)  IS
BEGIN
DBMS_OUTPUT.PUT_LINE('printing matrix..');
END PRINT_MATRIX;

The PRINT_MATRIX procedure does a simple DBMS_OUTPUT.PUT_LINE to print the matrix; When I try to use my MY_UTILS.PRINT_MATRIX(v_matrix) I get the error:

PLS-00306: wrong number or types of arguments in call to
  'PRINT_MATRIX'

In my other .sql file I also declare the MATRIX type the same as in the package.

file_1.sql

set serveroutput ON;
DECLARE
TYPE VECTOR IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
TYPE MATRIX IS TABLE OF VECTOR INDEX BY PLS_INTEGER;
v_matrix MATRIX;
BEGIN
--populate matrix, printing it works with 2 FOR LOOPS
MY_UTILS.PRINT_MATRIX(v_matrix); -- error
END;

Body:
Oracle sees your variable as a different type to the formal argument. To you they look identical, buy to Oracle they are two different types.

From the documentation:

A collection type defined in a package specification is incompatible with an identically defined local or standalone collection type.

So you have to change your variable declaration to use the type from the package:

DECLARE
  v_matrix MY_UTILS.MATRIX;
BEGIN
  --populate matrix, printing it works with 2 FOR LOOPS
  MY_UTILS.PRINT_MATRIX(v_matrix); -- error
END;


问:
How to create modal box using html, css and jquery?
How to create modal box using html, css and jquery?
    </p>

```<code><button id="myBtn">Open Modal</button>
</code>```

<p>
    </p>

```<code>  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>
</code>```

答:
For creating modal, you can also use some libraries.

One of them you can use w3css library

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

    <button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Click Me to open modal</button>

          ×

Some text. Some text. Some text.

Some text. Some text. Some text.

```
</div>
</div>
</p>

<p>Using Jquery and given example by Leon
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {

  $("#myBtn").on("click", function() {
    $("#modalContainer").show();
  });

  $(".close").on("click", function() {
    $("#modalContainer").hide();
  });

});</code>```

#modalContainer {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Click Me

<div id="modalContainer">

  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div></code>```

Body:
How to create modal box using html, css and jquery?

<button id="myBtn">Open Modal</button>

    ×

Some text in the Modal..

Body:
For creating modal, you can also use some libraries.

One of them you can use w3css library

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<body>
  <div class="w3-container">
    <button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Click Me to open modal</button>

    <div id="id01" class="w3-modal">
      <div class="w3-modal-content">
        <div class="w3-container">
          <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">&times;</span>
          <p>Some text. Some text. Some text.</p>
          <p>Some text. Some text. Some text.</p>
        </div>
      </div>
    </div>
  </div>

</body>

Using Jquery and given example by Leon

$(document).ready(function() {

  $("#myBtn").on("click", function() {
    $("#modalContainer").show();
  });

  $(".close").on("click", function() {
    $("#modalContainer").hide();
  });

});

#modalContainer {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="#" id="myBtn">Click Me</a>

<div id="modalContainer">

  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>


问:
Integer Linear Least Squares
So I have to find a x that minimizes norm(A.dot(x) - y, 2) A is a matrix and y is a vector.

This could be easily accomplished with scipy.optimize.lsq_linear or numpy.linalg.lstsq but I need x to be integers. In general "Integer programming" is NP-complete.
I did find Routines for solving the standard integer least squares problem but I thought I'd ask before I convert it from matlab.

Is there an established library that can solve the Integer Linear Least Squares in python?

答:
You could use one of the optimization libraries available for python than can handle (mixed) integer programming. Do a google search and you will find many. Since your problem is convex, cvxpy can be used as a nice interface to many of them. Here's a toy example using a built-in integer programming solver (which may not be very efficient for large scale problems)

```<code>import numpy as np
import cvxpy

np.random.seed(123) # for reproducability

# generate A and y
m, n = 10, 10
A = np.random.randn(m,n)
y = np.random.randn(m)

# declare the integer-valued optimization variable
x = cvxpy.Int(n)

# set up the L2-norm minimization problem
obj = cvxpy.Minimize(cvxpy.norm(A * x - y, 2))
prob = cvxpy.Problem(obj)

# solve the problem using an appropriate solver
sol = prob.solve(solver = 'ECOS_BB')

# the optimal value of x is
print(x.value)
</code>```

```<code>[[-13.]
 [ -3.]
 [  3.]
 [  6.]
 [  1.]
 [ -5.]
 [ -1.]
 [ -3.]
 [ -2.]
 [ -6.]]
</code>```

Title: Integer Linear Least Squares

Body:
You could use one of the optimization libraries available for python than can handle (mixed) integer programming. Do a google search and you will find many. Since your problem is convex, cvxpy can be used as a nice interface to many of them. Here's a toy example using a built-in integer programming solver (which may not be very efficient for large scale problems)

import numpy as np
import cvxpy

np.random.seed(123) # for reproducability

# generate A and y
m, n = 10, 10
A = np.random.randn(m,n)
y = np.random.randn(m)

# declare the integer-valued optimization variable
x = cvxpy.Int(n)

# set up the L2-norm minimization problem
obj = cvxpy.Minimize(cvxpy.norm(A * x - y, 2))
prob = cvxpy.Problem(obj)

# solve the problem using an appropriate solver
sol = prob.solve(solver = 'ECOS_BB')

# the optimal value of x is
print(x.value)

[[-13.]
 [ -3.]
 [  3.]
 [  6.]
 [  1.]
 [ -5.]
 [ -1.]
 [ -3.]
 [ -2.]
 [ -6.]]


问:
Display Activity Indicator while app is busy
I'm developing a Cross Platform mobile app with Xamarin.Forms.

I'm trying to simulate a POST on web service with a simple Activity Indicator and I button. I want that on button click the Activity Indicator displays and remains until IsLoading property is set to false.

Here my XAML:

```<code><AbsoluteLayout x:Name="layOuter">
    <ActivityIndicator x:Name="actWebRequest"
        IsVisible="{ Binding IsLoading,
                             Mode=TwoWay }"
        IsRunning="{ Binding IsLoading,
                             Mode=TwoWay }"
        AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 100"
        AbsoluteLayout.LayoutFlags="PositionProportional" />

    <Button x:Name="btnLogin"
                    IsVisible="{ Binding Show,
                             Mode=TwoWay }"
                    Text="{ StaticResource LoginButtonText }"
                    Command="{ Binding LoginCommand }"
                    BackgroundColor="Teal" />
</AbsoluteLayout>
</code>```

And he the LoginCommand:

public event PropertyChangedEventHandler PropertyChanged;

```<code>    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingField, value))
            return;

        backingField = value;

        OnPropertyChanged(propertyName);
    }

    private bool _isLoading;
    private bool _show;

    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } }
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } }

    private void Login()
    {
        IsLoading = true;
        Show = false;

        // DoSomething()
        System.Threading.Tasks.Task.Delay(5000).Wait();

        IsLoading = false;
        Show = true;
    }
</code>```

By tapping the Login button nothing happens, and after 5 seconds the application navigates to another. I also tried ACR User Dialogs but it doesn't works.

My app must be Android and iOS compatible.

Can you help me?
Thanks.

EDIT:

Yes, I binded the page to WievModel in this way:

```<code>public class ContentBase<T> : ContentPage where T : class, new()
{
    public T ViewModel
    {
        get { return BindingContext as T; }
        set { BindingContext = value; }
    }

    public ContentBase() { ViewModel = new T(); }
}

public class LoginPageBase : ContentBase<LoginViewModel> { }

public partial class LoginPage : LoginPageBase
{
    public LoginPage()
    {
        InitializeComponent();
    }

    protected override bool OnBackButtonPressed()
    {
        return true;
    }
}

public class LoginViewModel : ViewModelBase
{
    private bool _isLoading;
    private bool _show;
    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } }
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } }
}
</code>```

What I mean is that when I tap the Login button the Activity Indicator doesn't show even if I set IsLoading to true.

答:
The problem is obvious:

```<code>System.Threading.Tasks.Task.Delay(5000).Wait();
</code>```

You are blocking the main thread with Wait(), await it instead:

```<code>await System.Threading.Tasks.Task.Delay(5000);
</code>```

Then you should see the activity indicator.

Body:
I'm developing a Cross Platform mobile app with Xamarin.Forms.

I'm trying to simulate a POST on web service with a simple Activity Indicator and I button. I want that on button click the Activity Indicator displays and remains until IsLoading property is set to false.

Here my XAML:

<AbsoluteLayout x:Name="layOuter">
    <ActivityIndicator x:Name="actWebRequest"
        IsVisible="{ Binding IsLoading,
                             Mode=TwoWay }"
        IsRunning="{ Binding IsLoading,
                             Mode=TwoWay }"
        AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 100"
        AbsoluteLayout.LayoutFlags="PositionProportional" />

    <Button x:Name="btnLogin"
                    IsVisible="{ Binding Show,
                             Mode=TwoWay }"
                    Text="{ StaticResource LoginButtonText }"
                    Command="{ Binding LoginCommand }"
                    BackgroundColor="Teal" />
</AbsoluteLayout>

And he the LoginCommand:

public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingField, value))
            return;

        backingField = value;

        OnPropertyChanged(propertyName);
    }

    private bool _isLoading;
    private bool _show;

    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } }
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } }

    private void Login()
    {
        IsLoading = true;
        Show = false;

        // DoSomething()
        System.Threading.Tasks.Task.Delay(5000).Wait();

        IsLoading = false;
        Show = true;
    }

By tapping the Login button nothing happens, and after 5 seconds the application navigates to another. I also tried ACR User Dialogs but it doesn't works.

My app must be Android and iOS compatible.

Can you help me?
Thanks.

EDIT:

Yes, I binded the page to WievModel in this way:

public class ContentBase<T> : ContentPage where T : class, new()
{
    public T ViewModel
    {
        get { return BindingContext as T; }
        set { BindingContext = value; }
    }

    public ContentBase() { ViewModel = new T(); }
}

public class LoginPageBase : ContentBase<LoginViewModel> { }

public partial class LoginPage : LoginPageBase
{
    public LoginPage()
    {
        InitializeComponent();
    }

    protected override bool OnBackButtonPressed()
    {
        return true;
    }
}

public class LoginViewModel : ViewModelBase
{
    private bool _isLoading;
    private bool _show;
    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } }
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } }
}

What I mean is that when I tap the Login button the Activity Indicator doesn't show even if I set IsLoading to true.

Body:
The problem is obvious:

System.Threading.Tasks.Task.Delay(5000).Wait();

You are blocking the main thread with Wait(), await it instead:

await System.Threading.Tasks.Task.Delay(5000);

Then you should see the activity indicator.


问:
How to search string in NSArray of NSObject Swift 3.0?
I am working on UiSearchbar,where if an user type some string, i have to show him the related suggestions. Right now the array is containing data in format like below:

```<code>"Name: Optional(\"2.1 - Classroom\")",
"Name: Optional(\"2.11 - Classroom\")",
"Name: Optional(\"2.22 - Classroom\")",
"Name: Optional(\"2.13 - Classroom\")",
"Name: Optional(\"2.34 - Classroom\")",
"Name: Optional(\"2.55 - Classroom\")"
</code>```

What I want to do is, when an user type 2.1 then i want to suggest him all the names start with 2.1 . I have tried all the suggestions i got from internet. However, i am new to iOS so i can't figure out that how i can do this.
This is my code :

```<code>func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
    if searchText.characters.count == 0{
        isSearch = false;
        self.resulttableview.reloadData()
    }else{
        let filtered  = list.filter(){
            return ($0 as AnyObject).contains(searchText) // 2.1 or searchtext
        }
        print(filtered)
    }
}
</code>```

For assigning the array

```<code>func itemsDownloaded(items: NSArray){
    list = items
    self.resulttableview.reloadData()
}
</code>```

And this code is for retrieving data from JSON and store in mutable array :

```<code>func downloadItems(){
    let url = NSURL(string: urlPath)!

    var session: URLSession!
    let configuration = URLSessionConfiguration.default

    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    let task = session.dataTask(with: url as URL)

    task.resume()
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data){
    self.data.append(data);
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?){
    if error != nil{
        print("Failed to download data")
    }else{
        print("Data downloaded")
        self.parseJSON()
    }
}

func parseJSON(){
    var jsonResult: NSMutableArray = NSMutableArray()

    do{
        let jsonResults = try JSONSerialization.jsonObject(with: self.data as Data , options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
        jsonResult = jsonResults.mutableCopy() as! NSMutableArray
    }catch let error as NSError{
        print(error)
    }

    var jsonElement = NSDictionary()
    let locations: NSMutableArray = NSMutableArray()

    for i in 0 ..< jsonResult.count{
        jsonElement = jsonResult[i] as! NSDictionary
        let location = roommodel()

        if let name = jsonElement["name"] as? String{
            location.name = name
        }

        locations.add(location)
    }

    DispatchQueue.main.async{
        self.delegate.itemsDownloaded(items: locations)
    }

}
</code>```

Thanks in advance

答:
try this </p>

```<code>let filtered  = list.filter(){

        return ($0 as AnyObject).contains("2.1") // 2.1 or searchtext

        }
        print(filtered)
</code>```

Body:
I am working on UiSearchbar,where if an user type some string, i have to show him the related suggestions. Right now the array is containing data in format like below:

"Name: Optional(\"2.1 - Classroom\")",
"Name: Optional(\"2.11 - Classroom\")",
"Name: Optional(\"2.22 - Classroom\")",
"Name: Optional(\"2.13 - Classroom\")",
"Name: Optional(\"2.34 - Classroom\")",
"Name: Optional(\"2.55 - Classroom\")"

What I want to do is, when an user type 2.1 then i want to suggest him all the names start with 2.1 . I have tried all the suggestions i got from internet. However, i am new to iOS so i can't figure out that how i can do this.
This is my code :

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
    if searchText.characters.count == 0{
        isSearch = false;
        self.resulttableview.reloadData()
    }else{
        let filtered  = list.filter(){
            return ($0 as AnyObject).contains(searchText) // 2.1 or searchtext
        }
        print(filtered)
    }
}

For assigning the array

func itemsDownloaded(items: NSArray){
    list = items
    self.resulttableview.reloadData()
}

And this code is for retrieving data from JSON and store in mutable array :

func downloadItems(){
    let url = NSURL(string: urlPath)!

    var session: URLSession!
    let configuration = URLSessionConfiguration.default

    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    let task = session.dataTask(with: url as URL)

    task.resume()
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data){
    self.data.append(data);
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?){
    if error != nil{
        print("Failed to download data")
    }else{
        print("Data downloaded")
        self.parseJSON()
    }
}

func parseJSON(){
    var jsonResult: NSMutableArray = NSMutableArray()

    do{
        let jsonResults = try JSONSerialization.jsonObject(with: self.data as Data , options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
        jsonResult = jsonResults.mutableCopy() as! NSMutableArray
    }catch let error as NSError{
        print(error)
    }

    var jsonElement = NSDictionary()
    let locations: NSMutableArray = NSMutableArray()

    for i in 0 ..< jsonResult.count{
        jsonElement = jsonResult[i] as! NSDictionary
        let location = roommodel()

        if let name = jsonElement["name"] as? String{
            location.name = name
        }

        locations.add(location)
    }

    DispatchQueue.main.async{
        self.delegate.itemsDownloaded(items: locations)
    }

}

Thanks in advance

Body:
try this

let filtered  = list.filter(){

        return ($0 as AnyObject).contains("2.1") // 2.1 or searchtext

        }
        print(filtered)