{
  "id": 9800,
  "title": "Reading Russian UTF8 data in Windows with R",
  "url": "/competitions/avito-prohibited-content/discussion/9800",
  "author_name": "",
  "post_date": "2014-07-17T15:05:40.097Z",
  "votes": null,
  "comment_count": 1,
  "views": 4397,
  "content": "<p>Hi,</p>\n<p>I am using R 3.0.2 64-bit on Windows 7 and had first a bit problems reading in the Russian data using it (I have been told UTF R read.csv etc works much better/easier at Linux/Macintosh).</p>\n<p>I noticed that easiest way to read Russian UTF8 to R is to change locale first to Russian and then just do read.csv with UTF-8 encoding (without locale changing it was always failing, as my normal locale in Windows is Finnish). This can be done as:</p>\n<p># Read first 10 rows<br>Sys.setlocale('LC_ALL', 'russian');<br>d2 &lt;- read.csv(&quot;avito_test.tsv&quot;, encoding=&quot;UTF-8&quot;, sep=&quot;\\t&quot;, header=TRUE, nrows=10)</p>\n<p>Update:<br>- Above has issue that reading of data breaks (when going to say nrows=1000). I thought changing UTF-8 to UTF8 helped but it did not, it reads all in but data in cyrillic font is totally broken.</p>\n<p>So any help how to get data loaded in at R is appreciated :)</p>",
  "messages": [
    {
      "id": "50785",
      "postDate": "07/17/2014 15:05:40",
      "content": "<p>Hi,</p>\n<p>I am using R 3.0.2 64-bit on Windows 7 and had first a bit problems reading in the Russian data using it (I have been told UTF R read.csv etc works much better/easier at Linux/Macintosh).</p>\n<p>I noticed that easiest way to read Russian UTF8 to R is to change locale first to Russian and then just do read.csv with UTF-8 encoding (without locale changing it was always failing, as my normal locale in Windows is Finnish). This can be done as:</p>\n<p># Read first 10 rows<br>Sys.setlocale('LC_ALL', 'russian');<br>d2 &lt;- read.csv(&quot;avito_test.tsv&quot;, encoding=&quot;UTF-8&quot;, sep=&quot;\\t&quot;, header=TRUE, nrows=10)</p>\n<p>Update:<br>- Above has issue that reading of data breaks (when going to say nrows=1000). I thought changing UTF-8 to UTF8 helped but it did not, it reads all in but data in cyrillic font is totally broken.</p>\n<p>So any help how to get data loaded in at R is appreciated :)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "50857",
      "postDate": "07/18/2014 15:39:16",
      "content": "<p>Solved above issue - loading with locale set to Russian only without encoding specification seems to be working. However, cyrillic characters are just displayed totally wrong - atleast on my machine.</p>\n<p>Below is R sample code if someone has any use for it. It does not score well, but is just simple example. It does not use title, keywords in modelling just category, subcategory, price and urls_cnt.</p>\n<p><code># Load in data<br>setwd(&quot;H:\\\\kaggle\\\\avito_prob_Content&quot;)<br></code><code>Sys.setlocale('LC_ALL', 'russian');<br>dTrain &lt;- read.csv(&quot;avito_train.tsv&quot;, sep=&quot;\\t&quot;, header=TRUE, &nbsp;nrows=1562937)<br><br></code><code># Pre-process (replace NA values by zeroes)<br>dTrain$price[is.na(dTrain$price)] &lt;- 0<br>dTrain$is_blocked[is.na(dTrain$is_blocked)] &lt;- 0<br>dTrain$urls_cnt[is.na(dTrain$urls_cnt)] &lt;- 0<br><br></code><code># Create new fields: log price, zero price indicator and <br># merged category field<br>dTrain$logPrice &lt;- log(1+dTrain$price)<br>dTrain$zeroPrice &lt;- as.integer(dTrain$price == 0)<br>dTrain$mergedCat &lt;- paste(dTrain$category,dTrain$subcategory, sep=&quot;/&quot;)<br><br></code><code># compute E[is_blocked|category,subcategory] to lookup &quot;table&quot;&nbsp;<br>ts1 &lt;- tapply(dTrain$is_blocked, dTrain$mergedCat, mean)<br>categories = as.numeric(ts1)<br>names(categories) = rownames(ts1)<br><br></code><code># Prepare training data<br>dSample &lt;- data.frame(is_blocked=dTrain$is_blocked)<br>dSample$mergedCat &lt;- dTrain$mergedCat<br>dSample$logPrice &lt;- dTrain$logPrice<br>dSample$urls_cnt &lt;- dTrain$urls_cnt<br>dSample$mergedCatScore &lt;- categories[dSample$mergedCat] <br>dSample$Z1 &lt;- dSample$logPrice * dSample$mergedCatScore <br><br></code><code># Train logistic regression model<br>glm.out1 = glm(is_blocked ~ mergedCatScore + logPrice + urls_cnt + Z1, family=binomial(logit), data=dSample)<br><br></code><code># Evaluate model (just visually)<br>outputData &lt;- data.frame(is_blocked=dSample$is_blocked, estimated=fitted(glm.out1), mergedCat=dSample$mergedCatScore)<br>sortedData &lt;- outputData[order(-outputData$estimated<br>),]<br>cs1 &lt;- cumsum(sortedData$is_blocked)<br>plot(cs1,col=&quot;#880088&quot;,cex=0.1,xlim=c(0,500000))</code><code>&nbsp;<br><br></code><code># Load test data in<br>dTest &lt;- read.csv(&quot;avito_test.tsv&quot;, sep=&quot;\\t&quot;, header=TRUE)<br><br></code><code># Pre-process test data, similarly as dTrain<br>dTest$price[is.na(dTest$price)] &lt;- 0<br>dTest$urls_cnt[is.na(dTest$urls_cnt)] &lt;- 0<br>dTest$logPrice &lt;- log(1+dTest$price)<br>dTest$zeroPrice &lt;- as.integer(dTest$price == 0)<br>dTest$mergedCat &lt;- paste(dTest$category,dTest$subcategory, sep=&quot;/&quot;)<br><br></code><code># Save R objects (they are faster to load and compressed) which<br># are not used in this example but in case you want to use later.<br>save(dTest, file=&quot;dTest.dat&quot;)<br>save(dTrain, file=&quot;dTrain.dat&quot;)<br><br></code><code># Check for covariate shift, just via numbers / eyes :)<br># (if there is such then that is indication that weighted logistic <br># regression should be applied)<br>summary(dTrain$urls_cnt)<br>summary(dTest$urls_cnt)</code><code>summary(dTrain$logPrice)<br>summary(dTest$logPrice)<br># -&gt; tail of dTest$urls_cnt is longer than dTrain$urls_cnt, but <br># otherwise no clear covariate shift observed<br></code><code><br># Prepare test data<br>dSample &lt;- data.frame(mergedCat=dTest$mergedCat)<br>dSample$logPrice &lt;- dTest$logPrice<br>dSample$urls_cnt &lt;- dTest$urls_cnt<br>dSample$mergedCatScore &lt;- categories[dSample$mergedCat] <br>dSample$Z1 &lt;- dTest$logPrice * dSample$mergedCatScore<br><br></code><code># Do predictions<br>output &lt;- predict(glm.out1,type=&quot;response&quot;,dSample)<br></code><code>outputData &lt;- data.frame(id=dTest$itemid, estimated=output)<br>sortedData &lt;- outputData[order(-outputData$estimated<br>),]<br>sortedData$estimated &lt;- NULL<br><br></code><code># write output to file<br>write.csv(sortedData,&quot;submission1.csv&quot;,row.names=FALSE)</code><code><code><br></code></code></p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 50857,
      "author_name": "sfin2015",
      "author_url": "",
      "post_date": "07/18/2014 15:39:16",
      "content": "<p>Solved above issue - loading with locale set to Russian only without encoding specification seems to be working. However, cyrillic characters are just displayed totally wrong - atleast on my machine.</p>\n<p>Below is R sample code if someone has any use for it. It does not score well, but is just simple example. It does not use title, keywords in modelling just category, subcategory, price and urls_cnt.</p>\n<p><code># Load in data<br>setwd(&quot;H:\\\\kaggle\\\\avito_prob_Content&quot;)<br></code><code>Sys.setlocale('LC_ALL', 'russian');<br>dTrain &lt;- read.csv(&quot;avito_train.tsv&quot;, sep=&quot;\\t&quot;, header=TRUE, &nbsp;nrows=1562937)<br><br></code><code># Pre-process (replace NA values by zeroes)<br>dTrain$price[is.na(dTrain$price)] &lt;- 0<br>dTrain$is_blocked[is.na(dTrain$is_blocked)] &lt;- 0<br>dTrain$urls_cnt[is.na(dTrain$urls_cnt)] &lt;- 0<br><br></code><code># Create new fields: log price, zero price indicator and <br># merged category field<br>dTrain$logPrice &lt;- log(1+dTrain$price)<br>dTrain$zeroPrice &lt;- as.integer(dTrain$price == 0)<br>dTrain$mergedCat &lt;- paste(dTrain$category,dTrain$subcategory, sep=&quot;/&quot;)<br><br></code><code># compute E[is_blocked|category,subcategory] to lookup &quot;table&quot;&nbsp;<br>ts1 &lt;- tapply(dTrain$is_blocked, dTrain$mergedCat, mean)<br>categories = as.numeric(ts1)<br>names(categories) = rownames(ts1)<br><br></code><code># Prepare training data<br>dSample &lt;- data.frame(is_blocked=dTrain$is_blocked)<br>dSample$mergedCat &lt;- dTrain$mergedCat<br>dSample$logPrice &lt;- dTrain$logPrice<br>dSample$urls_cnt &lt;- dTrain$urls_cnt<br>dSample$mergedCatScore &lt;- categories[dSample$mergedCat] <br>dSample$Z1 &lt;- dSample$logPrice * dSample$mergedCatScore <br><br></code><code># Train logistic regression model<br>glm.out1 = glm(is_blocked ~ mergedCatScore + logPrice + urls_cnt + Z1, family=binomial(logit), data=dSample)<br><br></code><code># Evaluate model (just visually)<br>outputData &lt;- data.frame(is_blocked=dSample$is_blocked, estimated=fitted(glm.out1), mergedCat=dSample$mergedCatScore)<br>sortedData &lt;- outputData[order(-outputData$estimated<br>),]<br>cs1 &lt;- cumsum(sortedData$is_blocked)<br>plot(cs1,col=&quot;#880088&quot;,cex=0.1,xlim=c(0,500000))</code><code>&nbsp;<br><br></code><code># Load test data in<br>dTest &lt;- read.csv(&quot;avito_test.tsv&quot;, sep=&quot;\\t&quot;, header=TRUE)<br><br></code><code># Pre-process test data, similarly as dTrain<br>dTest$price[is.na(dTest$price)] &lt;- 0<br>dTest$urls_cnt[is.na(dTest$urls_cnt)] &lt;- 0<br>dTest$logPrice &lt;- log(1+dTest$price)<br>dTest$zeroPrice &lt;- as.integer(dTest$price == 0)<br>dTest$mergedCat &lt;- paste(dTest$category,dTest$subcategory, sep=&quot;/&quot;)<br><br></code><code># Save R objects (they are faster to load and compressed) which<br># are not used in this example but in case you want to use later.<br>save(dTest, file=&quot;dTest.dat&quot;)<br>save(dTrain, file=&quot;dTrain.dat&quot;)<br><br></code><code># Check for covariate shift, just via numbers / eyes :)<br># (if there is such then that is indication that weighted logistic <br># regression should be applied)<br>summary(dTrain$urls_cnt)<br>summary(dTest$urls_cnt)</code><code>summary(dTrain$logPrice)<br>summary(dTest$logPrice)<br># -&gt; tail of dTest$urls_cnt is longer than dTrain$urls_cnt, but <br># otherwise no clear covariate shift observed<br></code><code><br># Prepare test data<br>dSample &lt;- data.frame(mergedCat=dTest$mergedCat)<br>dSample$logPrice &lt;- dTest$logPrice<br>dSample$urls_cnt &lt;- dTest$urls_cnt<br>dSample$mergedCatScore &lt;- categories[dSample$mergedCat] <br>dSample$Z1 &lt;- dTest$logPrice * dSample$mergedCatScore<br><br></code><code># Do predictions<br>output &lt;- predict(glm.out1,type=&quot;response&quot;,dSample)<br></code><code>outputData &lt;- data.frame(id=dTest$itemid, estimated=output)<br>sortedData &lt;- outputData[order(-outputData$estimated<br>),]<br>sortedData$estimated &lt;- NULL<br><br></code><code># write output to file<br>write.csv(sortedData,&quot;submission1.csv&quot;,row.names=FALSE)</code><code><code><br></code></code></p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "50785": "",
    "50857": ""
  },
  "source": "meta"
}