我如何转换" RWeka"决策树成为一个" party" R中的树?

时间:2016-05-15 18:56:12

标签: r weka party

我在R中使用RWeka软件包以适应M5&#39;使用&#34; M5P&#34;将树映射到数据集。然后我想将生成的树转换为&#34; party&#34;树,这样我就可以访问变量的重要性。我遇到的问题是我似乎无法使函数"Error: all(sapply(split, head, 1) %in% c("<=", ">")) is not TRUE" 正常工作而不会出现以下错误:

n <- nrow(data)

k <- 5

indCV <- sample( rep(1:k,each=ceiling(n/k)), n)


for(i in 1:k){

#Training data is for all the observations where indCV is not equal to i

training_data <- data.frame(x[-which(indCV==i),])

training_response <- y[-which(indCV==i)]

#Test the data on the fifth of the data where the observation indices are equal to i

test_data <- x[which(indCV==i),]

test_response <- y[which(indCV==i)]

#Fit a pruned model to the training data

fit <- M5P(training_response~., data=training_data, control=Weka_control(N=TRUE))

#Convert to party

p <- as.party(fit)
}

只有当我在for循环中应用函数时才会出现此错误,但是当我运行5次交叉验证时,for循环是必需的。

以下是我一直在运行的代码:

RWeka

1 个答案:

答案 0 :(得分:2)

M5P包中有一个将party树转换为example("M5P", package = "RWeka")个对象的示例。如果您运行partykit,则树可视化实际上由plot(m3)绘制。运行示例后,请参阅as.party(m3)J48

但是,对于constparty,您可以获得一个完全成熟的M5P对象,party也是如此。在后一种情况下,树结构本身可以转换为lm,但节点内的线性模型不能完全直接转换为party个对象。因此,如果要使用party表示来计算仅依赖于树结构的度量(例如,用于拆分的变量,拆分数,拆分点等),那么您可以这样做。但是,如果您想要计算取决于模型或预测的度量(例如,均方误差等),那么public class FragmentGlobalfeed extends android.support.v4.app.Fragment { public static final int CONNECTION_TIMEOUT = 10000; public static final int READ_TIMEOUT = 15000; private android.support.v7.widget.RecyclerView mRVProfile; private com.example.admin.quoteme.AdapterProfile mAdapter; private android.widget.LinearLayout llLayout; @Override public void onCreate(android.os.Bundle savedInstanceState) { super.onCreate(savedInstanceState); //background doesn't seem to be able to launch so i am gonna take an alt step and try methods to update //BackgroundFeed backgroundFeed = new BackgroundFeed(getActivity()); //backgroundFeed.execute(); /*AsyncLogin asyncLogin = new com.example.admin.quoteme.FragmentGlobalfeed.AsyncLogin(); asyncLogin.execute();*/ new com.example.admin.quoteme.FragmentGlobalfeed.AsyncLogin().execute(); } @Override public android.view.View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState) { // Inflate the layout for this fragment android.view.View view = inflater.inflate(com.example.admin.quoteme.R.layout.fragmentglobalfeed, null); java.util.List<Profile> data = new java.util.ArrayList<>(); //recyclerview mRVProfile = (android.support.v7.widget.RecyclerView) view.findViewById(com.example.admin.quoteme.R.id.recyclerViewGlobal); mRVProfile.setLayoutManager(new android.support.v7.widget.LinearLayoutManager(getActivity())); mAdapter = new AdapterProfile(getActivity(), data); mRVProfile.setAdapter(mAdapter); return view; } private class AsyncLogin extends android.os.AsyncTask<String, String, String> { java.net.HttpURLConnection conn; java.net.URL url = null; @Override protected void onPreExecute(){ super.onPreExecute(); } @Override protected String doInBackground(String... params) { try{ //enter url address where ur json file is url = new java.net.URL("http:http://192.168.0.100/Quoteme/getfeed.php"); } catch (java.net.MalformedURLException e){ e.printStackTrace(); return e.toString(); } try { conn = (java.net.HttpURLConnection) url.openConnection(); conn.setReadTimeout(READ_TIMEOUT); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setRequestMethod("GET"); conn.setDoOutput(true); } catch (java.io.IOException e1) { e1.printStackTrace(); return e1.toString(); } try{ int response_code = conn.getResponseCode(); if(response_code == java.net.HttpURLConnection.HTTP_OK){ //read data java.io.InputStream input = conn.getInputStream(); java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(input)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } return (result.toString()); } else { return("unsuccessful"); } } catch (java.io.IOException e){ e.printStackTrace(); return e.toString(); } finally { conn.disconnect(); } } @Override protected void onPostExecute(String result){ java.util.List<Profile> data = new java.util.ArrayList<>(); try { org.json.JSONArray jArray = new org.json.JSONArray(result); //extraction for(int i= 0; i<jArray.length(); i++){ org.json.JSONObject json_data = jArray.getJSONObject(i); Profile profile = new Profile(); profile.setUser_id(json_data.getString("user_id")); profile.setQuote_points(json_data.getInt("quote_points")); profile.setQuote(json_data.getString("quote_description")); data.add(profile); } //setup and hand data over to rv // adapter /*mRVProfile = (android.support.v7.widget.RecyclerView)llLayout.findViewById(com.example.admin.quoteme.R.id.recyclerViewGlobal); mAdapter = new AdapterProfile(getActivity(), data); mRVProfile.setAdapter(mAdapter); mRVProfile.setLayoutManager(new android.support.v7.widget.LinearLayoutManager(getActivity()));*/ } catch (org.json.JSONException e){ android.widget.Toast.makeText(getActivity(),e.toString(), android.widget.Toast.LENGTH_LONG); } } } 类将不会有太大帮助。

相关问题